Embedding Visio in .net based Windows Forms Application

N

Naga

Hello,
I am using Visual studio.net to programmitically control Visio 2003.
I read some of the MSDN articles about this topic and trying to develop
some small sample programs.

I got some sample program devloped in VB 6.0 that just opens Visio
document with basic diagrams template with Basic shapes and then adds
Rectangle shape to the drawing and then sets the text to "HELLO WORLD".

so The VB code to achecive this task is as follows.

Sub HelloWorld ()
'Instance of Visio
Dim appVisio As Visio.Application
'Documents collection of instance
Dim docsObj As Visio.Documents
'Document to work in
Dim docObj As Visio.Document
'Stencil that contains master
Dim stnObj As Visio.Document
'Master to drop
Dim mastObj As Visio.Master
'Pages collection of document
Dim pagsObj As Visio.Pages
'Page to work in
Dim pagObj As Visio.Page
'Instance of master on page
Dim shpObj As Visio.Shape

'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;the program 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("Basic Diagram.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("Basic Shapes.vss")
Set mastObj = stnObj.Masters("Rectangle")
'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 = "Hello World!"
'Save the drawing and quit Visio. The message pauses the program
'so you can see the Visio drawing before the instance closes.
docObj.SaveAs "hello.vsd"
MsgBox "Drawing finished!", , "Hello World!"
appVisio.Quit
End Sub

So I used the above code to generate #C.net code as shown below by
adding VISIO ActiveX control to my Windows Form.

private Visio.Application VisApp = null;
private Visio.Page VisPage = null; private
Visio.Document VisDocument = null;
private Visio.Documents VisDocuments = null;
private Visio.Window VisWindow = null;
private Visio.Document VisStencil = null;
private Visio.Pages VisPages = null;
private Visio.Master VisMaster = null;
private Visio.Shape VisShape = null;
VisApp = (Visio.Application)axDrawingControl1.Window.Application;
VisPage = (Visio.Page) axDrawingControl1.Document.Pages[1];
VisWindow = (Visio.Window) axDrawingControl1.Window;
VisDocument = (Visio.Document) axDrawingControl1.Document;
VisDocuments =
(Visio.Documents)
axDrawingControl1.Window.Application.Documents;

VisDocument = VisDocuments.Add("Basic Diagram.vst");
VisPages = (Visio.Pages)axDrawingControl1.Document.Pages;
VisStencil = VisApp.Documents("Basic shapes.vss");
VisMaster = VisStencil.Masters("Rectangle");
VisShape = VisPage.Drop(VisMaster,4.25,5.5);
VisShape.Text = "HELLO WORLD";


If I execute the above #C code I am getting a compile errors for the
following two statements

VisStencil = VisApp.Documents("Basic shapes.vss");
VisMaster = VisStencil.Masters("Rectangle");


Does anybody know How to fix this problem? I will appreciate your help.

Thanks,
Naga.
 
A

Al Edlund

well, I don't write in c# so I guess if the opening the stencil failed which
would lead to the master call failing because it never opened. There's some
good examples of c# coding in the v2003 sdk, including the only one that
really goes in depth on using the drawing control.

The other thing to start watching is that in vb6 most indexes start at 1, in
..net they start at 0....

al

Naga said:
Hello,
I am using Visual studio.net to programmitically control Visio 2003.
I read some of the MSDN articles about this topic and trying to develop
some small sample programs.

I got some sample program devloped in VB 6.0 that just opens Visio
document with basic diagrams template with Basic shapes and then adds
Rectangle shape to the drawing and then sets the text to "HELLO WORLD".

so The VB code to achecive this task is as follows.

Sub HelloWorld ()
'Instance of Visio
Dim appVisio As Visio.Application
'Documents collection of instance
Dim docsObj As Visio.Documents
'Document to work in
Dim docObj As Visio.Document
'Stencil that contains master
Dim stnObj As Visio.Document
'Master to drop
Dim mastObj As Visio.Master
'Pages collection of document
Dim pagsObj As Visio.Pages
'Page to work in
Dim pagObj As Visio.Page
'Instance of master on page
Dim shpObj As Visio.Shape

'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;the program 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("Basic Diagram.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("Basic Shapes.vss")
Set mastObj = stnObj.Masters("Rectangle")
'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 = "Hello World!"
'Save the drawing and quit Visio. The message pauses the program
'so you can see the Visio drawing before the instance closes.
docObj.SaveAs "hello.vsd"
MsgBox "Drawing finished!", , "Hello World!"
appVisio.Quit
End Sub

So I used the above code to generate #C.net code as shown below by
adding VISIO ActiveX control to my Windows Form.

