• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Visio Guy

Smart graphics for visual people




  • Home
  • Hire Me
    • Hire Me
    • Résumé
  • Products
    • Products
    • Bubble Revision Shape
    • Layers to Pages Utility
    • Rack Unit Dimension Line
    • Radial Elements Tool with up to 100 Wedges
    • Text on a Circle Visio SmartShape
  • Index
    • Articles by Date
    • YouTube – VisioGuy
    • Download Information
    • Suggestion Box
    • Shop
    • Visio Art
    • Visio Links
    • – Visio Shapes & Stencils
    • – Visio Templates & Drawings
  • About
    • About
    • Donate
    • GitHub
    • jsFiddle
    • Reading List
    • Subscribe & Follow
      • – E-mail
      • – facebook
      • – Twitter
      • – RSS
    • Privacy Policy
  • Discussion Forum
You are here: Home / Development / Code / Export All Pages in Document

Export All Pages in Document

September 18, 2006 By Visio Guy 41 Comments

Every once in a while, a question comes along in the newsgroups that just screams at me; “WRITE SOME SAMPLE CODE!” Now I can’t describe exactly which types of questions speak to me in this way. Perhaps they’re questions I’ve heard time and again? Or they involve a fascinating graphical problem? Or is it just that I can actually finish them in one evening’s work?Whatever the reason, the latest question is; “How do I export all of the pages in a Visio document to image files?”

Export All Pages in Visio Document

Well, the simple answer is to use the Page.Export function, and simply give the name of an image file as an argument.

For instance: To do all the pages in a document simply requires a loop through each Page object in Document.Pages. To do all the pages in a document simply requires a loop through each Page object in Document.Pages.

But that’s only half the answer.

The big bone about programatically exporting images in Visio is that you can’t programmatically control the resolution of the output. I believe this is because the export filters (originally?) came from third parties, and didn’t necessarily have automation interfaces. But all is not entirely lost. If you manually export an image, you’ll see this dialog:

 

Visio Export Filter Options

You can set various options for the export, including the resolution, which is what most folks want to do. This seems all well and good, except for the fact that you can’t get at it programmatically, and even worse, Visio forgets your settings every time you close Visio.With all these limitations in mind, it seems like we’re left with no choice but a work-around that looks like this:

1. Manually export an image using SaveAs.
2. Set the resolution in the dialog.
3. Before shutting Visio down, run any automation code to export multiple images.

And that is exactly what the VBA code in the companion download file does.

Note: Visio 2010 has expanded the API for exporting images from Visio. Among the new features is programmatic control over export resolution. See: New Visio 2010 API for Configuring Raster Export here or here.

When your run the sub ExportAllPages either via the VBA interface, or by clicking on the big button sitting at the bottom of Page-1, you’ll see a funny dialog that lets you choose your export image type: .bmp, .gif, .jpg, .png or .tif.

 

Export Dialog

Once you’ve selected an image type, you’ll be presented with the SaveAs dialog:

This is your big chance to choose your export settings. In the File name field, type in an image file name such as test.gif, test.bmp, test.png, etc. (but not test.vsd!) Just make sure the extension on your dummy file matches the extension you picked in the first dialog!

After you’ve done this, the code will take over and export every page in your document to the image type you’ve selected. The files will be in the same directory as the .vsd file, and will be prefixed with the number of the page: 001, 002, 003, etc.

For those who just want to get the loop going, here’s a simpler version of the export code, without all the dialogs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Public Sub ExportAllPages_ToGif()
 
    Dim formatExtension As String
    formatExtension = ".gif" '...or .bmp, .jpg, .png, .tif
 
    '// Init folder, doc and counter:
    Dim filename As String, folder As String
    folder = ThisDocument.Path
 
    Dim doc As Visio.Document
    Set doc = Visio.ActiveDocument
 
    Dim i As Integer
    i = 1
 
    '// Loop through pages:
    For Each pg In doc.Pages
 
        '// Setup the filename:
        filename = Format(i, "000") & " " & pg.Name
 
        '// Append '(bkgnd)' to background pages:
        If (pg.Background) Then filename = filename & " (bkgnd)"
 
        '// Add the extension:
        filename = filename & formatExtension
 
        '// Save it:
        Call pg.Export(folder & filename)
 
        i = i + 1
 
    Next
 
Cleanup:
    Set doc = Nothing
