programatically setting the footer of a word document

D

darshan

Hi,

I am new to VBA, so would appretiate some help.
I am trying to programatically create many word documents from VB.
I have a word template with a few form fields in it, which I open
and substitute whatever I want to into the form fields and save the doc.
(all this i do in a loop)

1. I am not being able to put a form field in the footer.
2. Alternatively, I would also like to set the footer of the word document.
But I am not being able to do that too.
Can anyone pls. tell me how to set the footer of a word document
from a VB code ?

I tried doing some recording which gave me the foll. code

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.TypeText Text:="Header that I want to set"
Selection.MoveDown Unit:=wdScreen, Count:=3
Selection.TypeText Text:="Footer Left text" & vbTab & "Footer center" & vbTab
NormalTemplate.AutoTextEntries("Page X of Y").Insert Where:=Selection. _
Range, RichText:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument



On putting this code in the VB form, I am getting an error saying that
Command not available since no word document is open !
(in the vb code, the word application instance is set to NOT visible)

Thanks in advance
Darshan
 
J

Jezebel

First, instead of using form fields use DocProperty fields. These are easier
to update, and you can have them in your headers and footers also.

To set the header and footer from code, use something like

with ActiveDocument.Sections(1)
.Headers(wdHeaderFooterPrimary).Range.Text = "Header that I want to set"

with .Footers(wdHeaderFooterPrimary).Range
.Text = "Footer Left text" & vbTab & "Footer center" & vbTab
.Fields.Add Range:=ActiveDocument.Range(.End, .End),
Type:=wdFieldPage
....
end with
end with


Don't bother with all that Panes and views stuff.
 
D

darshan

Jezebel said:
First, instead of using form fields use DocProperty fields. These are easier
to update, and you can have them in your headers and footers also.

To set the header and footer from code, use something like

with ActiveDocument.Sections(1)
.Headers(wdHeaderFooterPrimary).Range.Text = "Header that I want to set"

with .Footers(wdHeaderFooterPrimary).Range
.Text = "Footer Left text" & vbTab & "Footer center" & vbTab
.Fields.Add Range:=ActiveDocument.Range(.End, .End),
Type:=wdFieldPage
....
end with
end with


Don't bother with all that Panes and views stuff.

Thanks for your reply Jezebel,
I just wanted to confirm weather ActiveDocument will work
when I am creating a word doc from VB and the word application
instance is set to NOT visible.

I was getting an error on the previous code snippet pasted
(the one i got after recording) and the error said that
command not available as no document open....so i think
objects like ActiveWindow, ActiveDocument may not be available.

Pls tell me if I am right.

Thanks once again
Darshan
 
S

Subhash

Jezebel said:
First, instead of using form fields use DocProperty fields. These are easier
to update, and you can have them in your headers and footers also.

To set the header and footer from code, use something like

with ActiveDocument.Sections(1)
.Headers(wdHeaderFooterPrimary).Range.Text = "Header that I want to set"

with .Footers(wdHeaderFooterPrimary).Range
.Text = "Footer Left text" & vbTab & "Footer center" & vbTab
.Fields.Add Range:=ActiveDocument.Range(.End, .End),
Type:=wdFieldPage
....
end with
end with


Don't bother with all that Panes and views stuff.

Hi,
I am also doing something similar.
I have not used DocProperty as of date.
I just read a bit and found that on doing a Ctrl+F9,
you can insert conditional text,
but I don't know how that will help me.

I just want a place holder in the header/footer,
which can be given a value from VB code.
The logic is all there in the VB code and cannot
stay in the template/word document.

I also could NOT find how custom DocProperty elements
can be defined.

Can some please give me a detailed reply ?

-Subhash
 
J

Jezebel

If you're working from VB, rather than using ActiveDocument use a reference
to the actual document. If your VB app is opening it, it will be something
like

Dim pDoc as Word.Document
Set pDoc = WordApp.Documents.Open(FileName)
With pDoc
.....

If the document is already open you can use Set pDoc =
WordApp.Documents(Index) where Index is the name or collection index --
the most recently opened document is Documents(1).
 
