Page 1 of 2 12>
Topic Options
#211321 - 2016-04-18 03:21 PM Visio Automation
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Has anyone ever had any luck automating Visio drawing modifications?

I'm trying to simply edit some text on a drawing. The below code seems to "work" as it does change the text and save it as a new file. But for some reason, Visio always pops open for a second. Don't know how to run it hidden, like I can Excel.

It also always displays the original file name in the console for a second. Maybe something weird with the .Open(), but I can't find any reason why it would do that.

Any ideas?

 Code:
Break On

$DrawingFile = @ScriptDir+"\Drawing1.vsdx"
$date = Join(Split(@Date,"/"),"")
$ext = SubStr($DrawingFile, InStrRev($DrawingFile,"."))
$newDrawingFile = Split($DrawingFile, $ext)[0] + "_" + $date + $ext

$oVisio = CreateObject("VISIO.Application")
$oVisio.Visible = 0
$oVisio.DisplayAlerts = 0

$oVisio.Documents.Open($DrawingFile)

$textBox1 = $oVisio.ActiveWindow.Page.Shapes.ItemFromID(1).Characters
$textBox1.Text = "987654321"

$nul = $oVisio.ActiveDocument.SaveAs($newDrawingFile)

$oVisio.Quit
$oVisio = 0

Top
#211322 - 2016-04-18 04:12 PM Re: Visio Automation [Re: ShaneEP]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
I once had similar trouble with an excel sheet which I tried to invisibly read in .. what helped in the end was using the EnableEvents Property ..

used like this ..

 Code:
$ = createobject('Excel.application')
$.enableEvents = 0
;open document here


didn't even need to mess around with Visible properties..

No clue if this is available in Visio as well, good luck ;\)

Top
#211323 - 2016-04-18 05:11 PM Re: Visio Automation [Re: Jochen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Found the secret via google.

Instead of...

CreateObject("VISIO.Application")

use

CreateObject("VISIO.InvisibleApp")

This fixes the Visio showing for a second. Now just have to figure out how to get the console from showing the file name everytime a file is opened.

Top
#211324 - 2016-04-18 05:45 PM Re: Visio Automation [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Nevermind...I'm an idiot. Just forgot to catch the return of the Open(). Adding nul= in front solved the issue.

 Code:
Break On

$DrawingFile = @ScriptDir+"\Drawing1.vsdx"
$date = Join(Split(@Date,"/"),"")
$ext = SubStr($DrawingFile, InStrRev($DrawingFile,"."))
$newDrawingFile = Split($DrawingFile, $ext)[0] + "_" + $date + $ext


$oVisio = CreateObject("VISIO.InvisibleApp")

$nul = $oVisio.Documents.Open($DrawingFile)

$textBox1 = $oVisio.ActiveWindow.Page.Shapes.ItemFromID(1).Characters
$textBox1.Text = "987654321"

$nul = $oVisio.ActiveDocument.SaveAs($newDrawingFile)

$oVisio.Quit
$oVisio = 0

Top
#211325 - 2016-04-18 06:52 PM Re: Visio Automation [Re: ShaneEP]
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Good riddance ;\)
_________________________



Top
#211353 - 2016-04-20 08:30 PM Re: Visio Automation [Re: Jochen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Alright, having another issue. What is the proper format that should be used to pass parameters to a com function?

For example, if I try to use the .Move() in Visio to move an object, I can't seem to find the magic way to call the function. Only seem to run into problems when the function requires parameters. The below gives me "ERROR: expected ')'" for the line after the move function call. The Copy() and Paste() appear to work fine.

 Code:
Break On
$nul = SetOption("NoMacrosInStrings", "On")
$nul = SetOption("NoVarsInStrings", "On")

$DrawingFile = @ScriptDir+"\drawing1.vsdx"
$newDrawingFile = @ScriptDir+"\drawing2.vsdx"

$oVisio = CreateObject("VISIO.InvisibleApp")
$nul = $oVisio.Documents.Open($DrawingFile)

$CellMDN = $oVisio.ActiveWindow.Page.Shapes.ItemFromID(205)
$CellMDN.Copy()

$oVisio.ActiveWindow.Page.Paste()

$NewCellMDN = $oVisio.ActiveWindow.Page.Shapes.ItemFromID(330)
$NewCellMDN.Move(-0.5,0.3)

$nul = $oVisio.ActiveDocument.SaveAs($newDrawingFile)

$oVisio.Quit
$oVisio = 0

