Extract information from a field code

F

frank

I am trying to create a better userform for inserting table of authority tags
(it's used in the legal industry). If you press SHIFT + ALT + I while in
word you will see the native word userform I'm talking about. I have a few
textboxes set up on the form but I need my userform to populate with the {TA}
tag information that is in the document. I have 3 separate boxes on the form:

Longcitation
Shortcitation
Category

This information is located in the {TA} field tags that have been placed in
the document. I just need to extract this information from the field tags.
i.e.,

{TA \l "Frank v. Everyone" \s "Frank" \c 1 }

My userform would read each one of these fields in the document and extract
the 3 pieces of information into 3 separate textboxes on the form

Longcitation listbox would read: Frank v. Everyone
Shortcitation listbox would read: Frank
Category listbox would read: 1

How can I easily read every {TA} in the document and extract the 3 types of
information into 3 separate listboxes?

Any help is greatly appreciated thanks. :D
 
J

Jay Freedman

Follow the comments in this demo:

Sub Demo()
Dim fld As Field
Dim fcode As String
Dim codeParts As Variant
Dim i As Integer
Dim partType As String
Dim part As String
Dim lparam As String
Dim sparam As String
Dim cparam As String

For Each fld In ActiveDocument.Fields
'check only the TA fields
If fld.Type = wdFieldTOAEntry Then
' get the field code in a string variable
fcode = fld.Code

' make an array of the code parts
codeParts = Split(fcode, "\")

' codeParts(0) contains the TA keyword
'which can be ignored
For i = 1 To UBound(codeParts)
' get the letter that followed the slash
partType = LCase(Left(codeParts(i), 1))

'get the rest of the part and
'remove the quotes and spaces at the ends
part = codeParts(i)
part = Right(part, Len(part) - 2)
part = Trim(Replace(part, """", ""))

'put the result in the proper bin
'(replace these with assignments to
'.Text property of the text boxes)
Select Case partType
Case "l"
lparam = part
Case "s"
sparam = part
Case "c"
cparam = part
Case Else
End Select
Next

'just so you can see the results for this demo
MsgBox "Longcitation: " & lparam & vbCr & _
"Shortcitation: " & sparam & vbCr & _
"Category: " & cparam
End If
Next fld
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
F

frank

There is one other thing. These table of authority codes also exist in the
footnotes and the suggested coding only looks at the main document. Is there
a way to read the TA field codes located in the footnotes as well?
 
J

Jay Freedman

Yes, this will catch TA fields in footnotes, endnotes, and anywhere
else you were able to stick them:

Sub Demo()
Dim oRg As Range
Dim fld As Field
Dim fcode As String
Dim codeParts As Variant
Dim i As Integer
Dim partType As String
Dim part As String
Dim lparam As String
Dim sparam As String
Dim cparam As String

For Each oRg In ActiveDocument.StoryRanges
Do
For Each fld In oRg.Fields
'check only the TA fields
If fld.Type = wdFieldTOAEntry Then
' get the field code in a string variable
fcode = fld.Code

' make an array of the code parts
codeParts = Split(fcode, "\")

' codeParts(0) contains the TA keyword
'which can be ignored
For i = 1 To UBound(codeParts)
' get the letter that followed the slash
partType = LCase(Left(codeParts(i), 1))

'get the rest of the part and
'remove the quotes and spaces at the ends
part = codeParts(i)
part = Right(part, Len(part) - 2)
part = Trim(Replace(part, """", ""))

'put the result in the proper bin
'(replace these with assignments to
'.Text property of the text boxes)
Select Case partType
Case "l"
lparam = part
Case "s"
sparam = part
Case "c"
cparam = part
Case Else
End Select
Next

'just so you can see the results for this demo
MsgBox "Longcitation: " & lparam & vbCr & _
"Shortcitation: " & sparam & vbCr & _
"Category: " & cparam
End If
Next fld
Set oRg = oRg.NextStoryRange
Loop Until oRg Is Nothing
Next oRg
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
F

frank

ohhh man, that's perfect. God, I love beautifully written code. I know this
stuff is probably nothing to guys like you, but to a novice like me it reads
like poetry. It makes the dialog box I created work so smoothly.

Thanks again for everything.
 
J

Jay Freedman

frank said:
ohhh man, that's perfect. God, I love beautifully written code. I
know this stuff is probably nothing to guys like you, but to a novice
like me it reads like poetry. It makes the dialog box I created work
so smoothly.

Thanks again for everything.

Thank you for the compliment. :)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top