Save current visio page as PNG and SVG files using VBA
為了要在 OpenOffice.org2 裡用 Visio 畫出來的圖,我總是得先在 Visio 裡 save as 成 PNG 格式,然後再在 OpenOffice.org2 的 writer 裡插入並設定連結。由於要不斷地在 Visio 與 OpenOffice.org2 之間修改,所以每次都要另外設定存檔格式,十分麻煩。所以簡單地寫了個 VBA,簡化這個步驟:
- 將目前正在編輯的 Visio 檔案的目前頁面,以目前檔名去掉延伸檔名的部分,存檔成 PNG 格式的圖檔 (當然要再加上 .png 副檔名)。
- 理想上,透過 SVG 格式會更好。但是因為目前 OpenOffice.org2 對 SVG 的支援,只有一個半官方的 XML filter 叫 SVG Import Filter for OpenOffice 2.0 勉強可以用,所以暫時還是在 Visio 裡同時存檔成 PNG 與 SVG 檔,先用 PNG,以後有機會再用 SVG。
用 macro 錄製了一下,發覺關鍵很簡單,就是這麼一行:
Application.ActiveWindow.Page.Export <filename>
其中,Visio 會依據 <filename> 的副檔名,決定要 export 成哪種格式。再加上檔名的處理就一切搞定。以下以 Visio 2003 為準,完整 Sub routine 如下:
Sub ExportPageAsPNGandSVG()
'
' Save current diagram as PNG with default transparent color and SVG files.
'
Dim DotPos
DotPos= InStrRev(ThisDocument.Name, ".")
If Not IsNull(DotPos) And DotPos > 0 Then
Dim NoExtName
NoExtName = Left(ThisDocument.Name, DotPos - 1)
' MsgBox NoExtName
Application.ActiveWindow.Page.Export ThisDocument.Path + NoExtName + ".png"
Application.ActiveWindow.Page.Export ThisDocument.Path + NoExtName + ".svg"
Else
MsgBox "Cannot resolve path name to save, action cancelled."
End If
End Sub
從 Visio 的選單裡,Tools -> Macros -> Macros,在 Macro name 欄位裡填上 ExportPageAsPNGandSVG,按 Create 按鈕,會出現 VBA editor,把上面的程式碼填進去,然後從 VBA editor 的選單裡選 File -> Close and Return to Visio 即可。以後就可以隨時從 Visio 選單的 Tools -> Macros 裡,選擇剛剛編輯的 ExportPageAsPNGandSVG 來用。
Post a Comment