Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>AFAIK to alter a documents header (simply) must be done by having the document open. That said you have a few options. First if the documents are saved in the office XML format then you could open the files using the MSXML library and alter the data in the header. (Or any of the dozens of other ways to alter what is essentially a text file.) If the file(s) are still in the binary format you really only have one of two options. The first is to open the file via vba and alter the header via the document object model. The second would be to figure out the binary format (which is documented) and alter it using the VB6/VBA native binary IO (very non-trivial). </p> <p>Unless I thought I could gain more time then I was going to lose writing code to alter the documents directly I would probably just loop through all the file in the folder, open them and alter them. As for storing the header somewhere... You could just put the header data in a text file and pull it in. Or keep a document template somewhere. </p> <p>Here is a very trivial example:</p> <pre><code>Public Sub Example() Dim asFiles() As String Dim lFile As Long Dim docCrnt As Word.Document asFiles = GetFiles("C:\Test\", "*.doc") For lFile = 0&amp; To UBound(asFiles) Set docCrnt = Word.Documents.Open(asFiles(lFile)) docCrnt.Windows(1).View.SeekView = wdSeekCurrentPageHeader Selection.Text = "I am the header." docCrnt.Close True Next End Sub Public Function GetFiles( _ ByVal folderPath As String, _ Optional ByVal pattern As String = vbNullString _ ) As String() Dim sFile As String Dim sFolder As String Dim asRtnVal() As String Dim lIndx As Long If Right$(folderPath, 1&amp;) = "\" Then sFolder = folderPath Else sFolder = folderPath &amp; "\" End If sFile = Dir(sFolder &amp; pattern) Do While LenB(sFile) ReDim Preserve asRtnVal(lIndx) As String asRtnVal(lIndx) = sFolder &amp; sFile lIndx = lIndx + 1&amp; sFile = Dir Loop If lIndx = 0&amp; Then ReDim asRtnVal(-1&amp; To -1&amp;) As String End If GetFiles = asRtnVal Erase asRtnVal End Function </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload