Passing variables by VBA between excel and visio in early binding

Posted on 2013-05-21
Last Modified: 2013-05-28
I control MS Office.Visio from Excel, using early binding, to manipulate Visio pages, shapes etc.  Thus far, the superior scope for RibbonX customisation in Excel has given me control over customised processes in Visio, that greatly exceeds the customisation provided by the visio ribbon.

However, I now need to control the occurrence of specific RibbonX custom tabs in Excel, by detecting change events triggered by changes in the current Visio.activePage.

VisioGuy (EE) provided me with code that detects change events in visio active page, when placed in the visio document code sheet:

Dim WithEvents m_visWins As Visio.Windows
Private Sub Document_RunModeEntered(ByVal Doc As IVDocument)

  Set m_visWins = Visio.Application.Windows

End Sub

Private Sub m_visWins_WindowTurnedToPage(ByVal visWin As IVWindow)

  If (visWin.Type = Visio.VisWinTypes.visDrawing) Then
    Debug.Print "WindowTurnedToPage: " & visWin.Page  '<<<<<<<<<<<
    Debug.Print "WindowTurnedToPage, but not a drawing window: " & visWin.Caption
  End If
End Sub

This code works well in visio but, perhaps not surprisingly, does not capture the Visio event when the code is placed the worksheet code page of an excel workbook that has been set to reference the visio object model (via VBE/Tools/References).

How do I monitor in excel,  visWin.Page (marked '<<<< above) as an object or as a string = object.Name

- can I use a public variable in visio, and make it detectable in excel (my attempts have failed so far)
- do I create a new class in visio, and attempt to detect visio page changes as a property of the visio object model?
- do I try to use the Application.Run Method inserted in code at the point marked '<<<< (above) and pass variables as arguments by position. I understand Application.Run arguments cannot be objects, but I could live with that, if it works for other variables).
- or something else?

- IF POSSIBLE! it would be helpful to have one generic method that can be used to pass variables in either direction between Excel and Visio, to transmit other variables in the future. Given that Visio macros are stored as documents, and hence called by ...ExecuteLine("myMacro"), would a bidirectional method be impossible?

Some pointers to start me off would be appreciated..

Question by:Kelvin4
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
LVL 11

Assisted Solution

jkpieterse earned 80 total points
ID: 39186644
I expect you should be able to create a class module in Excel VBA which uses the WithEvents construct using Visio as an application:

Public WithEvents VisioApp as Visio.Application

'Events go here

And in a normal sub when you have captured an object variable that holds the relevant instance of the Visio application (suppose we called the class clsVisio):

Dim mcVisio as clsVisio

Sun Init()
Dim oVisio as Visio.Application
'Somehow instantiate oVisio
set mcVisio = New clsVisio
Set mcVisio.VisioApp = oVisio
'Now events should be active
LVL 30

Accepted Solution

Scott Helmers earned 420 total points
ID: 39187328
I like the sound of jkpieterse's idea -- I'm not convinced it will work but please let us know if it does.

Ignoring event handling and just answering your question about communicating in both directions between an instance of Visio and an instance of Excel, please take a look at Use Excel's hidden data store to share data across VBA projects. Although the article is about Excel-to-Excel communication, I've used the technique between Excel and Visio as well. It does require that you run code in Visio, which may or may not be what you want.

Continuing to ignore event-driven ideas, you can also use the registry or environment variables as an intermediate data store...

Author Comment

ID: 39187419
Jkpieterse and Scott: thank you for complementary and rapid responses.

All my shape-level and some page-level coding is in Visio; Excel handles higher levels between visio file creation/records and visio page creation.

I will take no more than three days to work on your comments. I hope you can wait til then for a settlement of points.

Appreciately, Kelvin.
Independent Software Vendors: 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!

LVL 11

Expert Comment

ID: 39187961
Fine with me!!!

Author Comment

ID: 39193383
Thanks, again, for your suggestions.  yrs Kelvin...

Scott: Thank you for referencing the resources available for variable-sharing between visio and excel.
This will help me a lot. As you point out, it will not address the requirement for an Event component.

Jkpieterse: I extracted a section of my Excel code to try to follow your suggestion, alas without success.
However, I am a novice at class creation so I've attached my files for your inspection, and any comment you may wish.
I admit that the class creation process is shorter that in your original suggestion, but it simply mimics the
class set up in pageTurn Event in Visio.vsd. Looking at that first:

File: pageTurn Event in Visio.vsd:
is a straight visio-vased solution (from visioGuy) showing a Visio.Application.Windows event for page turning.
Change the page, and a msgbox report.
Now my adaptation to Excel....

File: visPageTurnEventInXL add WithEvents v2.3.xlsm
is an extract from my Project that links Excel to drive Visio:

In ThisWorkbook:
I enable the visio file "try to detect page turn in Excel.vsd" to be loaded from the file browser.
All you need to do is place the .vsd file in your chosen directory,
Open excel
Cancel browser
Open vbe
in excel modules\globals\ set Public Const options_EndUser_OpenFile_DefaultPath As String = "C:" to your directory loc of "try to detect page turn in Excel.vsd"
Save and reopen excel file
Use browser to open "try to detect page turn in Excel.vsd"

All the supporting code for the page turn event is in ThisWorkbook, becuase:
I had to run the browser from ThisWorkbook to enable
Public WithEvents m_visWins As Visio.Windows to be created when the visio file is opened.

However, as you can see at the foot of the code page, in the starred comments:
Set m_visWins = Visio.Windows threw an error.

Your comments have set me thinking, can you push it a tad further?
Thanks, Kelvin

Author Comment

ID: 39197336
Sunday 26 am.
I a solution to the EVENT component of my question, and will provide code shortly. In outline:

A visio event detects the turning of a new visio page
Application.Run passes the turned as argument from visio to a standard module in Excel.

The Excel module writes the turned visio to an excel sheet cell.

An excel event detects the excel sheet change and, if the changed cell does contain the turned, the event creates that name as a public excel variable - which is my aim.

So far, it works fine..

I would be very glad to know if the single event solution suggested by  jkpieterse can work. It would certainly help add to my tool-box of solutions.

Will propose a division of points on Tuesday am (European )


Author Closing Comment

ID: 39202065
jkpieterse's outline suggestion grappled with the EVENT part of my question, and helped to get me thinking. But I could not exploit his suggestion and added some early feedback (above) for possible comment.

Scott's comments addressed the sharing of string data between office apps via Hidden Name space, which he has used to share string variables between visio and excel. So far, I've seen Hidden Name space working between instances of Excel (using his demo files in his EE Tips and Tricks paper: 2009-07-06) .

Scott: I would be grateful for the visio code lines that enable visio to write to xL Hidden Name space. My brain has got a bit spaced out!

Thanks, Kelvin

Author Comment

ID: 39202101
Attached are the files I referred to above.
Place both in the same folder, open the excel file first and follow the instructions on sheet1
This sheet also summarises the purpose of the demo


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Periodically someone asks me whether there’s a way to automatically convert all of the pages in a Visio drawing to PowerPoint slides. There have even been a few times when I’ve wanted to do that myself but I never really had enough incentive to figu…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
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.

749 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