Parsing Fields in VBA code

M

ML

I have a document that will have fields inserted by the user. Somehow I
need to allow the user to insert text in these fields that is freeform, the
only way I guess this can be done is via the fields add method. Assuming I
can do this in code, and set the user text as the QUOTE section of the
field, is there some means that I can later parse the document for a given
field code, and then strip out the QUOTE text in code?
 
D

Dave Lett

Hi ML,

You might be able to use something like the following:

Dim oFld As Field
Dim sFldText As String
Dim sFldCode As String
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldQuote Then
sFldCode = oFld.Code.Text
'''strip unneeded material
sFldText = sFldText & Mid(sFldCode, 9, Len(sFldCode) - 25) & vbCrLf
End If
Next oFld
Debug.Print sFldText

If you're not sure about what the field code might be, then you could change
the MID function to test for "extra" material by using the InStr function.

HTH,
Dave
 
M

ML

Thanks. As this is looping, is there an easy means to determine what page
the current field is on? I need to determine the page that each field is
found on in order to handle some additional processing.

So basically the user inserts these custom quote fields. At some later time
a macro parses the document, as it find each field it then saves a variable
to indicate something nit has found in relation to the page it was found on.

Can I determine the page easily?
 
D

Dave Lett

Hi ML,

Yes, there is an easy way to get the page number of a field. Here's an
example:

For Each oFld In ActiveDocument.Fields
Debug.Print oFld.Result.Information(wdActiveEndPageNumber)
Next oFld

HTH,
Dave
 

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