Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pull Header Information from a text file? 2

Status
Not open for further replies.
Sep 12, 2006
111
US
Does anyone know of a way to setup a header in a word file that reads its header text from a txt file?

We have a system in place that contains around 277 individual MS Word files in one folder. A user will go into this folder and copy out the individual word files they need for their project and paste them into their project folder. However, they have to go into each of the word files and change the header information to match their project info.

We have found a $30 MS Word add-in that works well to change the header information on all the douments at once, but I was hoping to setup soemthing that would allow them to enter the header information in a text file in their working directory and have it update the MS word files automatically.

Any one know if this can easily be done?
 

You can record a Macro in Word of how to modify the header:[tt]
1. Have Word runnig, but no Files open. (Start recording)
2. Open first Word doc.
3. Modify the header.
4. Save the file and close it.
5. open second file.
6. Repeat[/tt]
Then you can go to the VBA code (Alt-F11) and modify the code so it will change the header in all Word docs in that folder. To access file after file in the same location - check VBA's Dir function.

When you get stuck - ask for more help.


Have fun.

---- Andy
 



fsupport1600,

I notice that over the past 3 1/2 years, you have posted 42 threads and have received many good tips related to your stated needs. Yet, you have responded NOT ONCE, to
[blue]
Thank Tek-Tip Contributor
for this valuable post!
[/blue].

The [purple]little purple Stars[/purple] accomplish several important things.

First, it gives positive feedback to contributors, that their posts have been helpful.

Second, it identifies threads as containing helpful posts, so that other members can benefit.

