Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

InDesign Visual Basic Data Merge

Status
Not open for further replies.

rgoerss

Programmer
Aug 28, 2002
11
0
0
US
Hey there,

I'm looking to fully automate a template creation / data merge process and I am running into trouble with creating the data merge fields and inserting them in the text frames. It seems like the only way to insert a data merge field is in a story... But stories don't allow for geometric sizing, which is how I control the layout of the template using text frames. I'm wondering if it is possible to use a story to build the contents (Including an inserted data merge field) and then somehow assign this contents to a text frame.

I'm also wondering if I then change the story if the contents of the text frame will change (i.e. linked) or if the contents is actually embedded in the text frame.

I'm sort of hoping that somebody here has messed with automating data merges from VB and can help me out here. I don't have a ton of time to play with it any more than I have already, work constraints and all.

Thanks,
Rob
 
Alright, I've had a chance to mess with things a bit and I figured out a way of doing this that I thought would work, but for some reason this code crashes InDesign.

Code:
myDocument.DataMergeProperties.SelectDataSource "U:\JOBS\" & sJobNumber & "\LOT_DATA\" & lData.LotNumber & ".csv"

If myDocument.Stories.Count >= 1 Then
  For i = 1 To myDocument.Stories.Count
    DoEvents
    Set myStory = myDocument.Stories.Item(i)
    sContent = myStory.Contents
    lPos1 = InStr(sContent, "<HOUSEP>")
    If lPos1 > 0 Then
      sNewContent = Left(sContent, lPos1 - 1) & Mid(sContent, lPos1 + 8)
      myStory.Contents = sNewContent
      Set myDataMergeField = myDocument.DataMergeProperties.DataMergeFields.Item(1)
      Set myDMTextPH = myDocument.DataMergeTextPlaceholders.Add(myStory, lPos1, myDataMergeField)
    End If

This is obviously a stripped down version of it. The idea here is to replace textual placeholders that are entered during the data entry stage with DataMergeTextPlaceholders. The code causes InDesign to stop responding on the last line which is supposed to insert the DataMergeTextPlaceholder into the story.
 
Nevermind, turns out that the StoryOffset parameter of the Add method uses a 0-based character index, whereas all of the arrays in the InDesign object model use a 1-based index. I should have thought of that earlier... On the bright side, everything is working properly now. Thread concluded!

For future reference, in case anybody else is looking to 100% automate a merge from Visual Basic, here is a sample of the working code:

Code:
If myDocument.Stories.Count >= 1 Then
  For i = 1 To myDocument.Stories.Count
    DoEvents
    Set myStory = myDocument.Stories.Item(i)
    sContent = myStory.Contents
    lPos1 = InStr(sContent, "<HOUSEP>")
    If lPos1 > 0 Then
      sNewContent = Left(sContent, lPos1 - 1) & Mid(sContent, lPos1 + 8)
      myStory.Contents = sNewContent
      Set myDataMergeField = myDocument.DataMergeProperties.DataMergeFields.Item(1)
      Set myDMTextPH = myDocument.DataMergeTextPlaceholders.Add(myStory, lPos1 - 1, myDataMergeField)
    End If

[...repeat checks for additional fields as necessary...]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top