Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2007

Tip: Looking for answers? Try searching our database.

Extract information from a field code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
frank - 31 Mar 2007 06:02 GMT
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
Jay Freedman - 31 Mar 2007 17:25 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>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
[quoted text clipped - 23 lines]
>
>Any help is greatly appreciated thanks.  :D
frank - 31 Mar 2007 20:30 GMT
This is beautiful Man!

Thank you so much!

Long live Sparta

> Follow the comments in this demo:
>
[quoted text clipped - 86 lines]
> >
> >Any help is greatly appreciated thanks.  :D
frank - 05 Apr 2007 00:02 GMT
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?

> Follow the comments in this demo:
>
[quoted text clipped - 86 lines]
> >
> >Any help is greatly appreciated thanks.  :D
Jay Freedman - 05 Apr 2007 01:29 GMT
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        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>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
[quoted text clipped - 90 lines]
>> >
>> >Any help is greatly appreciated thanks.  :D
frank - 05 Apr 2007 02:00 GMT
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.  

> Yes, this will catch TA fields in footnotes, endnotes, and anywhere
> else you were able to stick them:
[quoted text clipped - 160 lines]
> >> >
> >> >Any help is greatly appreciated thanks.  :D
Jay Freedman - 05 Apr 2007 14:33 GMT
> 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. :-)

Signature

Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.