And third, it identifies the original poster (that's YOU, BTW), as a grateful member, that not only receives, but is willing to give tokens of thanks.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Skip,

Thank you for making me aware of this. For whatever reason, I never realized or seen the link to "Thank "Screenname" for this valuable post!"

I have no problem doing that from now on. I should take the time and go back to my previous post and give the credit where its needed.
 
You could also use an INCLUDETEXT field in your header. This can point to your text file.

Gerry
 


Ahh, Gerry!

Great Tip! Save youself from texual embarrassment.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Gerry,

I was looking into that option as well. However, we have a default location where the word files are copied from.

Once copied they go into their respective project directory. Copying the word files and the .txt file from the default directory to the project folder is it going to break the link to the text file?

If is is their a way to keep the INCLUDETEXT field linked to the text file no matter where the word files are copyed to as long as the txt file resides in the same directory as the word files?
 

The best way to see if this will work is.... to try it.

Have fun.

---- Andy
 
No, you can not maintain a link per se. HOWEVER, you could make it dynamic, that is, insert the INCLUDETEXT field extracting the path from the active document.

First of all you need to understand what Word does to the given path when you insert an INCLUDETEXT.

Say you insert an INCLUDETEXT field (via the Insert > Field menu) and you point it to:

c:\zzz\WhatEver.txt

What Word does is have the field code of the INCLUDETEXT as:

{ INCLUDETEXT "c:\\zzz\\WhatEver.txt" \* MERGEFORMAT }

Note the double slashes.

Now suppose you have a Word document - c:\zzz\ThisWhatever.doc.

The point being is that the folder the Word document is in is NOT relevant. You can extract it, and use it to ALSO get the path to the given text file (assuming it IS in the same folder!!). Like this:
Code:
Sub StickTextIntoHeader()
Dim strDocPath As String
Dim strFile As String
Dim r As Range

[COLOR=red]' get the document path[/color red]
strDocPath = ActiveDocument.Path & "\"

[COLOR=red]'replace the slashes with double slashes[/color red]
strDocPath = Replace(strDocPath, "\", "\\")

[COLOR=red]' build the field code string[/color red]
strFile$ = "INCLUDETEXT  " & Chr(34) & _
      strDocPath & "WhatEver.txt" & Chr(34)

[COLOR=red]' set the header range object[/color red]
Set r = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
   .Range
   
[COLOR=red]' add the INCLUDETEXT field to header range[/color red]
r.Fields.Add Range:=r, Type:=wdFieldEmpty, _
      Text:=strFile$, PreserveFormatting:=True
End Sub
Of course even the name of the text file can be a variable as well.

So you could a generic procedure (in a global template, or Normal if you have to...) that could be executed from ANY Word document, that grabs a text file name from a InputBox, and puts that text file content into any header. (NOTE: I used the primary header for section 1, but it could be for any header.)
Code:
Sub StickTextIntoHeader()
Dim strDocPath As String
Dim strFile As String
Dim r As Range

strDocPath = ActiveDocument.Path & "\"
strDocPath = Replace(strDocPath, "\", "\\")

strFile$ = "INCLUDETEXT  " & Chr(34) & _
      strDocPath & [b]InputBox("Name of text file?")[/b] & Chr(34)


Set r = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
   .Range
   
r.Fields.Add Range:=r, Type:=wdFieldEmpty, _
      Text:=strFile$, PreserveFormatting:=True
End Sub

Gerry
 
Of course you could even extend this into NOT having the text files in the same folder. Although it certainly is easier if they are in the same folder. Nevertheless, you could also get a folder name AND the text file name, and still build the INCLUDETEXT field text string.
Code:
[COLOR=red]' the function to get the text file
' path and name[/color red]

Function GetFileName(FileFilter As String, _
   ReturnPath As Boolean, _
   ReturnFile As Boolean) As String
[COLOR=red]' returns the folder and/or filename to a single user selected file[/color red]
Dim strFileName As String, strPathName As String
    If Not ReturnPath And Not ReturnFile Then Exit Function
       If FileFilter = "" Then FileFilter = "*.txt*"
    With Application.Dialogs(wdDialogFileOpen)
        .Name = FileFilter
        On Error GoTo MultipleFilesSelected
        If .Display = -1 Then
            strFileName = .Name
        End If
        On Error GoTo 0
    End With
    On Error GoTo 0
[COLOR=red]    ' remove any "-characters[/color red]
    If InStr(1, strFileName, " ", vbTextCompare) > 0 Then
        strFileName = Mid$(strFileName, 2, Len(strFileName) - 2)
    End If
    If ReturnPath Then
        strPathName = CurDir & Application.PathSeparator
    Else
        strPathName = ""
    End If
    If Not ReturnFile Then strFileName = ""
    GetFileName = strPathName & strFileName
MultipleFilesSelected:
End Function

[COLOR=red]' the Sub to insert the INCLUDETEXT field[/color red]
Sub StickTextIntoHeader2()
Dim strFile As String
Dim strTextFile As String
Dim r As Range

strTextFile = GetFileName("", True, True)
strTextFile = Replace(strTextFile, "\", "\\")

strFile = "INCLUDETEXT  " & Chr(34) & _
      strTextFile & Chr(34)

Set r = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
   .Range
   
r.Fields.Add Range:=r, Type:=wdFieldEmpty, _
      Text:=strFile$, PreserveFormatting:=True
End Sub
Now (if this is what you wanted, or had) you could get the text file from ANY folder.


BTW: you do NOT have to click the OK button in the file/folder dialog. All you have to do is navigate to the folder with the text file and double click the file. Voila! It is inserted as an INCLUDETEXT field in the header.

Gerry
 
And lastly - really all this should be in the VBA forum, not here in the Office forum - it must be noted that all of the above examples replaces the header content.

If there is existing content that you wish to keep, you would have to adjust (Collapse) the range object accordingly.

This appends the INCLUDETEXT field at the end.
Code:
Set r = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
   .Range
With r
   .Collapse 0
   .Fields.Add Range:=r, Type:=wdFieldEmpty, _
      Text:=strFile, PreserveFormatting:=True
End With

This appends the INCLUDETEXT field at the end on new line - as a new paragraph.
Code:
Set r = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
   .Range
With r
   .Collapse 0
   .InsertParagraphAfter
   .Collapse 0
   .Fields.Add Range:=r, Type:=wdFieldEmpty, _
      Text:=strFile, PreserveFormatting:=True
End With

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top