Visio Guy

Visio Discussions => General Visio => Topic started by: David.P on July 29, 2014, 01:33:31 PM

Title: Quickly toggle "non-printing" shape property
Post by: David.P on July 29, 2014, 01:33:31 PM
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.

(http://www.swotster.com/swotster/English/Visio2007/images/images_les_023/les23_image03_en.jpg)

Thanks for any help
David.P
Title: Re: Quickly toggle "non-printing" shape property
Post by: Jumpy on July 29, 2014, 02:24:42 PM
Quote from: David.P on July 29, 2014, 01:33:31 PM
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).
Title: Re: Quickly toggle "non-printing" shape property
Post by: David.P on July 29, 2014, 02:31:54 PM
Hmm, I expected the macro to work on any current selection (like in all my Word macros)....

Anyway, here's the 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
Title: Re: Quickly toggle "non-printing" shape property
Post by: Yacine on July 29, 2014, 05:27:44 PM
QuoteHmm, 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
Application.ActiveWindow.Page.Shapes.ItemFromID(92)
by
ActiveWindow.Selection (1)
This is the shortest form that you can use.

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


If your code is meant to handle only one shape then:
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:
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.
Title: Re: Quickly toggle "non-printing" shape property
Post by: wapperdude on July 29, 2014, 06:01:52 PM
QuoteI'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.   ::)
Title: Re: Quickly toggle "non-printing" shape property
Post by: David.P on July 29, 2014, 06:39:37 PM
That's great guys! Thanks very much Yacine. I'll try the code and report back asap.
Title: Re: Quickly toggle "non-printing" shape property
Post by: Yacine on July 29, 2014, 07:56:51 PM
QuoteWell, 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:
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:
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
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:

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

Title: Re: Quickly toggle "non-printing" shape property
Post by: wapperdude on July 29, 2014, 11:39:31 PM
 ;D   :o   :D   :P   ???   :D

You really are bored!

Think you did cover every possibility!  But, no more caffeine for you!
Title: Re: Quickly toggle "non-printing" shape property
Post by: David.P on July 30, 2014, 06:52:30 AM
Thanks again Yacine, that macro now works beautifully on any selected shape.
Title: Re: Quickly toggle "non-printing" shape property
Post by: Jumpy on July 30, 2014, 08:15:54 AM
Quote from: Yacine on July 29, 2014, 05:27:44 PM
QuoteHmm, 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.

Quote from: Yacine on July 29, 2014, 05:27:44 PM
@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.
Title: Re: Quickly toggle "non-printing" shape property
Post by: dirkasarus-rex on February 10, 2015, 08:36:33 PM
This 'reusable' code works to iterate through grouped and ungrouped shapes.

Thanks Yacine for posting this!   8)
Title: Re: Quickly toggle "non-printing" shape property
Post by: David.P on April 15, 2015, 10:21:51 AM
Ok guys, I now haven't used my non-printing shape property macro:

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?
Title: Re: Quickly toggle "non-printing" shape property
Post by: wapperdude on April 15, 2015, 05: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
Title: Re: Quickly toggle "non-printing" shape property
Post by: David.P on April 22, 2015, 09:44:06 AM
Thank you very much Wapperdude,

will try that and report back if it worked.

Cheers David.P
Title: Re: Quickly toggle "non-printing" shape property
Post by: David.P on July 21, 2016, 12:45:57 PM
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
Title: Re: Quickly toggle "non-printing" shape property
Post by: wapperdude on July 21, 2016, 11:59:25 PM
What key are you trying to assign?

Didn't notice which version of Visio you're using.

Wapperdude
Title: Re: Quickly toggle "non-printing" shape property
Post by: David.P on July 22, 2016, 06:11:44 AM
It was the same key Ctrl+Q that I used previously. I'm using Visio 2003.

Actually I recorded a dummy macro called "asdf" in order to paste the above code into it, and assigned Ctrl+Q with the "asdf" macro.

Afterwards however, that shortcut did not work (possibly because the macro was called "Make_shape_not_printable" instead of "asdf" after pasting the code). Then I tried to assign Ctrl+Q again, to the "Make_shape_not_printable macro" -- but got the message that Ctrl+Q was already taken.
Title: Re: Quickly toggle "non-printing" shape property
Post by: wapperdude on July 22, 2016, 02:18:45 PM
A little confused.  The new asdf macro is in same or different document?  The "Q" shortcut key worked at first for asdf?  Then, you pasted the code into it, but did not change the macro name, and the key still worked?  Then, you renamed the macro and it stopped working?

What I would do...
In a new doc, do the sequence of steps above and note when the key stops working.  It should stop with name change, change name back to asdf and try key again.  Assuming this works, confirms the key is assigned to specific macro.  Now, delete asdf macro.  Record new macro, say mac1.  Try to assign your "Q" key.  If it doesn't take, then Visio isn't releasing the assignment.  But, I think it should work.

Assuming the last step worked, open your problem document, make a backup copy, delete the macro.  Might want to save as some other name.  Perhaps close and reopen this doc.  Paste your macro.  Assign the key.  I think it ought to take .

You could just do the last part 1st.  If it works, done.  If not, then do the 1st set of what I would stuff to find out where it fails.

Wapperdude

Wapperdude
Title: Re: Quickly toggle "non-printing" shape property
Post by: David.P on July 22, 2016, 02:30:52 PM
> The new asdf macro is in same or different document?
Same document

> The "Q" shortcut key worked at first for asdf? 
Didn't try the key

> Then, you pasted the code into it, but did not change the macro name, and the key still worked?
Didn't try the key

> Then, you renamed the macro and it stopped working?
I did not manually rename the macro. It was renamed by the pasting which contained the new macro name.

> open your problem document, make a backup copy, delete the macro.  Perhaps close and reopen this doc.  Paste your macro.  Assign the key.

Thanks, that worked! The key assignment was accepted.

BTW, with Visio, there is no template document where you can save your macro collection to (like normal.dot with Word), is there?
Title: Re: Quickly toggle "non-printing" shape property
Post by: wapperdude on July 22, 2016, 04:10:29 PM
No template file as you're suggesting.  You can save your macros to a common file location and then provide Visio with a reference path to that directory.

Wapperdude
Title: Re: Quickly toggle "non-printing" shape property
Post by: David.P on July 26, 2016, 03:39:02 PM
Thank you Wapperdude.

Where in the settings do I provide Visio with that reference path?

And does this mean that my macros will be available in every new Visio document as well?
Title: Re: Quickly toggle "non-printing" shape property
Post by: wapperdude on July 26, 2016, 04:38:18 PM
For starters, see:  http://visguy.com/vgforum/index.php?topic=4886.0 (http://visguy.com/vgforum/index.php?topic=4886.0)

Wapperdude
Title: Re: Quickly toggle "non-printing" shape property
Post by: David.P on July 26, 2016, 05:11:46 PM
Thanks, will study that.