Opening a second visio file in early binding with Excel, to copy shapes between visio files

I use Excel vba procedures to open and manipulate visio drawings:
To selecting a file in a dialogue box:

visFilePath = Application.GetSaveAsFilename(options_EndUser_OpenFile_DefaultPath, fileFilterString) 'default defined by Const
    If visFilePath <> "False" Then
        exists = FileFolderExists(visFilePath)
        dialgBxVisFileChosen = True
        dialgBxVisFileChosen = False
        Exit Sub
    End If

and then early binding:

If Not IsFileOpen(visFilePath) Then
       Set visioApp = New Visio.Application 'early binding
       Set visQDoc = visioApp.Documents.Open(visFilePath)
       Set pgs = visQDoc.Pages
end if

Now I want to open a second visio file so I can copy visio shapes from one file to the other.

I'm trying to work out a simple scheme, and worry that I will run into gotchas..
I propose to make two versions of all the variables above, distinguishing a don_vis file (copy domor) and rec_vis file (copy recipient): extending for example:

don_visFilePath & rec_visFilePath
don_visioApp    & rec_visioApp   etc etc

The inter file copying code would be focused on don_shapes and rec_shapes as members of  don_pages and rec_pages collections.

I can send event signals between excel and visio, in either direction, and have copied shapes within single visio files.

What worries me about my simple approach is, inexperience!  Where is this simple approach likely to cause me problems?

Thanks, Kelvin4
Who is Participating?
Visio_GuyConnect With a Mentor Commented:
I think it helps to break things up into as many small functions/subs as possible. Also, I personally use "from" and "to" in my variable names for stuff like this. Maybe:

'// Here's the main routine. It takes a 'from' page and a 'to' page as arguments.
Public Sub CopyShapes( ByRef visPgFrom As Visio.Page, _
                       ByRef visPgTo As Visio.Page )

  Dim shp As Visio.Shape
  For Each shp in visPgFrom.Shapes
    Call m_copyShape( shp, visPgTo )
  Next shp

  '// Cleanup:
  Set shp = Nothing

End Sub

Private Sub m_copyShape( ByRef visShpFrom As Visio.Shape, _
                         ByRef visPgTo As Visio.Page  )

  '// Location of 'from' shape on 'from' page:
  Dim px As Double, py As Double
  px = visShpFrom.CellsU("PinX").ResultIU
  py = visShpFrom.CellsU("PinY").ResultIU

  '// Drop 'from' shape on 'to' page:
  Dim shp As Visio.Shape
  Set shp = visPgTo.Drop( visShpFrom, px, py )

  '// Set the date and time this happened in the shape's
  '// as an example of doing something interesting in the
  '// 'to' page:
  shp.Text = Format(Now(), "")

  '// If this was a function instead of a Sub, we could return
  '// the shape object:
  '// Set m_copyShape = shp

  '// Cleanup:
  Set shp = Nothing

End Sub

Open in new window

Scott HelmersConnect With a Mentor Visio Consultant, Trainer, Author, and DeveloperCommented:
What you're describing is pretty straightforward as far as I can tell. The only thing that occurs to me is to be careful that you don't refer to ActiveDocument or any other "active" object. You probably don't do this anyway because your code is running in Excel not Visio, but I thought I'd mention it.

Other than that, Just be sure you keep your donor and receiver variables straight (especially each time you copy and paste a chunk of code to reuse it), and you should be fine.
Kelvin4Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.