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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding text to a header in a word document 1

Status
Not open for further replies.

PortyAL

Technical User
May 13, 2005
126
GB
Hi

I use the following code to create a Word document from Access.

Code:
 Set objword = New Word.Application

    With objword
      .Visible = True
      .Documents.Add Template:=("i:\ia manual\templates\quality assurance review.dot")
      .Selection.Goto Name:=("job")
      .Selection.TypeText Text:=(Forms![frm recs tracked jobs]![Job])
      .Selection.Goto Name:=("auditor")
      .Selection.TypeText Text:=(Forms![frm recs tracked jobs]![AuditorName])
      .ActiveDocument.SaveAs (QARname)
      .Quit
    End With

"job" and "auditor" are bookmarks saved in the body of the Word template. This works fine.

However, I have another Word document I want to create which has a table in the page header. I want to insert the [AuditorName] and [Job] as above into this table. I have created bookmarks in the table but as it is in the page header ".Selection.Goto Name" will not work, as it can't find the bookmark.

I tried various things e.g.

Code:
.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
.Selection.TypeText Text:=(Forms![frm recs tracked jobs]![Job])
.Selection.Goto Name:=("Job")

but all I get is "Run-time error 5678. Word cannot find the requested bookmark."

I then recorded the procedure as a macro in Word:

Code:
Sub Macro2()

    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.GoTo What:=wdGoToBookmark, Name:="Dept"
    With ActiveDocument.Bookmarks
        .DefaultSorting = wdSortByName
        .ShowHidden = True
    End With
    Selection.TypeText Text:="gg"
End Sub

I tried inserting the relevant parts of this in the Access module but got the same error. (Incidently, running the above macro in the word document also results in a bookmark not found error.)

Any advice would be greatly appreciated.

AL
 
try
Code:
 .ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    .Selection.TypeText Text:=""
    .ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    .Selection.GoTo What:=wdGoToBookmark, Name:="job"
 
Thanks, but it still gives the same "Cannot find bookmark" error message.

AL
 
Some further info. The line below in the code by pwise does select the header in the Word doc.

Code:
.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

In Word if I choose Insert Bookmark and hit Go To it does select the bookmark. I recorded this process in Word and it gave what pwise suggested:

Code:
Selection.GoTo What:=wdGoToBookmark, Name:="Job"

However, even when I run the saved macro from Word I get the same "Cannot find bookmark" error. All very strange!

AL
 
I have bookmarks set in the header and can navigate to them in Word by choosing View - Header/Footer, and then Insert - Bookmark - Go To.

The problems start when trying to do it automatically.

AL
 
You may try this:
Code:
.ActiveDocument.StoryRanges(wdPrimaryHeaderStory).Bookmarks("job").Select
.Selection.TypeText Text:=(Forms![frm recs tracked jobs]![Job])
If you have PageSetup with Different first page you may try to replace wdPrimaryHeaderStory with either wdFirstPageHeaderStory or wdEvenPagesHeaderStory

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi

Only getting back to this now.

Thanks. This works a treat!

AL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top