🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #18069

Mapping XML character styles : VB Code

#18069
From: Michael Bray
Date: Mon, 28 Apr 2003 10:17
60 lines
2918 bytes
This is my final solution for mapping xml tagged character styles to InDesign character styles.
It is based on code snippets from many different emails so thanks goes in whole to those original mailers but I thought I would give my final code here to save others the leg work.

Example xml:
<bodytext>blah blah <b>blah</b> blah <i>blah</i> blah<bodytext>

There are two VB functions below.
You should have an InDesign document object created in the usual way.
I call mine oDoc and it is passed into the functions by reference.

Public Sub DoCharStyles(ByRef oDoc As InDesign.Document)
    Dim oCharStyle As InDesign.CharStyle

    'if you already have the character styles created
    'then you don't need the following

    'create the bold character style
    Set oCharStyle = oDoc.CharStyles.Add
    oCharStyle.Name = "Bold"
    oCharStyle.FontStyle = "Bold"

    'create the italic character style
    Set oCharStyle = oDoc.CharStyles.Add
    oCharStyle.Name = "Italic"
    oCharStyle.FontStyle = "Italic"

    'map the inline xml tagged character styles
    MapXMLTagsToCharStyles oDoc.XMLElements, oDoc

End Sub

Private Sub MapXMLTagsToCharStyles( _
    ByVal Elements As InDesign.XMLElements, _
    ByRef oDoc As InDesign.Document)

    'map inline xml tagged character styles for
    'Bold, Italic etc to the InDesign character styles

    Dim Counter As Integer
    Dim TextItem As InDesign.Text

    'traverse the xml tree and map the styles
    On Error Resume Next
    For Counter = 1 To Elements.Count
        If Elements.item(Counter).XMLElements.Count > 0 Then
            MapXMLTagsToCharStyles Elements.item(Counter).XMLElements, oDoc
        Else
            If (Elements.item(Counter).Contents.item(1).Texts.item(1).TextContents <> "") Then
                Set TextItem = Elements.item(Counter).Contents.item(1).Texts.item(1)
                Select Case LCase(Elements.item(Counter).MarkupTag.Name)
                Case "b"
                    TextItem.ApplyStyle oDoc.CharStyles.item("Bold")
                Case "i"
                    TextItem.ApplyStyle oDoc.CharStyles.item("Italic")
                End Select
            End If
        End If
    Next

End Sub
Technical Details
Message-ID: <1de8e6dc.-1@WebX.la2eafNXanI>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!news.maxwell.syr.edu!nntp.infostrada.it!newsfeed01.sul.t-online.de!t-online.de!WebX

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f939a,12f80cd395aab5b5,start
X-Google-Attributes: gidf939a,public
X-Google-ArrivalTime: 2003-04-28 03:40:04 PST
From: "Michael Bray" <michael@xgpad.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!news.maxwell.syr.edu!nntp.infostrada.it!newsfeed01.sul.t-online.de!t-online.de!WebX
Newsgroups: adobe.scripting.indesign
Subject: Mapping XML character styles : VB Code
Message-ID: <1de8e6dc.-1@WebX.la2eafNXanI>
Date: Mon, 28 Apr 2003 03:17:23 -0700
Lines: 69
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit