Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Run a visio macro from excel when the visio file is already open

Posted on 2009-04-18
5
Medium Priority
?
851 Views
Last Modified: 2012-05-06
A previous question asked how to run a visio macro from within excel.  The response code worked - but only if the Visio doucment was closed and required opening:
'Set objVisioDoc = objVisio.Documents.Open("C:\...\Visio file.vsd")
objVisioDoc.ExecuteLine "nameofmacroinVisio"    'change to the name of the macro within the Visio document that you want to run

How can I run this macro when the file is already open and I want it to remain open?

0
Comment
Question by:OliasOfBilli
  • 3
  • 2
5 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 24175119
Hello OliasOfBilli,

It would be helpful to see the whole code, but something like this is likely what you need:

On Error Resume Next
Set objVisioDoc = objVisio.Documents("Visio file.vsd")
If Err <> 0 Then
    Err.Clear
    Set objVisioDoc = objVisio.Documents.Open("C:\...\Visio file.vsd")
End If
On Error GoTo 0
objVisioDoc.ExecuteLine "nameofmacroinVisio"

Regards,

Patrick
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 24175125
OliasOfBilli,

On Error Resume Next tells VBA to simply move on to the next line if it has a runtime error.  If the Visio file
is already open, then the first "Set objVisioDoc" statement will set the object variable to be that file.  If it is
not open, there will be an error, and thus the Err.Number property will be something other than zero.  In that
case, clear the error, and open the file.  On Error GoTo 0 simply turns off the error handling.

Regards,

Patrick
0
 

Author Comment

by:OliasOfBilli
ID: 24175227
Thanks, Patrick.  Unfortunately, as I run through the code, the code opens a new Visio window. It does not recognise the already opened Visio document which contains the   "RunFromExcel" macro.
As requested - I have attached the fuill code in the Excel doc.


Sub RunningVisioMacroFromWithinExcel()
    Dim objVisio As Object
    Set objVisio = CreateObject("Visio.Application", "")
    Dim objVisioDoc As Object
    
    On Error Resume Next
    Set objVisioDoc = objVisio.Documents("ConversionArrowv2.vsd")
        If Err <> 0 Then
            Err.Clear 'If not open, Err.Number NE zero and clear the error,open the file.
            Set objVisioDoc = objVisio.Documents.Open("C:\Documents and Settings\Tim\Desktop\ConversionArrowv2.vsd")
        End If
    On Error GoTo 0 'Turn off the error handling.
        objVisioDoc.ExecuteLine "RunFromExcel"
    
    Set objVisioDoc = Nothing
    Set objVisio = Nothing
End Sub

Open in new window

0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 200 total points
ID: 24175280
OK, the first thing you have to do is look for an open instance of Visio...



Sub RunningVisioMacroFromWithinExcel()
    Dim objVisio As Object
    Dim objVisioDoc As Object

    On Error Resume Next
    Set objVisio = GetObject(, "Visio.Application") 'returns Visio app if it is running, error if not
    If Err <> 0 Then
        Err.Clear
        'start Visio because it wasn't running before
        Set objVisio = CreateObject("Visio.Application", "")
    End If
    Set objVisioDoc = objVisio.Documents("ConversionArrowv2.vsd")
    If Err <> 0 Then
        Err.Clear 'If not open, Err.Number NE zero and clear the error,open the file.
        Set objVisioDoc = objVisio.Documents.Open("C:\Documents and Settings\Tim\Desktop\ConversionArrowv2.vsd")
    End If
    On Error GoTo 0 'Turn off the error handling.
    objVisioDoc.ExecuteLine "RunFromExcel"
   
    Set objVisioDoc = Nothing
    Set objVisio = Nothing
End Sub
0
 

Author Closing Comment

by:OliasOfBilli
ID: 31571767
Patrick, your help was very much appreciated (and very speedy).  Thnaks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question