#211321 - 2016-04-18 03:21 PM
Visio Automation
|
ShaneEP
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?
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
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 ..
$ = 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
|
|
|
|
#211324 - 2016-04-18 05:45 PM
Re: Visio Automation
[Re: ShaneEP]
|
ShaneEP
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.
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
|
|
|
|
#211353 - 2016-04-20 08:30 PM
Re: Visio Automation
[Re: Jochen]
|
ShaneEP
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.
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.
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
|
|
|
|
#211357 - 2016-04-21 03:30 PM
Re: Visio Automation
[Re: Allen]
|
Arend_
MM club member
Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
|
$NewCellMDN = $oVisio.ActiveWindow.Page.Shapes.ItemFromID(330)
$NewCellMDN.Move(-0.5,0.3)
Should be
$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
|
|
|
|
#211360 - 2016-04-21 05:06 PM
Re: Visio Automation
[Re: ShaneEP]
|
ShaneEP
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.
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_
MM club member
Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
|
Glad I could be of some help
|
Top
|
|
|
|
#211398 - 2016-04-29 08:54 PM
Re: Visio Automation
[Re: ShaneEP]
|
ShaneEP
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.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 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
|
|
|
|
#211403 - 2016-05-02 12:10 PM
Re: Visio Automation
[Re: ShaneEP]
|
BradV
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
|
|
|
|
Moderator: Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
|
0 registered
and 920 anonymous users online.
|
|
|