By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,496 Members | 1,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,496 IT Pros & Developers. It's quick & easy.

Headers and footers

P: n/a
I am trying to save and print reports of calculations of a program that make
adjustments. Doing that for a Word Document is fine because we can format the
text from VB .NET.

I have already all the results in a word document, but I would like to put a
Header and a Footer and page numbers on the document. However I don’t have
the finest idea how to make that!!

From VBA I can do that, but the instructions that put Header and Footer
automatically in VBA don’t work in VB .NET.

Do anyone now's how to do this from VB .NET?


Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a

First, you need to get a reference to the word document, then you just use
the automation stuff as you would with VBA, but with some VB.NET syntax.
This example will create a new document and add peage numbers to the header,
aligned to the right. Make sure you add a reference to the Word object
library to your project and import the library at the top of your code.
btw: Word documents have headers and footers by default, they are just not
visible :).
Imports System.Runtime.InteropServices
Imports Word
Dim theWordApplication as Word.Application
Dim theDocument as Word.Document


' Create a new word application

theWordApplication = New Word.Application

' Add a new document, based on this template

theDocument =
m_WordApplication.Documents.Add(Template:=CType(__ whatever_the_template_it_if_you_want_one__,

' Make the application invisible

theWordApplication.Visible = False ' Make this true to see results while

' Add page numbers to the document.

Dim wordHeaderIndex As Word.WdHeaderFooterIndex =

theWordApplication.Sections.Item(1).Headers.Item(w ordHeaderIndex).PageNumbers.Add
( _

Word.WdPageNumberAlignment.wdAlignPageNumberRight, _
FirstPage:= True )

Catch Ex As Exception

' Failed.


' One thing about Interop, always Try/Catch and cleanup references.


End Try
Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.