Converting VSD Drawings to PNGs – VBScript

This VBScript contains the logic to convert each drawing (page) of a specific Microsoft Visio file to a png named after its source page name and saves it to a target directory.

Option Explicit

' constants required for opening files in Visio
Const visOpenRO = 2
Const visOpenMinimized = 16
Const visOpenHidden = 64
Const visOpenMacrosDisabled = 128
Const visOpenNoWorkspace = 256

' constants required for setting ExportSize in Visio
Const visRasterFitToCustomSize = 3
Const visRasterPixel = 0


Sub export(filePath, exportDirectory, widthInPixels, heightInPixels)

    ' open file
    Dim visioApplication : Set visioApplication = CreateObject("Visio.Application")

    ' set export size
    visioApplication.Settings.SetRasterExportSize visRasterFitToCustomSize, widthInPixels, heightInPixels, visRasterPixel

    ' open document in Visio without showing it to the user
    visioApplication.Documents.OpenEx filePath, visOpenRO + visOpenMinimized + visOpenHidden + visOpenMacrosDisabled + visOpenNoWorkspace

    ' iterate over all pages and export each one
    Dim currentItemIndex
    For currentItemIndex = 1 To visioApplication.ActiveDocument.Pages.Count

        Dim currentItem : Set currentItem = visioApplication.ActiveDocument.Pages.Item(currentItemIndex)

        ' use the lowercase name for the file
        Dim exportPath : exportPath = exportDirectory & "\" & LCase(currentItem.Name) & ".png"

        ' export happens here!
    	currentItem.Export exportPath
    Next

    ' Quit Visio
    visioApplication.Quit
End Sub

' current directory
Dim currentDirectory : currentDirectory = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")

' file to open
Dim filePath : filePath = currentDirectory & "\AI - stundenplan.vsd"

' set export directory
Dim exportDirectory : exportDirectory = currentDirectory

export filePath, exportDirectory, 3557, 4114

Annotations about VBScript in general to better understand what is going on in this script.

  • The colon (:) is the statement separator. This can be used to declare and assign a variable in one line.
  • Use Dim NAME : NAME = VALUE for variables referencing not objects
  • Use Set NAME = OBJECT for variables referencing objects
  • Line Comments are started with '
  • No parantheses are allowed for calling Subs (procedures) or Functions
  • Stating Option Explicit at the first line requires each variable to be declared before it can be used
  • To determine what parameters to set, you can use the record macro function in Visio. This button is not directly available in Visio 2010, refer to this guide on how to make it visible.
  • The object explorer of Visio 2010 is very helpful to find the correct functions or procedures.
  • Use the ampersand (&) to concatenate strings

Using the Kindle for Reading Scientific Papers

Yesterday, my all new Kindle arrived. Screen size 15cm, weighing only 170g.

My Kindle

Why? To read more. Well, this was my excusion to myself.

It really is a fascinating device. The contrast of the screen is very high and reading makes much fun with it. If you are reading books formatted in the .mobi / the kindle format. Using this format, you can change the font size and it automatically adjust itself.

On constrast, if you want to read PDFs on this small screen, you may have luck that the page size of the PDF is very small and, therefore, the letters are readable. As the font size cannot be changed, you cannot increase it. But most PDFs have DIN A4, US LETTER or DIN A5 as their format. Or something in between. Some may have two columns (which is common in scientific papers from IEEE) and others footnotes…. All these kinds of things makes a PDF very hard to read on the Kindle I bought.

So, what can be done about this?

  • Buy a Kindle DX. This Kindle has a 9.7″ screen size and costs $380.
  • Rotate the screen to landscape mode for having a little higher font size.
  • You can only zoom in.
  • Convert the PDF.

I don’t want to buy a Kindle DX as it is too expensive, has to be bought from amazon.com and shipped from the US.

I have tried to rotate the screen. It works for some cases, but it is for from perfect.

Zooming makes everything only worse. Trust me!

I testet the conversion tool named K2pdfoft which optimizes a PDF for a kindle device. The results are quite ok for the PDF I testet (one column, scientific, with tables, footnotes).

I will post more about conversion tools and compare them with each other testing various scientific paper styles in the future.  In the mean time, I will read my books which have the mobi format.

PS: If you buy books at http://manning.com or http://pragprog.com, you get the eBook as a PDF, mobi and ebug DRM free format! This is really cool!

PPS: I will try to be a more active blogger. 🙂 Especially as I have a new design!!!!!!!!!!