Headers and footers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?

Thanks
 
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

Try

' 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__,
Object))

' Make the application invisible

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

' Add page numbers to the document.

Dim wordHeaderIndex As Word.WdHeaderFooterIndex =
WdHeaderFooterIndex.wdHeaderFooterFirstPage

theWordApplication.Sections.Item(1).Headers.Item(wordHeaderIndex).PageNumbers.Add
( _

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

Catch Ex As Exception

' Failed.

Finally

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

Marshal.ReleaseComObject(theDocument)
Marshal.ReleaseComObject(theWordApplication)

End Try
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Eliminate Group Footer 4
Lock Print Areas 1
Headers/footers 3
Header and Footer 1
Copy formatted text but not headers/footers 1
Excel Unique last page footer 1
Page Numbering 6
Disabling &[Time] macro in headers and footers 1

Back
Top