CPForecast
Programmer
Hello,
I have a Word XP macro that I am writing to add some dynamic information to a series of documents. One of the tasks is to recreate the headers and footers of each document based on the information for each specific report. For example, the report title, category, and month and year of publication for each report are pulled from a database and are to be added to the headers and footers. There is a different header and footer for the first page, even pages, and odd pages. We are moving to this new dynamic way up updating the headers and footers from having the information changed manually by the staff here.
I have attached a sample of what one of our documents looks like. I am having a few problems with this task that I can't seem to figure out. I'm far from being an expert VBA programmer, so I'm sure there are more efficient ways to do some of the things I'm trying to do. I looked around other forum posts and was able to solve a few of my problems, but additional assistance or insight to complete this task would be much appreciated. Thanks.
1. When the headers and footers get put in with the current code, the same page number gets added to each page (i.e, every page says "Page 1").
2. I am using the vbTabs to space out the information on the left and right-hand sides of the headers/footer, but it seems that because of the two columns in the document, the right-hand headers and footers aren't completely flushed against the right side of the document.
3. When these new headers and footers get put in, they don't have the formatting that the old headers and footers had. This is an imperative part of the process and I honestly am not sure how to get them to be formatted the way they're supposed to be.
Thanks again for any help.
I have a Word XP macro that I am writing to add some dynamic information to a series of documents. One of the tasks is to recreate the headers and footers of each document based on the information for each specific report. For example, the report title, category, and month and year of publication for each report are pulled from a database and are to be added to the headers and footers. There is a different header and footer for the first page, even pages, and odd pages. We are moving to this new dynamic way up updating the headers and footers from having the information changed manually by the staff here.
I have attached a sample of what one of our documents looks like. I am having a few problems with this task that I can't seem to figure out. I'm far from being an expert VBA programmer, so I'm sure there are more efficient ways to do some of the things I'm trying to do. I looked around other forum posts and was able to solve a few of my problems, but additional assistance or insight to complete this task would be much appreciated. Thanks.
Code:
Sub HeadersAndFooters(currentFields, year, Month As String)
Dim ThisDoc As Document
Dim oHF As HeaderFooter
Dim var
Dim HeaderText()
Dim FooterText()
Dim HeaderEven, HeaderOdd, FooterEven
Dim FooterOdd, HeaderFirst, FooterFirst
Dim FullMonth
Dim objSection As Section
ActiveWindow.View.ShowHiddenText = True
FullMonth = monthname(currentFields!PUB_MONTH_INT)
HeaderEven = "Page " + CStr(Selection.Information(wdActiveEndPageNumber)) + vbTab + _
vbTab + currentFields!BOOK_NAME + vbCrLf + currentFields!REPORT_TITLE
HeaderOdd = currentFields!BOOK_NAME + vbTab + vbTab + _
"Page " + CStr(Selection.Information(wdActiveEndPageNumber)) + vbCrLf + _
vbTab + vbTab + currentFields!REPORT_TITLE
HeaderFirst = currentFields!BOOK_NAME
FooterEven = FullMonth + " " + year
FooterFirst = "©" + year + vbTab + FullMonth + " " + year
FooterOdd = "©" + year + vbTab + FullMonth + " " + year
HeaderText = Array(HeaderOdd, HeaderFirst, HeaderEven)
FooterText = Array(FooterOdd, FooterFirst, FooterEven)
Set ThisDoc = ActiveDocument
For Each objSection In ActiveDocument.Sections
For Each oHF In objSection.Headers
oHF.Range.Delete
Next
For Each oHF In objSection.Footers
oHF.Range.Delete
Next
Next
With ThisDoc.Sections(1)
For var = 1 To 3
.Headers(var).Range.Text = HeaderText(var - 1)
.Footers(var).Range.Text = FooterText(var - 1)
With .Footers(var).Range
If var = 1 Or var = 2 Then
.InlineShapes.AddPicture "K:\DATA\IMAGES\filogo-nodate.emf", False, True
End If
End With
Next
End With
ActiveWindow.View.ShowHiddenText = False
End Sub
1. When the headers and footers get put in with the current code, the same page number gets added to each page (i.e, every page says "Page 1").
2. I am using the vbTabs to space out the information on the left and right-hand sides of the headers/footer, but it seems that because of the two columns in the document, the right-hand headers and footers aren't completely flushed against the right side of the document.
3. When these new headers and footers get put in, they don't have the formatting that the old headers and footers had. This is an imperative part of the process and I honestly am not sure how to get them to be formatted the way they're supposed to be.
Thanks again for any help.