Solved

Passing variables by VBA between excel and visio in early binding

Posted on 2013-05-21
8
1,624 Views
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  '<<<<<<<<<<<
  Else
    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..

Thanks!
0
Comment
Question by:Kelvin4
  • 5
  • 2
8 Comments
 
LVL 11

Assisted Solution

by:jkpieterse
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
0
 
LVL 30

Accepted Solution

by:
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...
0
 

Author Comment

by:Kelvin4
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.
0
 
LVL 11

Expert Comment

by:jkpieterse
ID: 39187961
Fine with me!!!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Kelvin4
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
visPageTurnEventInXL-add-WithEve.xlsm
pageTurn-Event-in-Visio.vsd
try-to-detect-page-turn-in-Excel.vsd
0
 

Author Comment

by:Kelvin4
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 )

Kelvin
0
 

Author Closing Comment

by:Kelvin4
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
0
 

Author Comment

by:Kelvin4
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

Kelvin
TestGetxlObjectv3.vsd
VisAuthDev-Current.xlsm
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now