J

Jezebel

Ctrl-F9 inserts an empty field. Use Alt-F9 to toggle the display of field
codes and field results. This is not necessarily conditional text -- it is
text or document automation in various forms. If you don't know about Word
fields, read Help on it and experiment with Fields on the Insert menu.

A Word Document has two collections of properties: BuiltinDocumentProperties
(mostly the things you see on the File > Properties > Summary dialog --
Title, subject, etc) and CustomDocumentProperties (anything you can see on
the File > Properties > Custom dialog).

You can display some built-in document properties within the document by
adding a field (ctrl-F9) that has just the property name, eg { Title } or
{ Subject }. You can display CustomDocumentProperties by using { DocProperty
MyProperty }. Try adding a custom property called 'ID' with value 'ABC123'.
Now add docproperty fields: { DocProperty ID } anywhere in your document. If
you change the property value, you need to update fields by selecting and
pressing F9.
 
D

darshan

Jezebel said:
If you're working from VB, rather than using ActiveDocument use a reference
to the actual document. If your VB app is opening it, it will be something
like

Dim pDoc as Word.Document
Set pDoc = WordApp.Documents.Open(FileName)
With pDoc
.....

If the document is already open you can use Set pDoc =
WordApp.Documents(Index) where Index is the name or collection index --
the most recently opened document is Documents(1).

(pls don't feel annoyed by my repeated questioning
but as i am new here, i need a bit of help)

Yes, That is exactly the thing i am asking
How can i access the footer of a doc (i mean i dunno the object
hierarchy)

i.e.

Dim pDoc as Word.Document
Set pDoc = WordApp.Documents.Open(FileName)
With pDoc
.Headers(wdHeaderFooterPrimary).Range.Text = "Header that I want
to set"

with .Footers(wdHeaderFooterPrimary).Range
.Text = "Footer Left text" & vbTab & "Footer center" & vbTab
.Fields.Add Range:=ActiveDocument.Range(.End, .End),
Type:=wdFieldPage
....
end with
end with


AGAIN THERE IS AN ACTIVEDOCUMENT INTHE WITH....ENDWITH

will it work ? esp when the word application is set to NOT visible.

Even if u give me pointers to documents / MSDN articles
where i can read up, i would be very grateful .

Thanks once again,
Darshan
 
J

Jezebel

darshan said:
"Jezebel" <[email protected]> wrote in message

(pls don't feel annoyed by my repeated questioning
but as i am new here, i need a bit of help)

Yes, That is exactly the thing i am asking
How can i access the footer of a doc (i mean i dunno the object
hierarchy)

i.e.

Dim pDoc as Word.Document
Set pDoc = WordApp.Documents.Open(FileName)
With pDoc
.Headers(wdHeaderFooterPrimary).Range.Text = "Header that I want
to set"

with .Footers(wdHeaderFooterPrimary).Range
.Text = "Footer Left text" & vbTab & "Footer center" & vbTab
.Fields.Add Range:=ActiveDocument.Range(.End, .End),
Type:=wdFieldPage
....
end with
end with


AGAIN THERE IS AN ACTIVEDOCUMENT INTHE WITH....ENDWITH

will it work ? esp when the word application is set to NOT visible.


You need to replace ALL the references to ActiveDocument with your own
document reference (pDoc or whatever).

Headers and footers belong to Sections of a document, not to the document
itself.

Learn the object model! The Object Browser maps it out for you, or
experiment with the Intellisense options.
 
F

Francis Dion

Word automation is a powerful tool to generate documents but it is
also inherently vulnerable to many configuration issues. There are
many conditions that can make your code fail, and code that works fine
on one machine might not on another one.

You might want to investigate XpertDoc as an alternative solution. It
creates genuine MS-Word documents without making any call to the
MS-Word application, making it more reliable, much faster and easier
to deploy and support.

XpertDoc allows you to embed the logic for your code right into your
MS-Word template, turning it into a powerful wysiwyg tool. You can put
XpertDoc field expressions into footnotes, header, footers, tables,
etc.

You can check it out at www.xpertdoc.com.
 

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

Top