And below is what Visio says happens when I record it in a macro.
 Code:
ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemFromID(330), visSelect
Application.ActiveWindow.Selection.Move -0.536562, 0.324956


Edited by ShaneEP (2016-04-20 08:31 PM)

Top
#211354 - 2016-04-21 01:07 AM Re: Visio Automation [Re: ShaneEP]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
Loc: USA
I never have understood when some com objects will and wont take parameters in kix... but I know there are some that won't. At some point I'm pretty sure I tested this out with the scriptcontrol object, so there should be a work around if no one has any other suggestions.
Top
#211355 - 2016-04-21 01:16 AM Re: Visio Automation [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4549
Loc: USA
As an example, here's the code I fiddled with a while back that passes an existing kix object into the scriptcontrol so you can do stuff with it.

http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=201251#Post201251

Top
#211357 - 2016-04-21 03:30 PM Re: Visio Automation [Re: Allen]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
 Code:
$NewCellMDN = $oVisio.ActiveWindow.Page.Shapes.ItemFromID(330)
$NewCellMDN.Move(-0.5,0.3)

Should be
 Code:
$oVisio.ActiveWindow.Select($oVisio.ActiveWindow.Page.Shapes.ItemFromID(330), 2)
$oVisio.ActiveWindow.Selection.Move(-0.5,0.3)

Based on the Macro.
visSelect = 2 according to Microsoft

Top
#211359 - 2016-04-21 04:05 PM Re: Visio Automation [Re: Arend_]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Thanks Arend, that worked brilliantly. Weird that the copy and paste worked the other way, but not the move. Oh well, as long as it works. Thanks again!
Top
#211360 - 2016-04-21 05:06 PM Re: Visio Automation [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Just a follow up. Found another way to move the objec to a specified x, y. Also how to change the width and height of an object. I just post it here in case anyone else ever needs it.

 Code:
Break On
$nul = SetOption("NoMacrosInStrings", "On")
$nul = SetOption("NoVarsInStrings", "On")

;;;  https://msdn.microsoft.com/en-us/library/office/ff767900.aspx
$visDeselect = 1
$visSelect = 2
$visSubSelect = 3
$visSelectAll = 4
$visDeselectAll = 5

;;;  https://msdn.microsoft.com/en-us/library/office/ff765983.aspx
$visSectionObject = 1
;;;  https://msdn.microsoft.com/en-us/library/office/ff765539.aspx
$visRowXFormOut = 1
;;;  https://msdn.microsoft.com/en-us/library/office/ff767991.aspx
$VisXFormPinX = 0
$VisXFormPinY = 1
$visXFormWidth = 2
$visXFormHeight = 3

$DrawingFile = @ScriptDir+"\Drawing1.vsdx"
$newDrawingFile = @ScriptDir+"\Drawing2.vsdx"

$oVisio = CreateObject("VISIO.InvisibleApp")
$nul = $oVisio.Documents.Open($DrawingFile)

;;;  CHANGE WIDTH OF OBJECT 330 TO 1.65 INCHES
$oVisio.ActiveWindow.Page.Shapes.ItemFromID(330).CellsSRC($visSectionObject, $visRowXFormOut, $visXFormWidth).FormulaU = "1.65 in"

;;;  CHANGE X POSITION OF OBJECT 330 TO 7.3 INCHES
$oVisio.ActiveWindow.Page.Shapes.ItemFromID(330).CellsSRC($visSectionObject, $visRowXFormOut, $visXFormPinX).FormulaU = "7.3 in"

;;;  CHANGE Y POSITION OF OBJECT 330 TO 6.45 INCHES
$oVisio.ActiveWindow.Page.Shapes.ItemFromID(330).CellsSRC($visSectionObject, $visRowXFormOut, $visXFormPinY).FormulaU = "6.45 in"

$nul = $oVisio.ActiveDocument.SaveAs($newDrawingFile)

$oVisio.Quit
$oVisio = 0

Top
#211365 - 2016-04-22 01:24 PM Re: Visio Automation [Re: ShaneEP]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
Glad I could be of some help \:\)
Top
#211373 - 2016-04-22 06:30 PM Re: Visio Automation [Re: Arend_]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Now all you have to do is write a full script that creates a full drawing for like a demo at a show. :-)
Top
#211374 - 2016-04-22 08:03 PM Re: Visio Automation [Re: NTDOC]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
I've considered it. Trying to write scripts around existing templates (modifying only portions of text boxes, resizing and moving boxes, etc...) is almost as hard or harder, than creating the whole thing from scratch. Of course, I haven't even tried placing connectors, or symbols yet. Maybe I should just start creating a visLib to compliment Glenn's excel library.
Top
#211398 - 2016-04-29 08:54 PM Re: Visio Automation [Re: ShaneEP]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
This is what I've got so far, if anyone has any suggestions for further additions.
 Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;    visInit()			Create the Visio object reference                                           ;;
