Author Topic: Quickly toggle "non-printing" shape property  (Read 19002 times)

0 Members and 1 Guest are viewing this topic.

David.P

  • Full Member
  • ***
  • Posts: 172
Quickly toggle "non-printing" shape property
« on: July 29, 2014, 08:33:31 AM »
Hi forum,

what is the quickest (or is there a quicker than below) way to switch between the "printing/non-printing" status of a shape? I tried to record a VBA macro but it seems to work only on the shape where it has been recorded, not on other shapes.



Thanks for any help
David.P
Visio 2003 for production
Visio 2019

Jumpy

  • Hero Member
  • *****
  • Posts: 1061
Re: Quickly toggle "non-printing" shape property
« Reply #1 on: July 29, 2014, 09:24:42 AM »
I tried to record a VBA macro but it seems to work only on the shape where it has been recorded, not on other shapes.

What did you expect?

Perhaps you can show us the code generated by your macro recorder, and we can show you which line of code to change so your code works on a selected shape or every selected shape (and not only on Shape 42).

David.P

  • Full Member
  • ***
  • Posts: 172
Re: Quickly toggle "non-printing" shape property
« Reply #2 on: July 29, 2014, 09:31:54 AM »
Hmm, I expected the macro to work on any current selection (like in all my Word macros)....

Anyway, here's the code.
Code
Sub Shape_nicht_druckbar()
' Tastenkombination: Strg+q
    Dim UndoScopeID1 As Long
    UndoScopeID1 = Application.BeginUndoScope("Verhaltenseigenschaften")
    Application.ActiveWindow.Page.Shapes.ItemFromID(92).CellsSRC(visSectionObject, visRowMisc, visNonPrinting).FormulaU = "TRUE"
    Application.EndUndoScope UndoScopeID1, True

End Sub

Thanks
David
Visio 2003 for production
Visio 2019

Yacine

  • Hero Member
  • *****
  • Posts: 3208
Re: Quickly toggle "non-printing" shape property
« Reply #3 on: July 29, 2014, 12:27:44 PM »
Quote
Hmm, I expected the macro to work on any current selection (like in all my Word macros)....
Interesting comment. I don't code that much in Word myself, so that I did not realize that other applications do it better than Visio.  ;)

That would however explain why this same question is asked over and over again in the forum.

The trick is to replace the
Code
Application.ActiveWindow.Page.Shapes.ItemFromID(92)
by
Code
ActiveWindow.Selection (1)
This is the shortest form that you can use.

Safer versions would check if at least one shape is selected
Code
if ActiveWindow.Selection.count>0 then
  'your code here
end if

If your code is meant to handle only one shape then:
Code
If ActiveWindow.Selection.Count > 1 Then
    MsgBox "Please select only one shape."
Else
    'your code here
End If

Your code may as well be capable of handling several shapes at once, then:
Code
For Each shp In ActiveWindow.Selection
    'your code here
Next shp

@Jumpy, sorry for the inteference. I'm not only bored in the holliday time, but also wanted to set up a "re-usable" answer to this question. Entschuldigung.
« Last Edit: July 29, 2014, 12:32:08 PM by Yacine »
Yacine

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4837
  • Ideas Visio-lized into solutions
Re: Quickly toggle "non-printing" shape property
« Reply #4 on: July 29, 2014, 01:01:52 PM »
Quote
I'm not only bored in the holliday time...

Well, that explains a lot!  But, Mr. Boredom, your solution is incomplete!   ???  You didn't include the possibility to let the code loop thru every shape on the page and set it to non-printing.  Yeah.  Why would you do that?   :o  Perhaps to prevent people from making hardcopy copies.  I dunno.   ::)
Visio 2019 Pro

David.P

  • Full Member
  • ***
  • Posts: 172
Re: Quickly toggle "non-printing" shape property
« Reply #5 on: July 29, 2014, 01:39:37 PM »
That's great guys! Thanks very much Yacine. I'll try the code and report back asap.
Visio 2003 for production
Visio 2019

Yacine

  • Hero Member
  • *****
  • Posts: 3208
Re: Quickly toggle "non-printing" shape property
« Reply #6 on: July 29, 2014, 02:56:51 PM »
Quote
Well, that explains a lot!  But, Mr. Boredom, your solution is incomplete!     You didn't include the possibility to let the code loop thru every shape on the page and set it to non-printing.
I plead guilty...

Looping through every shape would be as easy as:
Code
For Each shp In ActivePage.Shapes 'note the difference to activewindow
    'your code here
Next shp

Now, how to access also the sub-shapes? You'll need two routines:
Code
Sub MainLoop()
    For Each shp In ActivePage.Shapes 'note the difference to activewindow
        actualProcessingAndDeeperLoop shp
    Next shp
End Sub


Sub actualProcessingAndDeeperLoop(shp As Visio.Shape) 'this one is recursive
    'do something with the shape

    'then handle it's sub-shapes
    For Each subShp In shp
        actualProcessingAndDeeperLoop subShp
    Next subShp
End Sub

... and of course " 'your code here" would then be substituted by
Code
shp.CellsSRC(visSectionObject, visRowMisc, visNonPrinting).FormulaU = "TRUE"

