Visio Guy

Visio Discussions => General Visio => Topic started by: ragnar01 on March 25, 2017, 07:23:39 PM

Title: Visio Printing - Select area to Print via VBA
Post by: ragnar01 on March 25, 2017, 07:23:39 PM
Is there a way to select the area to be displayed on screen (and therefore printed) programatically? Lets say on a drawing I want to view/print the area bounded by the rectangle with coordinates (2,2), (6,4)- this should show the 4 x 2 rectangle full screen and let me print the visible area.... but can I do this via VBA?

Thoughts?

Ragnar
Title: Re: Visio Printing - Select area to Print via VBA
Post by: Yacine on March 26, 2017, 08:43:12 AM
https://msdn.microsoft.com/en-us/library/office/ff768157.aspx (https://msdn.microsoft.com/en-us/library/office/ff768157.aspx)
You'll need to zoom to the right view first of course: https://www.google.de/search?newwindow=1&safe=off&q=zoom+vba+site%3Avisguy.com&oq=zoom+vba+site%3Avisguy.com&gs_l=serp.3...9556.10611.0.11394.4.4.0.0.0.0.104.362.3j1.4.0....0...1.1.64.serp..0.0.0.ykT2qQqR3ag (https://www.google.de/search?newwindow=1&safe=off&q=zoom+vba+site%3Avisguy.com&oq=zoom+vba+site%3Avisguy.com&gs_l=serp.3...9556.10611.0.11394.4.4.0.0.0.0.104.362.3j1.4.0....0...1.1.64.serp..0.0.0.ykT2qQqR3ag)
Title: Re: Visio Printing - Select area to Print via VBA
Post by: ragnar01 on March 26, 2017, 04:43:10 PM
Not quite what I had in mind - assuming a large drawing...say E Size 44x34 with shapes/data throughout, I would want to be able to print a specific 17x11 segment of that drawing without having to play around with manual zoom settings. Doesn't really need to be to any scale but really don't want to see info outside the boundaries of the requested segment - I can think of a couple of ways to do this outside of Visio but none are very elegant.

Ragnar
Title: Re: Visio Printing - Select area to Print via VBA
Post by: wapperdude on March 26, 2017, 10:18:03 PM
Visio doesn't really have the print selected area capability like, say Excel, does.  But, you can do a crude approximation with the following macro.  You have to select the shapes that you want printed, then run the macro.  It creates a temporary printing page with pasted shapes only.  Deletes page after printing.


Sub PrintSelShps()
'This macro is run from the ActivePage.
'It assumes that the desired shapes to be printed have been selected.
'It adds a temporary page, "Printing", copies the selected shapes to the
'new page, prints the page, then deletes the page.
'As coded, it assumes there is a background page, "Background-1", which should
'also be included with the printing.  If not, just comment out appropriate lines.

    Dim curPg As Page
    Dim addPg As Visio.Page

    Set curPg = ActiveWindow.Page
    Application.ActiveWindow.Selection.Copy

    Set addPg = ActiveDocument.Pages.Add
    addPg.Name = "Printing"
    addPg.Background = False
    addPg.Index = 2
    addPg.BackPage = "Background-1"
    addPg.PageSheet.CellsSRC(visSectionObject, visRowPageLayout, visPLOSplit).FormulaForceU = "1"
    addPg.PageSheet.CellsSRC(visSectionUser, 0, visUserValue).FormulaForceU = ""

    ActiveWindow.Page.Paste
    ActivePage.PageSheet.CellsSRC(visSectionObject, visRowPrintProperties, visPrintPropertiesOnPage).FormulaU = "1"
'
'The following line was grabbed from macro created by Macro Recorder.  If Printer name is known, use the name.
'If printer name not known, use macro recorder to get it.
    ActiveDocument.PrintOut PrintRange:=visPrintCurrentPage, PrinterName:="Canon MG6200 series Printer"
    ActiveDocument.Pages.ItemU("Printing").Delete True
    ActiveWindow.Page = curPg

End Sub


HTH.
Wapperdude
Title: Re: Visio Printing - Select area to Print via VBA
Post by: wapperdude on March 26, 2017, 11:37:51 PM
Too much spare time. 

So attached has original macro plus a second macro.  The 2nd uses spatialneighbor functionality.  Draw a rectangle over the area to be printed.  Keep the rectangle selected and run the 2nd macro.  Anything that is within, overlaps, or touches the rectangle will be printed.  Any shape form may be used to set the selection area:  ovals, triangles, irregular shapes.  Didn't test if the shape has to be closed or not.

This still isn't exactly a window defined area, but makes a pretty close approximation.

Wapperdude
Title: Re: Visio Printing - Select area to Print via VBA
Post by: ragnar01 on March 27, 2017, 12:35:36 AM
Interesting, will try to cobble together a sample of what I was thinking about and see if these might work - will report back tomorrow.

Tks.

Ragnar
Title: Re: Visio Printing - Select area to Print via VBA
Post by: wapperdude on March 27, 2017, 04:25:57 AM
Updated the file.  Cleaned up, added comments.  Made note of a couple potential issues when creating temporary page.

The macro now masks any shapes that might extend beyond the desired viewing/printing area.

Wapperdude
Title: Re: Visio Printing - Select area to Print via VBA
Post by: wapperdude on March 27, 2017, 04:59:10 AM
If you have V2013 or newer, the Visio object model has been updated to include window.setviewrect and window.set windowrect methods.  These might be more efficienct to use.  Can't say since I'm at V2007.

https://msdn.microsoft.com/en-us/library/office/ff767798.aspx (https://msdn.microsoft.com/en-us/library/office/ff767798.aspx)
https://msdn.microsoft.com/en-us/library/office/ff769098.aspx (https://msdn.microsoft.com/en-us/library/office/ff769098.aspx)

Wapperdude
Title: Re: Visio Printing - Select area to Print via VBA
Post by: JuneTheSecond on March 27, 2017, 06:46:09 AM
Long long time ago I had printed out a big drawing of about 1m x 0.7 m .
I overplayed a grid that divide the drawing into 4 x 4 = 16 pieces.
One piece has a size of a printer page.
I moved the drawing by changing the value of LocPinX and LocPinY with a simple macro
so that the PinX and PinY is the center of each cell of the grid.
After I print out the 16 sheets I pasted them with a transparent tape into a sheet

Title: Re: Visio Printing - Select area to Print via VBA
Post by: Yacine on March 27, 2017, 06:59:26 AM
@Ragnar, the zoom can be set via VBA. There where several links to this issue in the search link I sent you.
@Wapperdude, from V2010 upward Visio offers the possibility to print just the actual view.
Title: Re: Visio Printing - Select area to Print via VBA
Post by: wapperdude on March 27, 2017, 02:13:07 PM
@Yacine:  Thanks, good to know...hope I don't forget.  Still not sufficient reason to upgrade.   :o. Besides it was "fun" coding the suggested approaches.   ;)

I'll probably tweak the code to do the page indexing properly.  Maybe adjust zoom level too.

Wapperdude