Replacing Text in a doc.... From within Access

C

Chris

I have a need to open a document from within MS Access and then do a replace
on individual items in the doc with text pulled from an Access query.

Where can I start in order to get this to happen?
I am currently using an example module in an old access help file (97) but
it has many thing wrong to begin with.

Private Sub Command2_Click()
Dim WordObj As Object
Dim WordDoc As Document
'Dim WordRng As Range
'Dim WordPar As Paragraph
Set WordObj = CreateObject("Word.Application")
With WordObj
.WindowState = wdWindowStateMaximize
.Documents.Add
Set WordDoc = WordObj.ActiveDocument

Set WordRng = WordDoc.Range
With WordRng
.Font.Bold = True
.Font.Italic = True
.Font.Size = 16
.InsertAfter "Running Word From Access Using Automation"
.InsertParagraphAfter
'Insert a blank paragraph between the two paragraphs
.InsertParagraphAfter
End With
Set WordPar = WordRng.Paragraphs(3)

With WordPar.Range
.Bold = True
.Italic = False
.Font.Size = 12
.InsertAfter "Report Created: "
.Collapse Direction:=wdCollapseEnd
.InsertDateTime DateTimeFormat:="MM-DD-YY HH:MM:SS"
End With
.ActiveDocument.SaveAs "c:\My Documents\autCreateDate.Doc"
.Quit
End With
End Sub

First, there is no declaring a "range" in access. The original says to
declare wordrange as word.range, but that is a big flop in Access' eyes.

Please help !

Thanks!
Chris
chris dottt luksha att sequoyatech dott com
 
C

Chad DeMeyer

Chris,

The crucial decision to make is whether to use early binding or late
binding. The example code you posted uses late binding to instantiate the
Word application, so it doesn't know what "Word.Range" is any more than it
knows what "Range" is. Using late binding, you would have to declare all
the object variables as generic type objects, e.g.:

Dim WordDoc As Object
Dim WordRng As Object
Dim WordPar As Paragraph

The alternative is to use early binding. To use early binding, you need to
add a reference to Word's object library to your code project in Access
(Tools>References, check 'Microsoft Word x.0 Object Library' where x is the
version of Word installed on your hard drive). Then you would declare your
variables as:

Dim WordObj As Word.Application
Dim WordDoc As Word.Document
Dim WordRng As Word.Range
Dim WordPar As Word.Paragraph

....and you would need to instantiate the application with the following:

Set WordObj = New Word.Application

For more in-depth explanation of early vs. late binding, you can refer to
the MSDN article
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/sf7a4.asp.

Regards,
Chad
 
C

Chad DeMeyer

Errata:

Using late binding, Dim WordPar As Object (not As Paragraph)

My typing and thought process got out of sync.
Regards,
Chad
 

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