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!

Need help to Edit MS Word doc header text from Access

Status
Not open for further replies.

TrollBro

Technical User
Sep 4, 2004
98
US
Hi

I have an Access2003 database I use to open a MS Word template (its a form users will complete when sent out)and fill in certain bookmarked areas with info from a query. - works great. However, I also need to edit the header and footer (same on all pages) with some of the data as well, but I cannot figure out how to do it.

My code is below. Any ideas would really help - seems like it should be something simple - been working all day on this with no luck.

Thanks


Dim oApp As Object 'Variable for Word
Dim sFilename As String 'Variable for Auto-Save file name
Dim strTemplateName As String 'Variable for Word Template to be used
Dim objWORDdoc As Object
strTemplateName = "C:\Documents and Settings\folderx \MASTERTEMPLATE_auto2.dot"

sFilename = "C:\Documents and Settings\folderx\SAW_SummaryTemplate_v0309ra_BCS" & vBCS & "_H2.doc"

Set oApp = CreateObject("Word.Application")
oApp.Visible = True

If Dir(sFilename) = "" Then 'Test to see if created filename already exists
'and if not, open Template to fill in date
Set oApp = CreateObject("Word.Basic") 'otherwise just open that filename already
With oApp

.filenew Template:=strTemplateName
.EditBookmark Name:="TextVBA2", GoTo:=True
.Insert vSubCat
.filesaveas Name:=sFilename

End With

Else 'If filename already exists, just open the file at this point

oApp.Documents.Open sFilename
oApp.ActiveDocument.Save

End If

Dim oHF As HeaderFooter
Dim var
Dim HeaderText
Dim FooterText
HeaderText = vSomeRecordSpecificID 'been texting with simple "string"
FooterText = vFilename 'been texting with simple "string"

'BLOWS UP HERE:
oApp.Documents.Open.Sections.Headers.Range.Delete
oApp.Documents.Open.Sections.Headers.Range.Text = HeaderText
oApp.ActiveDocument.Save


 
What about this ?
Code:
oApp.ActiveDocument.Sections(1).Headers(1).Range.Text = HeaderText
oApp.ActiveDocument.Save

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV

I could not get that work when I tried that before. I recorded a macro in WORD that I cut a placed in a separate module that seems to work, but when I place it in my main code where it actually needs to run, I get errors.

CODE:

Dim oApp As Object 'Variable for Word
Dim Sfilename As String 'Variable for Auto-Save file name
Dim strTemplateName As String 'Variable for Word Template to be used
Dim objWORDdoc As Object


'Create path to Template
strTemplateName = "C:\Myfolder\mytemplate.dot"

'Create default SaveName for New SAW summary template
Sfilename = "C:\Myfolder\mydocument.doc"

Set oApp = CreateObject("Word.Application")
oApp.Visible = True

Set oApp = CreateObject("Word.Basic")
With oApp

.filenew Template:=strTemplateName

.EditBookmark Name:="TextVBA7", GoTo:=True
.Insert vStory

.filesaveas Name:=Sfilename

end with

'AT THIS POINT THE DOC IS VIEWABLE WITH THE CORRECT FILENAME
'However I cannot make it the active doc. So The code that follows generates an error: "The command is not available because no document is open" (but it IS open on my desktop)

'How can I set the focus to the new document that I can see but evidently not touch?

'Thanks!


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.EndKey Unit:=wdLine
Selection.TypeText Text:=vBCS
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument







 
You instantiate Word twice. Once with the method I'm using (through CreateObject("Word.Application")), then through another method.

I'm thinking this will give you two instances of Word in memory, and referencing as you do, might give even more instances of Word in memory, as it is implictit.

I'm more into using object variables for each kind of object I'm working on, so..

Aren't the way you're referring to bookmarks kind of "olden"?

[tt]dim oDoc as object

'Create path to Template
strTemplateName = "C:\Myfolder\mytemplate.dot"

'Create default SaveName for New SAW summary template
Sfilename = "C:\Myfolder\mydocument.doc"

Set oApp = CreateObject("Word.Application")
oApp.Visible = True

' Set oApp = CreateObject("Word.Basic")

oDoc = oApp.Documents.Add(strTemplateName)
with oDoc
If .Bookmarks.Exists("TextVBA7") Then
.Bookmarks("TextVBA7").Range.InsertAfter vStory
End If
.Sections(1).Headers(1).Range.Text = HeaderText
oDoc.SaveAs Sfilename

end with[/tt]

Roy-Vidar
 
Thanks Roy-Vidar


When it get to here:

oDoc = oApp.Documents.Add(strTemplateName)

It generates an error message "object var or with block var not set"

Any ideas?


Code:
Dim oApp As Object 'Variable for Word
Dim Sfilename As String 'Variable for Auto-Save file name
Dim strTemplateName As String 'Variable for Word Template to be used
'Dim objWORDdoc As Object


Dim oDoc As Object


'Create path to Template
strTemplateName = "C:\Myfolder\Mytemplate.dot"

'Create default SaveName for New template
Sfilename = "C:\Myfolder\Mydoc.doc"

Set oApp = CreateObject("Word.Application")
oApp.Visible = True

oDoc = oApp.Documents.Add(strTemplateName)
With oDoc
If .Bookmarks.Exists("TextVBA7") Then
.Bookmarks("TextVBA7").Range.InsertAfter vStory
End If
.Sections(1).Headers(1).Range.Text = "HeaderText" & vBCS
oDoc.SaveAs Sfilename

End With
 
[!]Set[/!] oDoc = oApp.Documents.Add(strTemplateName)

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

Thanks - That worked, but now the logo (image) in the header gets deleted, and I need to format the text in arial 8 bold grey.

So much closer than I was - but not sure on the logo and formatting.

Thanks!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top