Change the Header (or footer) of an MS Word document.
Someone asked me to change the headers of a couple of MS Word documents. I could not do it manually because there were more than 8000 documents.
So I wrote a little program. I used the MS Office object model.
Just get a reference to "Microsoft Office 9.0 Object Library"
Dim objMsword As New Word.Application
Dim objHeader As Word.HeaderFooter
Dim arDocfiles() As String
Dim strDocFile As String
objMsword =
New Word.Application
objMsword.Visible = False
Clipboard.SetDataObject(Image.FromFile("header.jpg"))
arDocfiles = IO.Directory.GetFiles("C:\docfiles", "*.doc")
For Each strDocFile In arDocfiles
Dim objworddoc As Word.Document
objworddoc = objMsword.Documents.Open(strDocFile)
objHeader = objworddoc.Content.Sections.First.Headers.Item(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary)
objHeader.Range.Paste()
objworddoc.Save()
objworddoc.Close()
objworddoc =
Nothing
Next
objMsword =
Nothing
- Ken