Write MS Word Document Header and Footer with vb.net

Good Morning everyone. Well I knew yesterday I didn’t have to work today. I went to bed knowing that. Of course I woke up at 5:00AM anyway and could not go back to sleep. I can be so driven by habit sometimes.

First some unpleasant business I have to attend to. Many times when I post code samples I assume you have a basic knowledge of .NET. If you don’t, you probably should not be using these samples. I write as a reference, a starting point if you will, for you to go from. Just copying and pasting is not going to help you. Yesterday, I got a message from a person upset I didn’t declare something that was in the code in one of my submissions which it was quite obvious how to do it. I helped him of course but it got me to thinking about why I do this blog. Truth is, I do it for me. It helps keep me sharp and thinking about new things. I will work on trying not to be so sloppy in the future. But cut me a little slack ok?

Today I am going to discuss how to write a MS Word document header  and footer with vb.net. It actually isn’t as simple as it might sound. You have to declare the variables or convert their values (I demonstrate below) used in the object ahead of time or you will generate an error. This ain’t vba folks.  Anyway, make it a great day!

Join me on Facebook

Dim oDoc As Word.Document
‘first lets get to the header of the document
oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = CInt(Word.WdSeekView.wdSeekCurrentPageHeader)
‘Typing something in the header
oDoc.Content.Application.Selection.TypeText(Text:="Martens ")
‘adding the page contents to the header
oDoc.Content.Application.Selection.Fields.Add(Range:=oDoc.Content.Application.Selection.Range, Type:=CInt(Word.WdFieldType.wdFieldEmpty), Text:="page")
‘return to the main document       
oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = CInt(Word.WdSeekView.wdSeekMainDocument)

For the footer

oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
oDoc.Content.Application.Selection.TypeText(Text:= _
‘return to the main document       
oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = CInt(Word.WdSeekView.wdSeekMainDocument)



  1. #1 by Omar on January 2, 2009 - 8:55 am

    That\’s a terrible example! What if your name is not \’Marten"? Then what are you supposed to do?Thanks very much it was very useful,Marten

  2. #2 by Kelly on January 3, 2009 - 2:25 pm

    sorry you didn\’t like the example. of course you could choose not to use it.

  3. #3 by Omar on January 14, 2009 - 9:41 am

    My failed attempt at sarcasm. It\’s a great example and I really appreciate you posting it.

  4. #4 by Ken on February 27, 2009 - 8:30 pm

    Could you add a little more about adding content and formatting to the document? I am a noob put picking up fast. Thanks for taking the time to write that in!

  5. #5 by Kelly on February 27, 2009 - 9:06 pm

    There are other blog entries with such info on my blog.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: