News:

Happy New Year!

Main Menu

VBA to Print drawing area and text area to pdf.

Started by Biomedmike, October 28, 2024, 01:21:40 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Biomedmike

Hi All, I am new to this forum but work with Visio often. Attached is a Topology Creator I developed specific to my role as a sales support person. Maybe this will help someone but I come with a request for help as well.

As you can see, many questions are asked on the left of this document. As they are answered the topology is built. As it is built, the answers to the questions are displayed to the right of the topology.

What I can not figure out is how to add VBA to print (Or save as pdf) just the Topology and the answers to the questions (The text that is added to the right) as a PDF on 2 seperate 8.5x11 pages.

Can anyone help me with the VBA to do this? A link to the file is pasted below.

https://drive.google.com/file/d/1lc_V10k52TmwW3AVXlQFYgM1_mtbB80W/view?usp=sharing 

Thank you all,

Mike

wapperdude

I'm confused.  I downloaded your file, expecting to see a filled in example of what you wanted printed.  But I see 1 page, Drawing and Decisions.  Drawing page has large shape with title Hospital Name  and a date in lower left corner.  Also present is a selection form, but it doesn't seem to have any selections or answers present.  So what are you trying to print/save as PDF?

Under printing, there is option to print only selected items.  Under SaveAs, there is similar Selection checkbox.  Both print and saveas can be specified as PDF.
Visio 2019 Pro

Biomedmike

Hello, Thank you very much for downloading and looking at the file.

Do you mind clicking some of the command buttons to anser the question? Most are yes/no. Just click a bunch of the buttons and the drawing will apprea. (It will build itself with the buttons you click). It will also make text appear to the right of the drawing.

I was looing to create a Macor that one could click and then print QTY-2 8.5x11 pages. 1 Page with the draweing that was built and the other page with the text that appreas as the command buttins are se4lected.

Thank you again for taking the time to look at this!

Mike

wapperdude

OK.  I see now.  To get 2 PDF pages, one for the main shape and 1 for the added text, a possible approach would be to create a 2nd Visio page as place-holder for the text.  This could be a temporary page, created for printing & saving purposes.  Then you would print the 2 pages or save them to PDF file.
Visio 2019 Pro

Biomedmike

I see. I am using "Layers" that toggle on and off to create the Topology Drawing and also to create the text that is on the right hand side of the drawing. I did not know how to turn layers on and off on a page that is not the same page as the associated command buttons are on.

If there is a way to do that I understand what you are saying.

Thank you again for your feedback.

Mike

Nikolay

I would do it very simply - just put a second page next to the first one.
I mean, keep the page print size 8.5x11 but the drawing size 17x11
That should do the trick automatically

Didn't this work?


Biomedmike

I guess I want my cake and eat it too. I am used to doing VBA in Excel. It seems unlimited. Visio on the other hand seems very limited without the ability to do very specific things.

I want the users to use "Presentation Mode" to make the selections on the left. If I change the page setup as you suggest it messes up the ability to see all 3 pages in the Presentation mode. So - I then changed your suggestion (which is a great one by the way) to Page Size Landscape 25.5 x 11. Then I see all 3 pages in Presentation mode. (Smaller than desired but I am starting to find Visio VBA is limited). Then when I print I get all 3 pages. I guess I will have to live with that.

Thank you very much for taking the time to review the document and responding to me.

Mike

Yacine

#7
There are many ways to accomplish your task.

The easiest is Nikolay's solution, but to consider your objection as to keep only one page for presenstations can easily be solved by wrapping the printing operation in a macro, that would first change the page topology for the printing purpose, then revert it back to original.
To access the relevant fields: activepage.pagesheet.Cells("PageWidth").FormulaU ... etc.

Another could be to select the report shapes and export them alone. Either manually or by code.
Yacine

Biomedmike

Hi Yacine,

Is there any way you could list the VBA code I would need to complete the action you mention above? I just can't figure it out despite my many attempts.

