How to fill header, body and footer using VBA?

M

Mark Findlay

In the past, I have used vba (Javascript) from a webpage to auto-load a word
doc and print it from a web page, but now the task has grown in complexity:

Still using vba/javascript: I need to:
1) Read a database for certain fields (I know how to do that)
2) Open an existing MSWord doc (we'll call it Doc1) using vba/javascript: (I
can do that too)

.... this is where it gets more complicated:

3) Open a 2nd MSWord doc file (we'll call it Doc2) that will serve as a
'template'.
3a) This 'template' doc file has a header and a footer defined, that have
keywords in it to be replaced by the database fields read in step 1.
4) The main body content of the template file is to be the MSWord doc read
in step 2 above.
5) Save the 'template' doc (Doc2) to disk as a new filename so that the
template remains unchanged.

The idea here, is that the admin of the website wants to be able to take
MSWord docs that users have submitted, and place the documents into a
standardized format containing a custom header and footer so that all
documents adhere to a standard.

This is for an intranet where all users will be using IE6, and MS Office
2000 and 2003.

Thanks for any help you can provide!

Mark
 
C

Charles Kenyon

I'm not sure I understand why you don't want to use a real template with
your headers and footers established the way you want them. Create a new
document based no the template, copy or insert your text.
 
P

Peter Hewett

Hi Mark Findlay

You need to create a base template that contains the appropriate Header/Footer
information. You can then use that template to create further templates or documents.
That way your standard Header/Footer will propagate to all subsequent documents.

HTH + Cheers - Peter
 
M

Mark Findlay

Hi Charles,

I'm sorry I wasn't clearer, but I am looking for an automated solution that
will allow an admin to view a list of documents via a webpage, then at the
click of a button, have all the documents in the list automatically
formatted with a custom header and footer.

Using vba, I have in the past done simple things like open the documents and
print them, but this now involves creating new documents out of the existing
documents - the new documents will contain a header and footer, that has
certain keywords in the header replaced by database values. (I will know
which database record to read based on the filename of the doc to be
converted).

So it will look like this:

user submitted doc 1
user submitted doc 2
user submitted doc 3
|
|
v
---------------------
Admin views filenames
in webpage and invokes
vba function to perform <----- vba reads in template doc with special
header and footer, merges user submitted docs.
reformatting of each doc.
-----------------------
|
|
v
converted doc 1
converted doc 2
converted doc 3

I expect that the vba will read in a static template that is a mail merge
template with the fields for merge already defined, then I would have to
ensure that the table was filled in advance with a single record to fulfill
the mail merge, then I would need to find a way to merge the entire 'user
submitted doc 1' into the template's body, then save the doc under a new
name.

Of course that is just my rough guessing as I haven't ventured into that
territory before, so I am open to all suggestions.

Thanks!
Mark
 
W

Wei-Dong XU [MSFT]

Hi Mark,

For automate altering the header and footer, we can access the range object of header and footer. Then according to your scenario, you can
decide how to change the content in the range object. I write one sample code for you.

'sample start--------------------------------------------------
Sub AlterHeaderFooter()

Dim objApp As Word.Application
Dim objDoc As Document
Set objApp = CreateObject("word.application")
Set objDoc = objApp.Documents.Open("<path>\template.doc")

With objDoc.Sections(1)
'please change the code here for the modification of your own
'header and footer
.Headers(wdHeaderFooterPrimary).Range.Text = "Header"
.Footers(wdHeaderFooterPrimary).Range.Text = "Footer"
End With

' insert the copy other doc content here. I have seen the sample
'...

'generate the interger between 9999 and 1 for the new file name
'or change it according to your scenario
Randomize
objDoc.SaveAs FileName:="<path>" & Int(10000 * Rnd) & ".doc"
objDoc.Close savechanges:=False
objApp.Quit
End Sub
'sample end---------------------------------------------------

Please feel free to let me know if you have any further questions.

Best Regards,
Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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