private Visio.Application VisApp = null;
private Visio.Page VisPage = null; private
Visio.Document VisDocument = null;
private Visio.Documents VisDocuments = null;
private Visio.Window VisWindow = null;
private Visio.Document VisStencil = null;
private Visio.Pages VisPages = null;
private Visio.Master VisMaster = null;
private Visio.Shape VisShape = null;
VisApp = (Visio.Application)axDrawingControl1.Window.Application;
VisPage = (Visio.Page) axDrawingControl1.Document.Pages[1];
VisWindow = (Visio.Window) axDrawingControl1.Window;
VisDocument = (Visio.Document) axDrawingControl1.Document;
VisDocuments =
(Visio.Documents)
axDrawingControl1.Window.Application.Documents;

VisDocument = VisDocuments.Add("Basic Diagram.vst");
VisPages = (Visio.Pages)axDrawingControl1.Document.Pages;
VisStencil = VisApp.Documents("Basic shapes.vss");
VisMaster = VisStencil.Masters("Rectangle");
VisShape = VisPage.Drop(VisMaster,4.25,5.5);
VisShape.Text = "HELLO WORLD";


If I execute the above #C code I am getting a compile errors for the
following two statements

VisStencil = VisApp.Documents("Basic shapes.vss");
VisMaster = VisStencil.Masters("Rectangle");


Does anybody know How to fix this problem? I will appreciate your help.

Thanks,
Naga.
 
M

Michel LAPLANE

Hi,

Change to this

VisStencil = VisApp.Documents.Add("Basic shapes.vss");
VisMaster = VisStencil.Masters.Add();
VisMaster.Name = "Rectangle";
VisMaster = VisStencil.Masters[("Rectangle")];
VisShape = VisApp.ActivePage.Drop(VisMaster, 4, 4);
VisShape.Text = "HELLO WORLD";

You know document is a collection so you must use Add.
Stencil have also a collection of masters so use add.
For retrieving a master you must get it out of the master collection.

Naga said:
Hello,
I am using Visual studio.net to programmitically control Visio 2003.
I read some of the MSDN articles about this topic and trying to develop
some small sample programs.

I got some sample program devloped in VB 6.0 that just opens Visio
document with basic diagrams template with Basic shapes and then adds
Rectangle shape to the drawing and then sets the text to "HELLO WORLD".

so The VB code to achecive this task is as follows.

Sub HelloWorld ()
'Instance of Visio
Dim appVisio As Visio.Application
'Documents collection of instance
Dim docsObj As Visio.Documents
'Document to work in
Dim docObj As Visio.Document
'Stencil that contains master
Dim stnObj As Visio.Document
'Master to drop
Dim mastObj As Visio.Master
'Pages collection of document
Dim pagsObj As Visio.Pages
'Page to work in
Dim pagObj As Visio.Page
'Instance of master on page
Dim shpObj As Visio.Shape

'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;the program 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("Basic Diagram.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("Basic Shapes.vss")
Set mastObj = stnObj.Masters("Rectangle")
'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 = "Hello World!"
'Save the drawing and quit Visio. The message pauses the program
'so you can see the Visio drawing before the instance closes.
docObj.SaveAs "hello.vsd"
MsgBox "Drawing finished!", , "Hello World!"
appVisio.Quit
End Sub

So I used the above code to generate #C.net code as shown below by
adding VISIO ActiveX control to my Windows Form.

private Visio.Application VisApp = null;
private Visio.Page VisPage = null; private
Visio.Document VisDocument = null;
private Visio.Documents VisDocuments = null;
private Visio.Window VisWindow = null;
private Visio.Document VisStencil = null;
private Visio.Pages VisPages = null;
private Visio.Master VisMaster = null;
private Visio.Shape VisShape = null;
VisApp = (Visio.Application)axDrawingControl1.Window.Application;
VisPage = (Visio.Page) axDrawingControl1.Document.Pages[1];
VisWindow = (Visio.Window) axDrawingControl1.Window;
VisDocument = (Visio.Document) axDrawingControl1.Document;
VisDocuments =
(Visio.Documents)
axDrawingControl1.Window.Application.Documents;

VisDocument = VisDocuments.Add("Basic Diagram.vst");
VisPages = (Visio.Pages)axDrawingControl1.Document.Pages;
VisStencil = VisApp.Documents("Basic shapes.vss");
VisMaster = VisStencil.Masters("Rectangle");
VisShape = VisPage.Drop(VisMaster,4.25,5.5);
VisShape.Text = "HELLO WORLD";


If I execute the above #C code I am getting a compile errors for the
following two statements

VisStencil = VisApp.Documents("Basic shapes.vss");
VisMaster = VisStencil.Masters("Rectangle");


Does anybody know How to fix this problem? I will appreciate your help.

Thanks,
Naga.
 

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