Thank you.

Mike

Yacine

There are several ways to write the macro you want.

1. Using the macro-recorder

Start the macro recorder and perform the operations needed. Usually you'll get some hard-coded references to objects. You'll need to generalize them.

2. Use the shapesheet

If you're a little bit more proficient in VBA coding, but miss the names of the cells to target, just open the shapesheet of the object (in your case the page - right click on an empty area of the page and select shapesheet). Perform the operations and observe which cells get modified.

In the shapesheet, you won't see the names of the cells directly, but you can follow these steps to identify them:
- Select any other cell.
- Type an "=" sign.
- Click the cell you are targeting.
- The name of the target cell will be written in the selected cell.
- Undo the changes once you know the name.

Use this cell name in your code.

3. Ask an AI

Visio being a niche product, the AIs usually make errors when writing Visio code, but at least you get the structure of your macro. You'll then need to adjust some formulas. That's where you can refer to the shapesheet or to Microsoft's online documentation: [https://learn.microsoft.com](https://learn.microsoft.com)

Below is the code, which provides a solution to your request.


Sub DoublePageWidthAndPrint()
    Dim vApp As Visio.Application
    Dim vDoc As Visio.Document
    Dim vPage As Visio.Page
    Dim originalWidth As Double
    Dim originalPagesX As Integer
    Dim originalOnPage As Boolean
   
    Set vApp = Visio.Application
    Set vDoc = vApp.ActiveDocument
    Set vPage = vApp.ActivePage
   
    ' Store original settings
    originalWidth = vPage.PageSheet.CellsU("PageWidth").Result("inches")
    originalPagesX = vPage.PageSheet.CellsU("PagesX").ResultIU
    originalOnPage = vPage.PageSheet.CellsU("OnPage").ResultIU
   
    ' Modify page width to double and set tiling to split into two printer pages
    vPage.PageSheet.CellsU("PageWidth").Formula = "17 in"
    vPage.PageSheet.CellsU("PagesX").Formula = "2"
    vPage.PageSheet.CellsU("OnPage").Formula = True
   
    ' Print the document
    vDoc.PrintOut visPrintCurrentPage
   
    ' Revert to original settings
    vPage.PageSheet.CellsU("PageWidth").Formula = originalWidth
    vPage.PageSheet.CellsU("PagesX").Formula = originalPagesX
    vPage.PageSheet.CellsU("OnPage").Formula = originalOnPage
End Sub

Yacine

Biomedmike

Hi Yacine,

I am leaving for the day and will try this when I get home.

Thank you again for taking the time to help me.

I will upload a new file with this code in it.

Take care,

Mike

Biomedmike

Hi Yacine,

All I can say is WOW! You are absolutely amazing!!!

If there is any way you could do 1 more thing, I would be very grateful.

Is it possible to modify the code to save as pdf (instead of printing to my printer)?

Also, is there any way to thank you monetarily for taking the time to help me?

Mike

wapperdude

#12
Sorry.  Been away.  Glad you have solution.

Need to clarify and add an option.
QuoteI did not know how to turn layers on and off on a page that is not the same page as the associated command buttons are on.
You don't have to do anything with the layers...you're just copying shapes to enable printing and saving PDF with a 2nd page.  All layer related actions will have been completed.

In fact, with code below, you don't have to do anything except to manually delete the "printing" page once it is no longer needed.  This wasn't done with the code, so that it may be saved to PDF if desired.
Ah.  I did change your ruler and page settings so that it conforms to standard of having lower left corner at (0,0).


Here's the code.
Sub SelectShps2()
'This macro is run from the ActivePage.
'It uses SpatialNeighborhood to find and select shapes in desired region
'It adds a temporary page, "Printing", copies the selected shapes to the
'new page.
'Both the original and new page are suitable for printing and PDF storing.
'
' Must manually remove printing page.

    Dim curPg As Page
    Dim addPg As Visio.Page
    Dim selBox As Shape
   
    Dim vsoShapeOnPage As Visio.Shape
    Dim intTolerance As Integer
    Dim vsoReturnedSelection As Visio.Selection
    Dim strSpatialRelation As String
    Dim intSpatialRelation As VisSpatialRelationCodes
   
    Set curPg = ActiveDocument.Pages.ItemU("Drawing and Decisions")
    ActiveWindow.DeselectAll
   
