importing a .bas file

K

Kevin

We have a perl scripts that creates a .bas file with several macros in it to
create a drawing of the environment.
I would like to have a macro that can be run as a scheduled task to import
this data, run the macros to draw the picture, and save itself as a webpage.

I have the Sub Document_DocumentOpened macro that will run the macros once
the .bas has been imported into visio, but I'm not sure how to go about
automatically importing a .bas file???
Can you have a macro to do tasks in VBA?
 
N

Nikolay Belyh

Kevin:
We have a perl scripts that creates a .bas file with several macros in it to
create a drawing of the environment.
I would like to have a macro that can be run as a scheduled task to import
this data, run the macros to draw the picture, and save itself as a webpage.

I have the Sub Document_DocumentOpened macro that will run the macros once
the .bas has been imported into visio, but I'm not sure how to go about
automatically importing a .bas file???
Can you have a macro to do tasks in VBA?

Maybe you can should consider scripting (VBS) instead?
I.e. you might want to rename your .bas file to .vbs file, and go with
cscript.
You even won't even need Document_Opened, or even a document in this
case.

---- doit.vbs ----

Set app = CreateObject("Visio.Application")

' here goes the content of your .bas file, instead of "Application"
use "app"

----

To run this file just use:
cscript.exe doit.vbs

If the above approach is not suitable for you for some reason, then
you can add .bas file using VBProject automation. You need to check
"Trust access to VBA project", and add reference to "Microsoft Visual
Basic for Applications Extensibility" for this to work.

Private Sub Document_DocumentOpened(ByVal doc As IVDocument)

doc.VBProject.VBComponents.Add(vbext_ct_StdModule).CodeModule.AddFromFile
("yourfile.bas")
End Sub
 
C

Chris Roth [MVP]

Hi Kevin,

Well first, you need to allow access to the VBA Project object model. In
Visio 2007, this is found under:

Tools > Security Center
Macro Settings Tab
Check "Trust access to the VBA project object model"

Once this is working, you need to add a reference to your VBA project to get
it tow work: "Microsoft Visual Basic for Applications Extensibility..." Now
you can use VBA to import the code modules.

I've dug up this module from my archives, maybe it will help you. This is
VBA code that imports .bas files into another document's VBA project:

Sub ImportModules()

Dim doc As Visio.Document
Set doc = Visio.ActiveDocument

Dim v As VBIDE.VBProject
Dim c As VBIDE.VBComponent

m_codeDirectory = ThisDocument.Path & CodeSubDir

Set v = doc.VBProject

'// Delete all the existing project blocks. Note: there is always
'// 1 module - ThisDocument, and it can't be deleted, evidently.
Do While v.VBComponents.Count > 1
Call v.VBComponents.Remove(v.VBComponents.Item(2))
Loop

Call v.VBComponents.Import(m_codeDirectory & VbaGlobals)
Call v.VBComponents.Import(m_codeDirectory & VbaConnectDbForm)

'// m_codeDirectory is a string that holds a folder with code modules in
it...
Call v.VBComponents.Item(1).CodeModule.AddFromFile(m_codeDirectory &
VbaThisDoc$)

End Sub

By the way, I've written more about VBA Macro Securty here:
http://www.visguy.com/2007/05/17/vba-macro-security/

--
Hope this helps,

Chris Roth
Visio MVP

Free Visio shapes:
http://www.visguy.com/category/shapes
Visio programming info:
http://www.visguy.com/category/programming/
Other Visio resources:
http://www.visguy.com/visio-links/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top