Template with filename/path in the header

W

WhiteRat

I'm running Word 2007 on XP/SP3

I need to open data files which are saved as .txt files in such a way that
the filename and path appear in the document. I cannot alter the name of the
file or the folders along the path, as this would disrupt data integrity for
the study I'm working on. The best I've been able to do so far is to right
click on the file and open with Word, then put the filename with path
{FILENAME\p\*MERGEFORMAT} in the header . I have a few hundred of these to
do, and I haven't been able to figure out a way to streamline the process.

I tried to rename my normal.dotm, and create a new one that I put the
filename and path in the header. But the field doesn't appear in the header
when opening a text file (it works fine on .docx files). I'm wondering if
there might be some way I can open these text files and have the file name
and path automatically entered, even if its not in the header.

Any help would be greatly appreciated.

David W.
 
G

Graham Mayor

TXT files do not support fields, pages or headers for that matter. If you
open a TXT file in Word all the functions of Word become available to edit
that document, but if you put the filename field in the header, there will
be no header when you save the document as a TXT file and the filename field
will be converted to text and placed at the end. It might therefore be
simpler to run a batch process to open all the documents in turn and add the
filename and path at the end. You can do this with a macro. Put all the TXT
files you want to modify in a single folder (or run the macro on the folders
where the TXT files currently lie). Note that as written it will modify ALL
files with the TXT extension in the current folder.

Sub BatchProcess()
Dim strFileName As String
Dim strPath As String
Dim oDoc As Document
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

With fDialog
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , _
"List Folder Contents"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" _
Then strPath = strPath + "\"
End With

If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(strPath, 1) = Chr(34) Then
strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strFileName = Dir$(strPath & "*.txt")

While Len(strFileName) <> 0
Set oDoc = Documents.Open(strPath & strFileName)
'
'Do what you want with oDoc
oDoc.Range.InsertAfter vbCr & oDoc.FullName
'
oDoc.Close SaveChanges:=wdSaveChanges
strFileName = Dir$()
Wend
End Sub

http://www.gmayor.com/installing_macro.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
W

WhiteRat

It works perfectly. Thank you so much Graham

Graham Mayor said:
TXT files do not support fields, pages or headers for that matter. If you
open a TXT file in Word all the functions of Word become available to edit
that document, but if you put the filename field in the header, there will
be no header when you save the document as a TXT file and the filename
field will be converted to text and placed at the end. It might therefore
be simpler to run a batch process to open all the documents in turn and
add the filename and path at the end. You can do this with a macro. Put
all the TXT files you want to modify in a single folder (or run the macro
on the folders where the TXT files currently lie). Note that as written it
will modify ALL files with the TXT extension in the current folder.

Sub BatchProcess()
Dim strFileName As String
Dim strPath As String
Dim oDoc As Document
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

With fDialog
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , _
"List Folder Contents"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" _
Then strPath = strPath + "\"
End With

If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(strPath, 1) = Chr(34) Then
strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strFileName = Dir$(strPath & "*.txt")

While Len(strFileName) <> 0
Set oDoc = Documents.Open(strPath & strFileName)
'
'Do what you want with oDoc
oDoc.Range.InsertAfter vbCr & oDoc.FullName
'
oDoc.Close SaveChanges:=wdSaveChanges
strFileName = Dir$()
Wend
End Sub

http://www.gmayor.com/installing_macro.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

You are welcome :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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