How to Control Visio(TM) Through OLE Automation

ID: Q147311


The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, versions 3.0, 3.0b, 6.0


SUMMARY

Visio(TM) is a registered trademark of Visio Corporation. Visio Corporation is one of the earliest users of the OLE specifications to enable its products to provide OLE Automation services.

This article provides a Visual FoxPro example that shows how to use OLE Automation to interact with other non-Microsoft applications.

Much of the syntax used in the command examples is derived from the documentation of Microsoft Visual Basic, Applications Edition provided with Microsoft Excel 7.0, and the browser tool of Microsoft Excel.

Many of the commands are specific to Visio, and are derived from Visio documentation and Help file topics.

NOTE: Some products mentioned in this article are manufactured by vendors independent of Microsoft; we make no warranty, implied or otherwise, regarding these products' performance or reliability.


MORE INFORMATION

The nucleus of the example shown in this article was provided by Andrew Ross MacNeil - Microsoft Most Valued Professional (MVP).


***********************************************************
** Visio.prg                                             **
**                                                       **
** Demonstration of OLE Automation with Visual FoxPro    **
** as client, Visio as server                            **
***********************************************************

CLEAR
CLEAR ALL

=MESSAGEBOX("Scale the Visual FoxPro desktop to occupy just the"+chr(13)+ ;
            "      the top 30 - 40% of the screen. Then, "+chr(13)+ ;
            "when Visio opens, size it and locate it at the bottom")

PUBLIC AppVisio, DocsVisio, AppPage,DocPage, MasterDoc, MasterObject, ;
     RectObject, WindowObject

WAIT WINDOW "Creating the object named appVisio"
AppVisio = CREATEOBJECT("Visio.application")

** You can use similar code by, instead of saying
* AppVisio = CREATEOBJECT("Visio.Application")
** say
* AppVisio = _SCREEN.activeform.visiocontrol.application

WAIT WINDOW "Creating a document"
DocsVisio = AppVisio.Documents   && A reference to a new visio document

WAIT WINDOW "Adding the basic template"
VisioDoc = DocsVisio.Add ("basic.vst")  && The basic template

WAIT WINDOW "Next, to the left you will see "+chr(13)+ ;
            "two references to the same page."+chr(13)+chr(13)+ ;
            "OLE objects have properties."+chr(13)+ ;
            "One of them is 'Name'."
AppPage = AppVisio.ActivePage
DocPage = VisioDoc.Pages(1)
? "AppPage.Name = "+AppPage.Name
? "DOCPAGE.Name = "+DOCPAGE.Name

WAIT WINDOW "Adding a name to the document." + chr(13) + ;
            "Watch the left end of the title bar."
DocPage.Name = "Demonstration"

** Now Pages Contain SHAPES.
** SHAPES can either be drawn outright using
** the DrawLine, DrawOval and
** DrawRectangle Methods

WAIT WINDOW "Drawing a line"
DocPage.DrawLine(1,9,3,10) && 1" from Left,  9" from bottom
                           && 3" from Left, 10" from bottom

** Or, they can be based on Master Shapes.
**
** Because the example opened up the "basic" vst (template)
**   you know there is a document
**   called Basic.vss (or stencil)

MasterDoc = AppVisio.Documents("basic.vss")  && Reference to the stencil

**
** Now you can pull out a Master Object

MasterObject = MasterDoc.Masters("Rectangle")

**
** This drops the object onto the document page
WAIT WINDOW "Unseen, the program accessed a rectangle tool" +chr(13)+ ;
            chr(13)+"Now, we drop it on the document page"
RectObject = DocPage.Drop (MasterObject, 4,9)  && Centered 4" from left
                                               && 9" from bottom

**
** You can also edit the text for an object

RectObject.Text = "Adding Diagram"  && Located in the
                                    && center of the rectangle

**
WAIT WINDOW "This draws an oval but it doesn't give us a reference"

docpage.DrawOval(5,3,4,5) && 5" from left, 3" from bottom
                          && to 4" from left, 5" from bottom

WAIT WINDOW "So we undo it"