End Sub
Public Sub ExportAllPages_ToGif()

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

    '// Init folder, doc and counter:
    Dim filename As String, folder As String
    folder = ThisDocument.Path

    Dim doc As Visio.Document
    Set doc = Visio.ActiveDocument

    Dim i As Integer
    i = 1

    '// Loop through pages:
    For Each pg In doc.Pages

        '// Setup the filename:
        filename = Format(i, "000") & " " & pg.Name

        '// Append '(bkgnd)' to background pages:
        If (pg.Background) Then filename = filename & " (bkgnd)"

        '// Add the extension:
        filename = filename & formatExtension

        '// Save it:
        Call pg.Export(folder & filename)

        i = i + 1

    Next

Cleanup:
    Set doc = Nothing
End Sub

Download “ExportAllPages.zip”

s!Aj0wJuswNyXlhT00ZtzYvRxoaWsd – Downloaded 5240 times – 103.00 B
  • Tweet
  • More
  • Pocket
  • Share on Tumblr
  • Print
  • Email

No related posts.

Filed Under: Code Tagged With: Exporting Images, Visio Code Samples, Visio Pages

Previous Post: « Create Visio Flowcharts Programmatically
Next Post: Map of North America »

Reader Interactions

Comments

  1. Liam says

    October 24, 2007 at 7:56 pm

    This is great!

  2. Bobby Dazzler says

    December 14, 2007 at 3:48 pm

    Quality little bit of code. Spot on. Saved me 30 x “Save As…” boredom (plus future occasions of the same task). Cheers. Bobby.

  3. Diwakar says

    July 22, 2008 at 6:39 am

    Excellent Script!

  4. Paul Irish says

    January 8, 2009 at 4:31 pm

    Quick tip.
    Make sure this vsd file is saved to a real place on the harddrive, not a temp folder.
    Also, even better if its in the same folder as the other VSD you’re processing.

  5. DurzoFlint says

    February 14, 2009 at 1:16 pm

    How can I modify this to save a selected drawing to a specific path and as a png file.

    thanks

    Durzo

  6. Visio Guy says

    February 16, 2009 at 9:36 am

    Hi Durzo,

    Just change a few lines, for example:

    Dim formatExtension As String formatExtension = “.png”

    folder = “C:\DurzosExports”

    – Chris

  7. Rick Bond says

    April 9, 2009 at 10:51 pm

    Dumb question:

    When I click the “Export All Pages” button in “Export All Pages.vsd, it exports only the Export All Pages” image.

    How do I tell it to export all the pages in MY document?

    Thank you,

    Rick

  8. Visio Guy says

    April 10, 2009 at 11:23 am

    Hi Rick,

    Make the document you want to export the “active” document. Ie: give it focus and minimize the “exporter”.

    Then, go into the VBA project and look for ThisDocument.ExportAllPages. Place your cursor somewhere in this procedure, then press F5 to run it.

    The code gets the ActiveDocument and exports that. When you press the button, then the exporter document is always active, so if using the button, you’re kind of stuck.

    But when running from the code window, any document can be active, so this is more flexible.

  9. david says

    August 30, 2010 at 7:40 pm

    Visio Guy, you are the BOMB!!! Thanks for saving me time and pain. May Karma repay you. 😀

  10. Mana says

    September 11, 2010 at 5:22 pm

    Hello,

    Please tell me more detail about how can I create the button Export all in my new Visiso document and insert the VB code?

    I tried to copy the button in demo file (Export All Pages.vsd) to my Visio file, but nothing happen when clicking on it.

    Thank you.

  11. Visio Guy says

    September 13, 2010 at 2:09 pm

    Hi Mana,

    Just copying the button doesn’t copy all the code behind it. You probably just end up with another button that is linked to nothing.

    Here’s roughly how to hook a button to VBA code. After adding a button to a Visio page:

    1. Right click the button
    2. Choose CommandButton Object > View Code
    3. In the VBA editor that appears, add the code from this article either by referencing sub ExportAllPages, or copying the code and pasting it into the Button’s procedure.

  12. jabbett says

    September 13, 2010 at 3:34 pm

    Looks like Visio 2010 has more API hooks for configuring export formats:

    New Visio 2010 API for Configuring Raster Export

  13. Visio Guy says

    September 13, 2010 at 3:55 pm

    Thanks jabbett,

    Yup, Visio 2010 finally has added some control over export resolution!

  14. Mana says

    September 16, 2010 at 9:17 am

    Thank you Visio Guy!
    Some small issues:

    formatExtension = “.gif” ‘…or .bmp, .jpg, .png, .tif
    =>
    formatExtension = “.gif”
    ‘//…or .bmp, .jpg, .png, .tif

    Set doc = Visio.ActiveDocument i = 1
    =>
    Set doc = Visio.ActiveDocument
    i = 1

  15. Visio Guy says

    September 16, 2010 at 11:00 pm

    Hi Mana,

    Fixed the second typo, not sure what your first comment was about…those are just comments!

  16. Lantrix says

    February 8, 2011 at 2:52 am

    Mana’s first comment was in essence this:

    Change this:

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

    to this:

    1
    2
    3
    
    Dim formatExtension As String
    formatExtension = ".gif"
    '//…or .bmp, .jpg, .png, .tif
    Dim formatExtension As String
    formatExtension = ".gif"
    '//…or .bmp, .jpg, .png, .tif
  17. Rob Pearson says

    November 9, 2011 at 4:46 am

    Many thanks for this. Your comment from April 10 2009 was essential to understand how to get this to work, so that should probably be in the main body of the article – don’t overestimate our level of knowledge!

    Also the export falls over if you have any disallowed characters in your page names – I had colons and it took me a while to figure out that I needed to remove them.

    Thanks again 🙂

  18. Visio Guy says

    November 9, 2011 at 12:42 pm

    Thanks Rob,

    TODO: replace characters from page names that will be illegal in file names.

  19. Pam says

    February 17, 2012 at 8:11 pm

    This is definitly great. Have you tried publishing web pages by looping through the pages? Is this possible?
    thank you

  20. ro says

    March 27, 2012 at 5:19 pm

    Hello,

    I am running the code and it is only exporting 8 sheets (my document has over 60 sheets which is why this macro would be ÜBER helpful!!)

    any help?

    thx

  21. Visio Guy says

    April 12, 2012 at 11:12 am

    Hi RO,

    I wonder if the export is taking too long and VBA is tripping over itself?

    Maybe try adding a DoEvents call after pg.Export, or put a breakpoint there and keep F5-ing to it to see if you get further than 8 pages (ie: give the system time to catch up)

  22. Visio Guy says

    April 12, 2012 at 12:35 pm

    @Pam,

    Yes, you could loop through pages and publish as web, but Visio’s Save As Web Page functionality automatically creates a mini-web-site with all of the pages in a document for you!

  23. Ken says

    January 16, 2015 at 5:15 pm

    You sir, are the man! Your nifty bit of code saved me hours and hours of work, thank you so much.

    If you are ever in Washington, DC please shoot me an email so that I may buy you all the drinks.

    Eternal Gratitude,

    Ken

  24. Israel Anthony Lopez says

    March 16, 2015 at 3:24 pm

    Visio Guy,

    I am having an issue in Microsoft Visio 2013 and get an error at:

    Call pg.Export(folder & filename)

    The funny thing is that it imports about half the pages then stops on my 12 page Visio diagram.

  25. sputnik says

    February 7, 2016 at 1:34 pm

    Hi Visio Guy,

    thanks a lot from here as well, it is a crazy useful bit of code!

    I believe that since the new version of Visio has been around for quite some time now, you have no intention to enhance this snippet, but if you happen to have come across the topic of applying transparancy settings via the API, I would appreciate if you could point me to some docs.

    Unfortunately I am forced to stick to Visio 2007, and whereas the script itself if spot on for batch export, transparency setting is simply not applied, and by googling I could not find any solution to apply it via the standard API.

    Keep it up, BR,
    sputnik

  26. Visio Guy says

    February 24, 2016 at 9:20 am

    Hi ???????,

    Re: transpaerncy: be sure to check out:

    The Hidden World of Visio Shapes
    http://www.visguy.com/2006/09/05/the-hidden-world-of-visio-shapes/

  27. sputnik says

    February 24, 2016 at 10:37 am

    Hi,

    thanks again; it is yet another very useful addition to my superficial knowledge about Visio.

    However I missed one important aspect in my previous comment: my intention is to apply transparency for the page/background of each sheet (which I am doing manually during exporting to images by setting transparency for white), but the ShapeSheet of does not seem to have any option for that (opposed to shapes or images, where there are parameters for this). If you have any further hints, they would be still appreciated!

    Thanks, BR,
    sputnik

  28. MILO says

    October 23, 2017 at 2:25 pm

    Hi,

    Thanks very much for the excellent scripts.

    Is it possible to use the same script for generating separate pdf files? Seems like simply adding additional .pdf extension, along with other extensions, does not work.

    Thanks & BR

  29. Alex says

    January 31, 2018 at 11:10 am

    Hi,

    Wanted to say i used this excellent export code as as a macro in my own Visio template and changed the extension to a .dwg, to export for AutoCAD, using Visio 2016. Anyway wanted to say a massive thanks to for supplying this and provide my adaption back to the community.

    Alex

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    
    Public Sub ExportAllPages_ToDWG()
     
        Dim formatExtension As String
        formatExtension = ".dwg" '...or .bmp, .jpg, .png, .tif
     
        '// Init folder, doc and counter:
        Dim filename As String, folder As String
        folder = ThisDocument.Path
     
        Dim doc As Visio.Document
        Set doc = Visio.ActiveDocument
     
        Dim i As Integer
        i = 1
     
        '// Loop through pages:
        For Each pg In doc.Pages
     
            '// Setup the filename:
            filename = " x" & pg.Name
     
            '// Append '(bkgnd)' to background pages:
            If (pg.Background) Then filename = filename & " (bkgnd)"
     
            '// Add the extension:
            filename = filename & formatExtension
     
            '// Save it:
            Call pg.Export(folder & filename)
     
            i = i + 1
     
        Next
     
    Cleanup:
        Set doc = Nothing
    End Sub
    Public Sub ExportAllPages_ToDWG()
     
        Dim formatExtension As String
        formatExtension = ".dwg" '...or .bmp, .jpg, .png, .tif
     
        '// Init folder, doc and counter:
        Dim filename As String, folder As String
        folder = ThisDocument.Path
     
        Dim doc As Visio.Document
        Set doc = Visio.ActiveDocument
     
        Dim i As Integer
        i = 1
     
        '// Loop through pages:
        For Each pg In doc.Pages
     
            '// Setup the filename:
            filename = " x" & pg.Name
     
            '// Append '(bkgnd)' to background pages:
            If (pg.Background) Then filename = filename & " (bkgnd)"
     
            '// Add the extension:
            filename = filename & formatExtension
     
            '// Save it:
            Call pg.Export(folder & filename)
     
            i = i + 1
     
        Next
     
    Cleanup:
        Set doc = Nothing
    End Sub
  30. Brad says

    September 28, 2019 at 6:34 pm

    Thanks for your code, I was excited to try it in Visio 2016. Unfortunately, after I opened the VSD file, it looks like Visio created an auto-recover file, which Bitdefender 2020 then blocked —

    “The file C:\Users\brads\AppData\Roaming\Microsoft\Excel\~ar68A6.xar has been detected as infected. The threat has been successfully blocked, your device is safe”

  31. Visio Guy says

    September 30, 2019 at 9:34 pm

    Hi Brad,

    I don’t know much about Bitdefender. A few thoughts: first, the file is pretty old. Visio has some file block settings buried deep in the Trust Center stuff in the backstage area. But I think they mostly are concerned with Visio 5 and earlier, so that’s pre-2000. Also, the file has VBA code, so you’re up against macro settings inside of Visio, and Bitdefender probably has some settings against VBA macros in Office documents.

    Luckily, the code is posted in this article, so you don’t really need the download!

  32. Rob Weinstein says

    June 23, 2021 at 8:14 pm

    Hi Visio Guy,

    I just tried Export All Pages today with Visio 2016 and it worked perfectly. I had to export a 56-page Visio document to PNG files and it was a huge time saver!

    Thanks,

    -Rob

  33. Visio Guy says

    June 24, 2021 at 8:55 pm

    Thanks for the story, Rob, great to hear the blog is helping people!

  34. Joshua Moldover says

    December 27, 2021 at 10:08 pm

    Unfortunately Microsoft’s changes to the MSDN blog have removed the link shown above as a useful resource. However, it is still available via the Internet Archive:
    https://web.archive.org/web/20150701060506/http://blogs.msdn.com/b/visio/archive/2010/03/03/new-visio-2010-api-for-configuring-raster-export.aspx

  35. Visio Guy says

    January 3, 2022 at 4:00 pm

    Thanks JM! I found another Microsoft link as well, but the pictures are missing. I added your link and the other one up above in the article under “…here and here”.

    Happy New Year!

  36. Remko says

    May 5, 2022 at 4:26 pm

    How about PDF?

    I can get this working but i dont know how to create a loop:

    ActiveDocument.ExportAsFixedFormat visFixedFormatPDF, “C:\ExportedVisioDocument .pdf”, visDocExIntentPrint, visPrintAll

  37. Visio Guy says

    May 5, 2022 at 5:00 pm

    Hi Remko,

    For PDF export, you would be exporting all of the pages in a document, by default (I think), so you wouldn’t need a loop…

    …unless you want each page as a separate PDF?

    Then, something like this, using the “from” and “to” parameters, as outline at:
    https://docs.microsoft.com/en-us/office/vba/api/visio.document.exportasfixedformat

    1
    2
    3
    4
    5
    6
    
    Dim i As Integer
    Dim path As String
    path = "C:\ExportedVisioDocument.pdf"
    For i = 1 to visDoc.Pages.Count
      Call visDoc.ExportAsFixedFormat(visFixedFormatPDF, path, visDocExIntentPrint, visPrintFromTo, i, i)
    Next i
    Dim i As Integer
    Dim path As String
    path = "C:\ExportedVisioDocument.pdf"
    For i = 1 to visDoc.Pages.Count
      Call visDoc.ExportAsFixedFormat(visFixedFormatPDF, path, visDocExIntentPrint, visPrintFromTo, i, i)
    Next i

    If you wanted to loop through multiple documents that happen to be open in Visio, then:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    Dim doc As Visio.Document
    Dim path As String
    path = "C:\ExportedVisioDocument " '//...we will append an index to make different paths for each doc
    Dim i As Integer
    i = 1
    For Each doc in Visio.Application.Documents
      Call visDoc.ExportAsFixedFormat(visFixedFormatPDF, path & i & ".pdf", visDocExIntentPrint, visPrintAll)  
      i = i + 1 '//...different path for each document!
    Next doc
    Dim doc As Visio.Document
    Dim path As String
    path = "C:\ExportedVisioDocument " '//...we will append an index to make different paths for each doc
    Dim i As Integer
    i = 1
    For Each doc in Visio.Application.Documents
      Call visDoc.ExportAsFixedFormat(visFixedFormatPDF, path & i & ".pdf", visDocExIntentPrint, visPrintAll)  
      i = i + 1 '//...different path for each document!
    Next doc
  38. Remko says

    May 6, 2022 at 10:13 am

    Thanks Chris! i was indeed looking for separate PDF’s I did get a compile error, but i ended up stealing some other code somewhere to create this:

    /[code]
    Dim FldrName As String
    Dim PageName As String
    Dim FileName As String
    Dim pg As Page
    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, "/", "__")

    FileName = FldrName & PageName & ".pdf"
    If Not (pg.Background) Then
    Call ActiveDocument.ExportAsFixedFormat(visFixedFormatPDF, FileName, visDocExIntentPrint, visPrintFromTo, i, i)
    i = i + 1
    End If
    Next
    End If
    [code]

  39. CoffeeSidu says

    May 17, 2022 at 10:42 pm

    Thank you for writing this code.

    Im trying to convert into .dwg or .dxf (should be same code)

    but when i run, it is giving me a bug.
    can anyone help?
    thanks

    Call pg.Export(folder & filename)

    Run-time error ‘-2032465768 (86db0898)’

    Is the message i keep getting when trying to run this line.

    Cheers

  40. bigfrog says

    January 30, 2023 at 6:08 am

    Big thanks to Remko for sharing his formula above.

    I’ve been tinkering with it as I have a need to name each outputted file as a specific shape data reference?

    Can Anyone help with how you would get this happening?

Leave a Reply Cancel reply

Primary Sidebar

Buy Text on a Circle Shape
Article — Video — Purchase

Categories

Buy my book!

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Tag Cloud

A/V Artistic Effects BPM Code Connectors Control Handles Countries Custom Patterns Custom Properties Data Graphics Data Linking Data Visualization David Edson David Parker Fill Format Formulas Functions Geometry Gradient Images Links Maps Multi-shapes Network Programming repeating shapes Resources Right-Click Actions Scale Shape Data ShapeSheet ShapeSheet Formulas ShapeSheet Functions SharePoint shiny SmartShapes Sport Sports Text Themes Tools Transparency User-defined Cells Visio 2007 Visio SmartShapes

Top Posts & Pages

  • - Visio Shapes & Stencils
  • - Visio Templates & Drawings
  • Amazon AWS Visio Shapes
  • Sankey Diagram Shapes for Visio
  • Free Visio People Shapes
  • Bubble Revision Shapes
  • Visio Network Server Shape Icon Customization Tool
  • AV Engineering Diagrams with Symbol Logic ECAV
  • Release the Power of Visio Custom Line Patterns
  • Crayon Visio Network Shapes, Revisited

www.visguy.com - Visio Guy - since 2006

 

Loading Comments...