Passing variables by VBA between excel and visio in early binding

Posted on 2013-05-21
Medium Priority
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
  • 5
  • 2
LVL 11

Assisted Solution

jkpieterse earned 240 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 31

Accepted Solution

Scott Helmers earned 1260 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.
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

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 page.name as argument from visio to a standard module in Excel.

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

An excel event detects the excel sheet change and, if the changed cell does contain the turned page.name, 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

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 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