VBA Error When Trying to Merge Visio Documents

beartooth91

Board Regular
Joined
Dec 15, 2024
Messages
76
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hello

I'm trying to merge several Visio documents and am getting the "Object doesn't support this property or method" on the line -

VBA Code:
.Pages(pageCount).Copy

I'm decent with Excel vba but Visio vba seems to be a little different animal. I've tried various flavors of code for this and I'm getting the same error. Sounds like I don't understand visio objects and methods.

Entire code as follows:
Thanks for any help....
VBA Code:
Sub MergeVisioDocuments()

    Dim visApp As Visio.Application
    Dim sourceDocs As Variant
    Dim targetDoc As Visio.Document
    Dim pageCount As Integer
    Dim i As Integer

    ' Set the Visio application object
    Set visApp = CreateObject("Visio.Application")
    
    ' Get the list of source Visio documents to merge (modify as needed)
    sourceDocs = Array("C:\scrap\Natrium\Working\NSS\Logic\26396-000-J3-NSS-C0001 Rev. 00A_Native DRAIN VESSEL IMMERSION HEATERS.vsdx", _
    "C:\scrap\Natrium\Working\NSS\Logic\26396-000-J3-NSS-C0100 Rev. 00A_Native TRAIN A SHX 2102A 2101A PAIR.vsdx", _
    "C:\scrap\Natrium\Working\NSS\Logic\26396-000-J3-NSS-C0101 Rev. 00A_Native TRAIN A SHX 2202A 2201A PAIR.vsdx")

    ' Create a new blank Visio document to be the target for merging
    Set targetDoc = visApp.Documents.Add("")

    ' Loop through each source document
    For i = LBound(sourceDocs) To UBound(sourceDocs)
        ' Open the source document
        With visApp.Documents.Open(sourceDocs(i))
            ' Loop through each page in the source document
            For pageCount = 1 To .Pages.Count
                ' Copy the current page to the target document
                .Pages(pageCount).Copy
                With targetDoc.Pages.Add
                    .Paste
                End With
            Next pageCount
            ' Close the source document without saving changes
            .Close 'False
        End With
    Next i

    ' Save the merged document (modify as needed)
    With targetDoc
        .SaveAs "C:\scrap\Natrium\Working\NSS\CombinedVisioFile.vsdx"
    End With

    ' Quit Visio application
    visApp.Quit
    Set visApp = Nothing

End Sub
 
I've never written Visio vba but after reading the documentation on Visio I'd say it's because you're trying to count the pages by referring to the Open method. Maybe
VBA Code:
With visApp.Documents
   .Open(sourceDocs(i))
'then the count would apply to the Documents collection
    For pageCount = 1 To .Pages.Count
 
Upvote 0
I've never written Visio vba but after reading the documentation on Visio I'd say it's because you're trying to count the pages by referring to the Open method. Maybe
VBA Code:
With visApp.Documents
   .Open(sourceDocs(i))
'then the count would apply to the Documents collection
    For pageCount = 1 To .Pages.Count
Maybe you fond the same thing?

The visio vba reference documentation does not list the .Copy method for either the Page or Pages objects......
 
Upvote 0
True, but your post is not about the Copy method, no? In Visio, I think that copy would be akin to its .Duplicate method.
 
Upvote 0
True, but your post is not about the Copy method, no? In Visio, I think that copy would be akin to its .Duplicate method.
I was trying to merge separate Vision files into one Vision file.
 
Upvote 0

Forum statistics

Threads
1,226,831
Messages
6,193,206
Members
453,779
Latest member
C_Rules

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top