;;    visFile()			Open, Save, SaveAs, or Close file functions                                 ;;
;;    visNewDrawing()		Create a new drawing file from any given template, in US or Metric          ;;
;;    visDrawingProperties()	Sets or Gets properties from current visio drawing                          ;;
;;    visChangePage()		Change the width, height, orientation, or name of current page              ;;
;;    visChangeElement()	Change the size, position, and font properties of an object                 ;;
;;    visChangeText()		Change all or a portion of the text on any given element                    ;;
;;    visCreateRect()		Create a rectangle with optional text, border, and fill                     ;;
;;    visCreateConnector()	Create a line connector between two objects                                 ;;
;;    visCreateElement()	Creates a given element from a given stencil file                           ;;
;;    visDeleteElements()	Deletes element(s) as specified by ID number(s)                            ;;
;;    visCopyElements()		Copies given element(s), as if right-clicked and copied                     ;;
;;    visPasteElements()	Pastes currently copied element(s)                                          ;;
;;    visExportAs()		Exports a visio drawing into either the PDF or XPS format                   ;;
;;    visPrintSetup()		Change print setup options zoom, fit to, centered, margins, and gridlines   ;;
;;    visPrint()		Prints all pages of current document to specified printer                   ;;
;;    VisQuit()			Destroy the instantiated object & shut down Visio                           ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Top
#211400 - 2016-04-30 05:34 AM Re: Visio Automation [Re: ShaneEP]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Have you looked at the XLLib library? Might be good to follow the standards for names. We combined a couple of distinct functions and libraries into a common file / format.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#211401 - 2016-04-30 03:39 PM Re: Visio Automation [Re: Glenn Barnas]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
I have yet to see any value in visio program in itself...
_________________________
!

download KiXnet

Top
#211402 - 2016-05-02 02:19 AM Re: Visio Automation [Re: Lonkero]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
Yeah I have Glenn. I actually tried to mimic it where I could.

I haven't used Visio much in the past either Jooel, but my new job requires a lot of drawings to be made. And a lot of it is just entering IP addresses into templates...They were just asking to be automated.

Top
#211403 - 2016-05-02 12:10 PM Re: Visio Automation [Re: ShaneEP]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 686
Loc: Maryland, USA
I have been using Visio for many years. I find it quite useful. Back in the early 2000s, I was consulting with a company that made fiber optic transport equipment for the tv cable industry. I created custom visio icons for them. I made the icons able to change based on selection. For example, I made it so that you could right click on it and select LC connectors and the image would change to LC connectors, or the same for SC connectors. Some devices were very similar. I made a common icon that you could just right click on select the desired type and the image would change to match the selection.

Visio is object oriented. You can assign properties to objects. So, you can put the serial number, part number, manufacturer, support end date, amount of RAM, power ratings, rack elevation, etc (what ever you find appropriate) in to the object. Visio is also COM aware, so you can put those properties in an external database or spreadsheet and link the object to it.

Having a Visio diagram of all of my racks helps me visualize what is where. I have notations in them about how each is connected to everything. For example, the left power supply of device x is connected to PDU z, port 4. The PDUs have an Ethernet interface. If all else fails, and I need to remove power and then restore it, I can do it remotely and I know exactly what ports to shut down. I have in the diagrams what serial ports are connected to what devices. So, I know if I go to server w, it is serially connected to the Cisco switch in the rack and I can get on the console in that fashion.

I wrote custom visual basic code as part of some balloon callouts. Now if you place that callout on an object, it will read the object's properties and display certain information in the callout. It easily makes critical information available directly on the diagram.

I've just found it to be very useful. \:\)

Top
#211404 - 2016-05-02 02:11 PM Re: Visio Automation [Re: BradV]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
Your conclusion is different than mine. \:\) I found it needed too much work and customization. It isn't free and as paid software it should not require so much work was my end take on it.
_________________________
!

download KiXnet

Top
Page 1 of 2 12>


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (mole) and 405 anonymous users online.
Newest Members
Timothy, Jojo67, MaikSimon, kvn317, kixtarts2025
17874 Registered Users

Generated in 0.079 seconds in which 0.027 seconds were spent on a total of 15 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org