Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automate Header Footer Manipulation 2

Status
Not open for further replies.

LJtechnical

Technical User
Aug 7, 2002
90
0
0
GB
Hi All,

I am trying to access the header and footer of a word template as it is opened in order to automate the input of text from an access table. I have the following code which works fine when I step through the code but not in the automation:

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If

If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Or ActiveWindow.ActivePane.View.Type _
= wdMasterView Then
ActiveWindow.ActivePane.View.Type = wdPageView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

All this gives, is the ever helpful "object variable or with block variable not set" any ideas?

Thanks

Dave

 
Hi LJtechnical,

I can't duplicate your problem. Can you give some more details. All I can think is that you're trying to run it before you have a Window initialized. Where is the code and when does it run?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
You do NOT have to open any panes at all to access header or footer information/text.

You can read any text in any header, or write text in any header, directly, using a Range object of the Sections.Headers(index).

There are ALWAYS three headers in each section.

wdHeaderFooterPrimary
wdHeaderFooterFirstPage
wdHeaderFooterEvenPage

There is NO odd page header object.

The header object wdHeaderFooterPrimary is always the first header text entered in a Section, regardless of whether it entered from an even or odd numbered page, and will ALWAYS be the header for an odd page afterwards.

Headers/Footers are stored in the Section object, and there are always three of them.

Code:
ActiveDocument.Sections(4).Headers(2).Range.Text = _
   "Section Four - First Page"

will write the text "Section Four - First Page" into the wdHeaderFooterFirstPage object's Range.Text, in Section 4.

NOTE: if the Section is NOT setup with Different First Page, this will NOT return an error. It is a valid command, as it is a valid object. If the Section is NOT setup for Different first page, and the first page is blank, then all the headers in the section will be stilll be blank.

If you ever change the setup to Different first page, then the above code, if already run, will automatically update the headers to reflect what was input as wdHeaderFooterFirstPage.

If you are not going to use different headers (first, or odd/even), then:

Code:
ActiveDocument.Sections(4).Headers(1).Range.Text = _
   "Section Four - Whatever"

will write the text into wdHeaderFooterPrimary (ie. Headers(1)), for that Section - in this case the fourth Section.

To retrieve the text from any header:

Code:
Dim SectionFourHeader As String
SectionFourHeader = ActiveDocument.Sections(4).Headers(1).Range.Text

Note, however, as the Headers(index) = 1, what is returned is wdHeaderFooterPrimary. If you DO have the section setup with Different first page, and/or Different odd and even, then to return all the headers (and again there are always three), you would have to retrieve the values for each one.

Headers(1).Range.Text = wdheaderFooterPrimary (or in this case the odd page)

Headers(2).Range.Text = wdHeaderFooterFirstPage (the first page of the section)

Headers(3).Range.Text = wdHeaderFooterEvenPage (the even pages).

Just to be careful..there is NO odd page header, Primary (or index = 1)acts for everything if there is no setup to be different, but acts as Odd Page if the Section IS setup with different first, odd/even pages.

Whew.

Gerry
 
Hi Tony,

Here is the full code sequence, hope you can shed some light on it, should probabaly mention I'm using Office 97.

Private Sub Document_New()
'David Attwell of LJ Technical Systems 24/09/2004

On Error GoTo ErrorHandler

Dim dbe As dao.DBEngine
Dim wks As dao.Workspace
Dim dbs As dao.Database
Dim rst As dao.Recordset
Dim strOfficePath As String
Dim strDBName As String
Dim strOfficeVersion As String
Dim DepN As String
Dim DepR As String
Dim Lab As String
Dim MyVar
Dim A As Integer
Dim aDoc As Document
Set aDoc = ActiveDocument
A = 1
Dim wrdApp As Object
Dim myDoc As Document
Set myDoc = ActiveDocument
Set wrdApp = CreateObject("Word.Application")
ActiveWindow.ActivePane.View.Type = wdNormalView
MyVar = MsgBox("This action may take some time! Do you want to continue?", 4, "Lab Import")
If MyVar = 7 Then
MsgBox "Import Cancelled."
wrdApp.Quit
GoTo QuitProcedure
Else
strOfficePath = "K:\Projects\Kuwait PAAET Project\Equipment Database\" 'strOfficePath & "Samples\"
Debug.Print strOfficePath