' Define region for selection.  Change coordinates as desired.
    Set selBox = ActivePage.DrawRectangle(8.5, 0.25, 15.25, 11)  'Very large.  Will capture everything within its boundary.
    selBox.Cells("LineColor").Formula = "2"        'set color to red
    selBox.Cells("LineWeight").Formula = "8 pt"    'nice fat line
    selBox.Cells("Geometry1.NoFill").Formula = "TRUE"   

'Spatial neighborhood related settings:   
    'Initialize string
    strSpatialRelation = ""
   
    'Set tolerance argument
    intTolerance = 0.25
   
    'Set Spatial Relation argument
    intSpatialRelation = visSpatialOverlap + visSpatialContain + visSpatialTouching
   
    'Get the set of spatially related shapes that meet the criteria set by the arguments.
    Set vsoReturnedSelection = selBox.SpatialNeighbors(intSpatialRelation, intTolerance, 0)
   
    'Evaluate the results.
    If vsoReturnedSelection.Count = 0 Then
        'No shapes met the criteria set by the arguments of the method.
        MsgBox "No shapes found"
    Else
    'Select shapes within defined window area
        For Each vsoSelshp In vsoReturnedSelection
            ActiveWindow.Select vsoSelshp, visSelect
        Next
        ActiveWindow.Selection.Copy
        Set selShps = ActiveWindow.Selection.Group
    End If
   
'Add the temporary page:
    Set addPg = ActiveDocument.Pages.Add
    addPg.Name = "Printing"
    addPg.Background = False
    addPg.Index = 2

    Set drpShps = ActiveWindow.Page.Drop(selShps, 3.5, 5.5).  'Location of group cntr placed here.
 Assumes page origin is (0,0).

' Some tidying up:
    ActiveWindow.DeselectAll
    ActiveWindow.Page = curPg    'Return to main page
    selBox.Delete       'Remove the region defining shape

End Sub
Visio 2019 Pro

Yacine

#13
Quote from: Biomedmike on October 29, 2024, 08:14:53 PMHi Yacine,
All I can say is WOW! You are absolutely amazing!!!
If there is any way you could do 1 more thing, I would be very grateful.
Is it possible to modify the code to save as pdf (instead of printing to my printer)?
Also, is there any way to thank you monetarily for taking the time to help me?
Mike

You may investigate on the different export functions to print to PDF instead of the printer.
https://learn.microsoft.com/en-us/office/vba/api/visio.document.exportasfixedformat

If you happen to have a PDF printer driver, you may also check the options of the printout function. I think there is one to specify the printer.

Regarding any payment: your "WOW" is for me payment enough. That will last for months.  ;)
A small contribution to the forum costs would be appreciated, but we're not ready for that yet: https://visguy.com/vgforum/index.php?topic=10484
Yacine

Biomedmike

I uploaded a new version with the 2 scripts that were written.

https://drive.google.com/file/d/1GP0hJgsZnQjHZNwAeQbvnzDDOfJ2x-Sw/view?usp=sharing   

I added 2 command buttons at the bottom left of the page.

The "print" command and VBA works and sends 2 desired pages to my physical printer. I was also hoping to have a save to pdf button but that code is not working. (No doubt my ignorance).

Could you look at the save as PDF code and see if I copied it in error?

Thank you.

Mike

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 429 (show)
Files included: 35 - 1407KB. (show)
Memory used: 1284KB.
Tokens: post-login.
Cache hits: 19: 0.00469s for 26,551 bytes (show)
Cache misses: 9: (show)
Queries used: 18.

[Show Queries]