Microsoft KB Archive/147311

= How to Control Visio(TM) Through OLE Automation =

Article ID: 147311

Article Last Modified on 10/15/2003

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 3.0b Standard Edition
 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q147311



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                           **
 * 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)
 * called Basic.vss (or stencil)

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


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

MasterObject = MasterDoc.Masters("Rectangle")

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
 * This drops the object onto the document page
 * This drops the object onto the document page


 * You can also edit the text for an object
 * 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

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
 * This next command may seem logical but it doesn't work
 * Oval.Move(3,3)
 * With Ovals, you need to set the Center

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

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")

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
 * As well as identify different STYLES to use.

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

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 KB147311

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.