'Get Office version from Word
strOfficeVersion = Application.Version
Debug.Print "Office version: " & strOfficeVersion

'Set up reference to correct versions of DAO and Access depending
'on Office version
strDBName = "KPDB.mdb"

If Left(strOfficeVersion, 1) = "8" Then
'Office 97 DAO version
Set dbe = CreateObject("DAO.DBEngine.35")
strDBName = strOfficePath & strDBName
ElseIf Left(strOfficeVersion, 1) = "9" Then
'Office 2000 DAO version
Set dbe = CreateObject("DAO.DBEngine.36")
strDBName = strOfficePath & strDBName
Else
MsgBox "Unknown Office version; canceling"
Exit Sub
End If

'Set up reference to Access database
Set wks = dbe.Workspaces(0)
Set dbs = wks.OpenDatabase(strDBName)

'Set reference to Access query containing data to import into Word
Set rst = dbs.OpenRecordset("Export Lab", dbOpenDynaset)
With rst

'Fill table with data from Access, row by row
'With rst
Selection.GoTo What:=wdGoToBookmark, Name:="Item1"
Do While Not .EOF
'Insert data into labels
Selection.TypeText Text:=![Item]
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=![Description]
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=![Quantity]
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=![KD]
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=![Fils]
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=![ExtKD]
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=![ExtFils]
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.HomeKey Unit:=wdLine
Selection.TypeText Text:=![Comment]
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.HomeKey Unit:=wdLine
DepN = ![Department]
DepR = ![Area Reference]
Lab = ![Lab Number]
.MoveNext
Loop
.Close
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Or ActiveWindow.ActivePane.View.Type _
= wdMasterView Then
ActiveWindow.ActivePane.View.Type = wdPageView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.MoveDown Unit:=wdLine
Selection.MoveDown Unit:=wdLine
Selection.MoveDown Unit:=wdLine
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=DepN
Selection.MoveDown Unit:=wdLine
Selection.TypeText Text:=DepR
Selection.MoveRight Unit:=wdCell
Selection.MoveUp Unit:=wdLine
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Lab
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End With

QuitProcedure:
wrdApp.Quit
Application.Quit SaveChanges:=wdPromptToSaveChanges
ErrorHandlerExit:
Exit Sub

ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If
End Sub
 
Hi LJtechnical,

As far as I can run your code without the template body or the database, it appears to work for me. What line do you get the error on?

I would, however, go with Gerry (I haven't read his post but I'm sure it's good) and access the Header via the Word Application rather than the Window (look at something like document.storyranges(wdprimaryheaderstory)).

Also, although it doesn't appear to cause a problem, you don't appear to be using the second instance of Word you are creating. What is the purpose of it?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Thanks to both of you! Between you, you sorted it out for me, just combined Gerrys well explained use of Section(#) with Bookmarks and had the perfect results. As follows:

ActiveDocument.Sections(1).Headers_(wdHeaderFooterPrimary).Range.Bookmarks_("BookmarkName").Range.Text = "Text String"

Have Stars!
 
Ah, I had not mentioned using bookmarks in headers. It was a long winded explanation as it was. But...as the Brits say...or used to...Hurrah! Yippee! as the Yanks say, and since I am neither, i will just say;

good. Bookmarks in headers/footers are a great tool. That way, you can directly reference them by name. Well, essentially that is what bookmarks are, named ranges.

Just be careful with how you handle replacing text in the bookmark. Often it will actually replace the bookmark.Start location, and thereby deleting itself.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top