Archive

Posts Tagged ‘VBA’

InDesign Scripting with VBA Example

November 24th, 2009 admin No comments

I was pleased to see that my old example still runs in CS4. Using VBA was very cool, it just suffered from dependency on Windows. But functionally we did many applications back in the early days of InDesign that worked just great with VBA. I think it was VB and AppleScript prior to JS and the cross-platform scripting? Let me go look that up.

In any case while JS scripting seems far more useful now, or “ExtendScript” as they call it, we still suffer from the sandbox and the inability to shell out to data sources and do work with them then get right back to automating InDesign. Using Java one can do such things with InDesign Server, but the Server-only nature of the Java makes debugging difficult. Its strongly-typed nature is a big headache, as with C# or any strongly typed language: you spend more time figuring out what type object you’re working with than you do manipulating the object.

I want to translate the VBA example to JavaScript with some database such as MySQL. In general the power of InDesign to do data-generated publishing is awesome.

Yes, JavaScript is better. VB is actually buggy… How come this JS works:

var myDocument = app.documents.item(0);
var myRectangle = myDocument.rectangles.item(0);
myHyperlinkURL = myDocument.hyperlinkURLDestinations.add("http://www.publishingsilicon.com");
myHyperlinkSource = myDocument.hyperlinkPageItemSources.add(myRectangle);
myHyperlink=myDocument.hyperlinks.add(myHyperlinkSource,myHyperlinkURL);
myHyperlink.visible=false;

and this equivalent in VB doesn’t:

Dim oInDesign As InDesign.Application
Dim oDocument As InDesign.Document
Dim oPage As InDesign.Page
Dim oImageRect As InDesign.Rectangle
Dim oHyperlinkURL As InDesign.HyperlinkURLDestination
Dim oHyperlinkSource As InDesign.HyperlinkPageItemSource
Dim oHyperlink As InDesign.Hyperlink
Set oInDesign = CreateObject("InDesign.Application.CS4")
Set oDocument = oInDesign.ActiveDocument
Set oImageRect = oDocument.Rectangles.Item(1)
Set oHyperlinkURL = oDocument.HyperlinkURLDestinations.Add("http://www.publishingsilicon.com")
Set oHyperlinkSource = oDocument.HyperlinkPageItemSources.Add(oImageRect)
Set oHyperlink = oDocument.Hyperlinks.Add(oHyperlinkSource, oHyperlinkURL)
oHyperlink.Visible = False

?

The VBA version bombs on the line with oDocument.HyperlinkPageItemSources.Add… and this sort of thing was posted to the forums over a year ago. I guess yet another reason the cross-platform JavaScript form of InDesign scripting is better is that it enjoys support. Can hardly blame Adobe for limiting support, especially as they cut 9% of their workforce when holiday season comes along. You can’t fight every battle.

Share
Categories: InDesign Tags: , ,