Did I forget an important case?

Well, you may want to loop through the shapes in all the pages of your document, then:

Code
Sub loopThroughAllPages()
Dim tempPg As Variant

    Set tempPg = ActivePage
    For Each pg In ActiveDocument.Pages
        ActiveWindow.Page = visDocument.Pages(pg).Name
        'check for background page
        If Not pg.Background Then
            For Each shp In ActivePage.Shapes
                'your code here
            Next shp
        End If
    Next pg
    'and back to the current page
    ActiveWindow.Page = visDocument.Pages(tempPg).Name
End Sub
« Last Edit: July 30, 2014, 03:11:02 AM by Yacine »
Yacine

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4837
  • Ideas Visio-lized into solutions
Re: Quickly toggle "non-printing" shape property
« Reply #7 on: July 29, 2014, 06:39:31 PM »
 ;D   :o   :D   :P   ???   :D

You really are bored!

Think you did cover every possibility!  But, no more caffeine for you!
Visio 2019 Pro

David.P

  • Full Member
  • ***
  • Posts: 172
Re: Quickly toggle "non-printing" shape property
« Reply #8 on: July 30, 2014, 01:52:30 AM »
Thanks again Yacine, that macro now works beautifully on any selected shape.
Visio 2003 for production
Visio 2019

Jumpy

  • Hero Member
  • *****
  • Posts: 1061
Re: Quickly toggle "non-printing" shape property
« Reply #9 on: July 30, 2014, 03:15:54 AM »
Quote
Hmm, I expected the macro to work on any current selection (like in all my Word macros)....
Interesting comment. I don't code that much in Word myself, so that I did not realize that other applications do it better than Visio.  ;)

That would however explain why this same question is asked over and over again in the forum.

I knew that the macro editor in Word and Excel often works with the selection (and I'm often annoyed by it, because in Excel-VBA it is often slower if you first select sth. and then do sth. with an object, instead of doing the sth. to the object directly). But I did not consiously make the connection, that Visio's behaviour is different, and extremly annoing, too, because in Visio I would like the macro recorder to work with a selection.

So thanks for the insight.

@Jumpy, sorry for the inteference. I'm not only bored in the holliday time, but also wanted to set up a "re-usable" answer to this question. Entschuldigung.

No need to be sorry. Because I'm all alone in the office, everyone and his brother having holidays, I'm quite the opposite of bored and only can spend 5 quick minutes a day in the forum, so your answer came much faster and is much richer than mine would have been.

dirkasarus-rex

  • Jr. Member
  • **
  • Posts: 12
Re: Quickly toggle "non-printing" shape property
« Reply #10 on: February 10, 2015, 03:36:33 PM »
This 'reusable' code works to iterate through grouped and ungrouped shapes.

Thanks Yacine for posting this!   8)

David.P

  • Full Member
  • ***
  • Posts: 172
Re: Quickly toggle "non-printing" shape property
« Reply #11 on: April 15, 2015, 05:21:51 AM »
Ok guys, I now haven't used my non-printing shape property macro:

Code
Sub Make_shape_not_printable()
' Keyboard Shortcut: Ctrl+q
    Dim UndoScopeID1 As Long
    UndoScopeID1 = Application.BeginUndoScope("Verhaltenseigenschaften")
    ActiveWindow.Selection(1).CellsSRC(visSectionObject, visRowMisc, visNonPrinting).FormulaU = "TRUE"
    Application.EndUndoScope UndoScopeID1, True
End Sub

...for a while, and it seems to have lost its keyboard shortcut "ctrl+q" that I had assigned to it.

Is there a way to re-assign that keyboard shortcut like, permanently to this macro?
Visio 2003 for production
Visio 2019

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4837
  • Ideas Visio-lized into solutions
Re: Quickly toggle "non-printing" shape property
« Reply #12 on: April 15, 2015, 12:27:04 PM »
In V2007, to restore the shortcut key:
  Bring up the macros window (Alt+F8) or Tools>macros.  Select (high light) the macro from the list and then press Options...  Enter the desired key in the box.  That's it.

As far as making it permanent, not sure if that's possible, but, ordinarily, I don't think it ought to disappear unless something was done to cause it to be removed.

Wapperdude
Visio 2019 Pro

David.P

  • Full Member
  • ***
  • Posts: 172
Re: Quickly toggle "non-printing" shape property
« Reply #13 on: April 22, 2015, 04:44:06 AM »
Thank you very much Wapperdude,

will try that and report back if it worked.

Cheers David.P
Visio 2003 for production
Visio 2019

David.P

  • Full Member
  • ***
  • Posts: 172
Re: Quickly toggle "non-printing" shape property
« Reply #14 on: July 21, 2016, 07:45:57 AM »
Sorry for taking this long...

Actually I think that this worked. However now, after reinstalling everything including Windows and Visio, I can't assign my keyboard shortcut to my macro anymore.

If I try, Visio tells me that I "should (!) assign a different hotkey to the macro because this key is already assigned to a different macro". However, I can't find a macro that has this shortcut, and I can't remove the shortcut from wherever it might be assigned to currently.

Thanks for any ideas regarding this,

Cheers
David
Visio 2003 for production
Visio 2019