appVisio.Undo
CLEAR

WAIT WINDOW " and create a reference named 'Oval' and draw the oval"
Oval = docpage.DrawOval(5,3,4,5)  && Oval is now the reference pointer

** This next command may seem logical but it doesn't work
* Oval.Move(3,3)
** With Ovals, you need to set the Center
WAIT WINDOW "Then move Oval's Center Point"
Oval.SetCenter(1,1)
WAIT WINDOW "Then move it again"
Oval.SetCenter(10,10)
WAIT WINDOW "Oops, right off of the page. We'll fix it!" TIMEOUT .6

Oval.SetCenter(8,10)
**
** Visio Application objects also have collections
**   of Windows and Active Windows.
** This allows you to perform your ZOOM Controls
WAIT WINDOW "Zooming to 150% of full page."
WindowObject = AppVisio.ActiveWindow
WindowObject.Zoom = 1.5

WAIT WINDOW "Zooming back to 20% of full page."
WindowObject.Zoom = .20

WAIT WINDOW "With Visio, you can also add other stencils."
  WAIT WINDOW "Adding the Organization Chart Stencil"
  AppVisio.Documents.Add("orgchart.vss")

** As well as identify different STYLES to use.
WAIT WINDOW "Enumerate count of Styles, then Styles 1, 2, & 25"
? "Styles.Count = "+str(VisioDoc.Styles.Count,2,0)
? "Styles(1).Name = "+VisioDoc.Styles(1).Name
? "Styles(2).Name = "+VisioDoc.Styles(2).Name
? "Styles(25).Name = "+VisioDoc.Styles(25).Name

** with these styles you can change an object's appearance.
WAIT WINDOW "Let's Look at the FillStyle property" + chr(13) + ;
            "The first will be 70% Gray"
RectObject.FillStyle = "70% Gray Fill"

WAIT WINDOW "About to create rectangle 2"
RectObject2 = DocPage.Drop (MasterObject, 6,9)
WAIT WINDOW "FillStyle = 50% Gray"
RectObject2.FillStyle = "50% Gray Fill"

WAIT WINDOW "About to create rectangle 3, 30% Gray"
RectObject3 = DocPage.Drop (MasterObject, 4,8)
RectObject3.FillStyle = "30% Gray Fill"

WAIT WINDOW "About to create rectangle 4, 10% Gray"
RectObject4 = DocPage.Drop (MasterObject, 6,8)
RectObject4.FillStyle = "10% Gray Fill"

WAIT WINDOW "Erasing the Left Two Rectangles"
RectObject.delete
RectObject3.delete

WAIT WINDOW "Saving drawing as c:\vfp\visiodem.vsd"
IF FILE("c:\vfp\visiodem.vsd")
  ERASE visiodem.vsd
ENDIF

appvisio.application.windows.item(1).activate  && Shift focus back
                                               && to drawing1 from
                                               && orgchart stencil
appvisio.application.activedocument.saveas("c:\vfp\visiodem.vsd")

WAIT WINDOW "Closing Visio"
=AppVisio.Documents()  && ok
appvisio.quit

CLEAR
WAIT WINDOW "Appending saved file to General Field"
CLOSE ALL
IF FILE("Genvisio.dbf")
  ERASE genvisio.dbf
  ERASE genvisio.fpt
ENDIF

CREATE TABLE genvisio (genfield g)
APPEND BLANK
APPEND GENERAL genfield FROM visiodem.vsd

=MessageBox("Now, expand the VFP window to full size, and the"+chr(13)+ ;
            "Browse window to full size within the VFP window")

WAIT WINDOW "The browse will display a blank edit region."+chr(13) + ;
            chr(13)+"Double-click the edit region to see" +chr(13)+ ;
            "inplace activation of the VISIO object."

BROWSE
** End of Visio.prg 

Additional query words: VFoxWin

Keywords : kb3rdparty kbcode kbVFp300 kbVFp300b kbVFp600 FxinteropOle
Version : 3.00 3.00b
Platform : WINDOWS
Issue type :


Last Reviewed: August 11, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.