VBA/Visio Automation/Run-Time Error

S

stephenoye

Hi,

Hoping someon can help me out here.

I have made a some customized shapes and a customized stencil which I would
like to use in my VBA program. However, the program only runs as far as the
"shapes" line and throws out a run-time error.

It works alright if I use the generic blocks but I need to use my own.
Can anyone help.

I've gone back to the roots and copied an example that I know works just to
make sure I have no typos.
The only bits I have changed are the stencil and the the shapes ie .vst &
..vss.

The program is happy with the stencil but not the .vss.


Any help will be appreciated.


Program below:



Sub AutoVisio()


Dim AppVisio As Visio.Application ' Declare an Instance of Visio.
Dim docsObj As Visio.Documents ' Documents collection of instance.
Dim DocObj As Visio.Document ' Document to work in.
Dim stnObj As Visio.Document ' Stencil that contains master.
Dim mastObj As Visio.Master ' Master to drop.
Dim pagsObj As Visio.Pages ' Pages collection of document.
Dim pagObj As Visio.Page ' Page to work in.
Dim shpObj As Visio.Shape ' Instance of master on page.

' Create an instance of Visio and create a document based on the
' Basic Diagram template. It doesn't matter if an instance of
' Visio is already running, CreateObject will run a new one.
Set AppVisio = CreateObject("visio.application")

Set docsObj = AppVisio.Documents

' Create a document based on the Basic Diagram template that
' automatically opens the Basic Shapes stencil.
Set DocObj = docsObj.Add("Trend.vst")

Set pagsObj = AppVisio.ActiveDocument.Pages

' A new document always has at least one page, whose index in the
' Pages collection is 1.
Set pagObj = pagsObj.Item(1)

Set stnObj = AppVisio.Documents("Trend.vss")
Set mastObj = stnObj.Masters("delay")

' Drop the rectangle in the approximate middle of the page.
' Coordinates passed with the Drop method are always inches.
Set shpObj = pagObj.Drop(mastObj, 4.25, 5.5)

' Set the text of the rectangle.
shpObj.Text = "This is some text."

' Save the drawing and quit Visio. The message pauses the program
' so you can see the Visio drawing before the instance closes.
DocObj.SaveAs "MyDrawing.vsd"
MsgBox "Drawing finished!", , "AutoVisio (OLE) Example"

' Quit Visio.
AppVisio.Quit

' Clear the variable from memory.
Set AppVisio = Nothing

End Sub
 
A

AlEdlund

And where in your code do you load the "trend.vss" stencil, or test to see
that it is loaded before attemptiong to assign it to stnObj?
al
 
P

Paul Herber

You've got a few problems with your code.
1. the Visio application already exists
2. you access and add to the current documents incorrectly
3. I think you need paths to the template and stencil
4. correct way to open a stencil
5. you can't close Visio while the macro is running


Sub AutoVisio()


Dim AppVisio As Visio.Application ' Declare an Instance of Visio.
Dim docsObj As Visio.Documents ' Documents collection of
instance.
Dim DocObj As Visio.Document ' Document to work in.
Dim stnObj As Visio.Document ' Stencil that contains master.
Dim mastObj As Visio.Master ' Master to drop.
Dim pagsObj As Visio.Pages ' Pages collection of document.
Dim pagObj As Visio.Page ' Page to work in.
Dim shpObj As Visio.Shape ' Instance of master on page.

' Create an instance of Visio and create a document based on the
' Basic Diagram template. It doesn't matter if an instance of
' Visio is already running, CreateObject will run a new one.
' Set AppVisio = CreateObject("visio.application")

Set docsObj = ThisDocument.Application.Documents

' Create a document based on the Basic Diagram template that
' automatically opens the Basic Shapes stencil.
Set DocObj = docsObj.Add("c:\Trend.vst")

Set pagsObj = DocObj.Pages

' A new document always has at least one page, whose index in the
' Pages collection is 1.
Set pagObj = pagsObj.Item(1)

Set stnObj = docsObj.OpenEx("c:\Trend.vss", visOpenDocked)
Set mastObj = stnObj.Masters("delay")

' Drop the rectangle in the approximate middle of the page.
' Coordinates passed with the Drop method are always inches.
Set shpObj = pagObj.Drop(mastObj, 4.25, 5.5)

' Set the text of the rectangle.
shpObj.Text = "This is some text."

' Save the drawing and quit Visio. The message pauses the program
' so you can see the Visio drawing before the instance closes.
DocObj.SaveAs "MyDrawing.vsd"
MsgBox "Drawing finished!", , "AutoVisio (OLE) Example"

End Sub
 
S

stephenoye

I have actually copied "Trend.vss" into the 1033 directory of Visio where it
stores all its stencils.

I wasn't aware that I needed to load it. I wouldn't know how. Hence, my
request for assistance.

Thanks
 
J

John... Visio MVP

The macro recorder is your best friend. It does not produce elegant code,
but it will give you a starting point.

I did a quick macro record to record what happens when I open a stencil in
the UI and drop a shape from the stencil. This is what I got:
Application.Documents.OpenEx
"C:\Users\John\Documents\Visio\IKEAfurniture.vss", visOpenRO + visOpenDocked
Application.Windows.ItemEx("Drawing1").Activate
Application.ActiveWindow.Page.Drop
Application.Documents.Item("C:\Users\John\Documents\Visio\IKEAfurniture.vss").Masters.ItemU("Bonde
sideboard"), 2.165354, 5.990404

John... Visio MVP
 

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