ShaneEP
(MM club member)
2016-04-18 03:21 PM
Visio Automation

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


JochenAdministrator
(KiX Supporter)
2016-04-18 04:12 PM
Re: Visio Automation

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 ;\)


ShaneEP
(MM club member)
2016-04-18 05:11 PM
Re: Visio Automation

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.


ShaneEP
(MM club member)
2016-04-18 05:45 PM
Re: Visio Automation

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


JochenAdministrator
(KiX Supporter)
2016-04-18 06:52 PM
Re: Visio Automation

Good riddance ;\)

ShaneEP
(MM club member)
2016-04-20 08:30 PM
Re: Visio Automation

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


AllenAdministrator
(KiX Supporter)
2016-04-21 01:07 AM
Re: Visio Automation

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.

AllenAdministrator
(KiX Supporter)
2016-04-21 01:16 AM
Re: Visio Automation

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


Arend_
(MM club member)
2016-04-21 03:30 PM
Re: Visio Automation

 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


ShaneEP
(MM club member)
2016-04-21 04:05 PM
Re: Visio Automation

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!

ShaneEP
(MM club member)
2016-04-21 05:06 PM
Re: Visio Automation

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


Arend_
(MM club member)
2016-04-22 01:24 PM
Re: Visio Automation

Glad I could be of some help \:\)

NTDOCAdministrator
(KiX Master)
2016-04-22 06:30 PM
Re: Visio Automation

Now all you have to do is write a full script that creates a full drawing for like a demo at a show. :-)

ShaneEP
(MM club member)
2016-04-22 08:03 PM
Re: Visio Automation

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.

ShaneEP
(MM club member)
2016-04-29 08:54 PM
Re: Visio Automation

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                           ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Glenn BarnasAdministrator
(KiX Supporter)
2016-04-30 05:34 AM
Re: Visio Automation

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


LonkeroAdministrator
(KiX Master Guru)
2016-04-30 03:39 PM
Re: Visio Automation

I have yet to see any value in visio program in itself...

ShaneEP
(MM club member)
2016-05-02 02:19 AM
Re: Visio Automation

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.


BradV
(Seasoned Scripter)
2016-05-02 12:10 PM
Re: Visio Automation

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. \:\)


LonkeroAdministrator
(KiX Master Guru)
2016-05-02 02:11 PM
Re: Visio Automation

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.

AllenAdministrator
(KiX Supporter)
2016-07-12 03:55 AM
Re: Visio Automation

Out of the 200 or so I've upgraded I only had two show stoppers. One would not upgrade at all, much like what you are describing, and one that wouldn't work because of an Intel Raid Array software glitch. The rest were pretty much butter.