Visio Guy

Visio Discussions => Programming & Code => Topic started by: dakndor on January 31, 2023, 05:19:08 AM

Title: Export as Fixed Format VBA for all pages in Doc
Post by: dakndor on January 31, 2023, 05:19:08 AM
Hi Brains Trust

I'm piecing together some VBA code to help my team automate exporting of large visio files (20+ pages) as separate PDF files.

So far I have been successful in getting this to work for all pages if I use the pg.Name as the file name source.

What we would like to do is draw data from a shapesheet reference to create individual file names for the outputted files.

I have written the code that I think will do this but I am having an issue with getting this code to properly run through each page. Currently it only manages to save the first page in the document. 



Public Sub ExportAllPages_ToPDF()

    Dim formatExtension As String
    formatExtension = ".pdf" '...or .bmp, .jpg, .png, .tif

    '// Init folder, doc and counter:
   Dim FldrName As String
Dim PageName As String
Dim FileName As String
Dim pg As Page
Dim shpName As Variant
Dim vsoShp As Shape
Dim i As Integer
i = 1

On Error Resume Next
If Len(ThisDocument.Name) < 1 Then
MsgBox "You need first to save this document. The pictures will be then saved in the same directory."
Else
FldrName = ThisDocument.Path
For Each pg In ThisDocument.Pages
PageName = pg.Name
PageName = Replace(PageName, "/", "__")
Set vsoShp = ActivePage.Shapes.Item("The Data")

FileName = vsoShp.CellsU("Prop.FILE_NAME").ResultStr(Visio.visNone) & ".pdf"
If Not (pg.Background) Then
Call ActiveDocument.ExportAsFixedFormat(visFixedFormatPDF, FileName, visDocExIntentPrint, visPrintFromTo, i, i)
i = i + 1
End If
Next
End If
   

Cleanup:
    Set doc = Nothing
End Sub



The shape that is being called in Set vsoShp is located on every page and self updates with a unique file name based on data within the page.  I am guessing my issue here is the use of ActivePage.Shapes.Item("") but I can't figure out what to change this to.

I'm hoping someone here can help as I'm a little bit stumped here.  Thanks!
Title: Re: Export as Fixed Format VBA for all pages in Doc
Post by: wapperdude on January 31, 2023, 04:11:16 PM
Replace "Activepage" with "pg".  Otherwise, you'll have to set each page as active ahead of that code line.
Title: Re: Export as Fixed Format VBA for all pages in Doc
Post by: dakndor on January 31, 2023, 10:19:20 PM
Hey Wapperdude

Thanks! That has fixed it.  I was pretty tired when I was piecing this together so I glossed over the Activepage ref.

Appreciate the support and I'm looking forward to getting more involved in this VBA stuff with Visio.