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

Re: Using text from field codes by Graham

Graham
Fri Aug 15 06:30:23 CDT 2003

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 gmayor@mvps.org
Web site www.gmayor.dsl.pipex.com
Word MVP web site www.mvps.org/word
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>



Dawn wrote:
> 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



Re: Using text from field codes by Jay

Jay
Fri Aug 15 09:48:46 CDT 2003

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

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word

Graham Mayor wrote:
> 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?
>
>
> Dawn wrote:
>> 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