Passing variables by VBA between excel and visio in early binding

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..

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jan Karel PieterseExcel and VBA ExpertCommented:
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
Scott HelmersVisio Consultant, Trainer, Author, and DeveloperCommented:
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...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kelvin4Author Commented:
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.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Jan Karel PieterseExcel and VBA ExpertCommented:
Fine with me!!!
Kelvin4Author Commented:
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
Kelvin4Author Commented:
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 )

Kelvin4Author Commented:
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
Kelvin4Author Commented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Visio

From novice to tech pro — start learning today.