Using text from field codes

D

Dawn

Is it possible to convert field codes in a Word document
into text that can be handled normally?

The field codes appear like:

{INCLUDETEXT "Main_Files/Test_Program_Results.htm" \*
MERGEFORMAT }

I would like to be able to use the filename, shown between
the " symbols, by creating a macro to copy it and then
paste it where required, but the brackets at the end which
define the field will not allow this to be carried out. As
soon as they are selected the whole field becomes a
selection.

Thanks in advance.

Dawn
 
G

Graham Mayor

CTRL+SHIFT+F9 will convert a field to text, but in this case it will convert
the included text to editable text, which is not what you appear to want. If
it is the filename you want to use elsewhere, select the filename and
bookmark it. You can then reproduce it with a REF field. This has an
additional snag in that paths in Includetext fields should have double
slashes in order to work correctly and this will almost certainly not be
required elsewhere.

Perhaps you could clarify what you are trying to do?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
Graham Mayor - Word MVP
E-mail (e-mail address removed)
Web site www.gmayor.dsl.pipex.com
Word MVP web site www.mvps.org/word
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
 
J

Jay Freedman

Hi, Dawn,

You can use the .Code property of the Field object to get the text between
the field braces, and then you have to do some work to extract the filename
from that string.

If you're working in Word 2000 or later, you can use the Replace function to
do the work easily (assuming that the fields were inserted through the
Insert > Field dialog so they have uniform capitalization of the keywords):

Sub foo1()
Dim oFld As Field
Dim strFName As String

For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldIncludeText Then
strFName = oFld.Code

' remove up to first quote
strFName = Replace(strFName, _
" INCLUDETEXT """, "")

' remove last quote & after
strFName = Replace(strFName, _
""" \* MERGEFORMAT ", "")

' convert double backslashes
strFName = Replace(strFName, _
"\\", "\")

MsgBox strFName
End If
Next oFld
End Sub

If you have to deal with Word 97, which doesn't have the Replace function,
or if the capitalization of the keywords may be inconsistent, then you have
a bit more work to do:

Sub foo2()
Dim oFld As Field
Dim strFName As String
Dim nPos As Long

Const Qt As String = """"

For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldIncludeText Then
strFName = oFld.Code

' remove quotes and
' everything before & after
nPos = InStr(strFName, Qt)
If nPos Then
strFName = Right(strFName, _
Len(strFName) - nPos)
nPos = InStr(strFName, Qt)
If nPos Then
strFName = Left(strFName, nPos - 1)
End If
End If

' change double backslashes to single
nPos = InStr(strFName, "\\")
Do While nPos
strFName = Left(strFName, nPos) & _
Right(strFName, _
Len(strFName) - nPos - 1)
nPos = InStr(strFName, "\\")
Loop

MsgBox strFName
End If
Next oFld
End Sub
 

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