open/manipulate a word doc from within access

  • Thread starter Thread starter Nev
  • Start date Start date
N

Nev

Hi,

I have generatad the code to perform a replace in word. I
now want to run the code from within access 97 but it
complains about variables being undefined for the word
applied functions like wdReplaceAll and wdFindContinue.
here is the code

Sub mcoReplacer()

Dim objword As Object
Set objword = CreateObject("Word.Application")
Set objword = GetObject("filename", "Word.Document")
objword.Application.Visible = True
objword.Open FileName:="filename",
ConfirmConversions:=False _
, ReadOnly:=False, AddToRecentFiles:=False,
PasswordDocument:="", _
PasswordTemplate:="", Revert:=False,
WritePasswordDocument:="", _
WritePasswordTemplate:="" ',
Format:=wdOpenFormatAuto
objword.ClearFormatting
objword.Selection.Find.ClearFormatting
objword.Selection.Find.Replacement.ClearFormatting
With objword.Selection.Find
.Text = "^t"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
objword.Selection.Find.Execute Replace:=wdReplaceAll
objword.Save
objword.Close
End Sub

Does anyone know what the problem may be?

Thanks in advance.
Nev.
 
Yes, the constants for wdReplaceAll and wdFindContinue are not defined.
Use something like

Word.wdReplaceAll
 
Nev said:
Hi,

I have generatad the code to perform a replace in word. I
now want to run the code from within access 97 but it
complains about variables being undefined for the word
applied functions like wdReplaceAll and wdFindContinue.
here is the code

Sub mcoReplacer()

Dim objword As Object
Set objword = CreateObject("Word.Application")
Set objword = GetObject("filename", "Word.Document")
objword.Application.Visible = True
objword.Open FileName:="filename",
ConfirmConversions:=False _
, ReadOnly:=False, AddToRecentFiles:=False,
PasswordDocument:="", _
PasswordTemplate:="", Revert:=False,
WritePasswordDocument:="", _
WritePasswordTemplate:="" ',
Format:=wdOpenFormatAuto
objword.ClearFormatting
objword.Selection.Find.ClearFormatting
objword.Selection.Find.Replacement.ClearFormatting
With objword.Selection.Find
.Text = "^t"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
objword.Selection.Find.Execute Replace:=wdReplaceAll
objword.Save
objword.Close
End Sub

Does anyone know what the problem may be?

Thanks in advance.
Nev.

Since you appear to be using late binding, I suspect you don't have a
reference set to the Word object library. If you want to continue using
late binding, you need to define these constants for yourself, or else just
use the numeric values that correspond to them. The simplest way to do that
is probably to open Word, bring up its VBA editor, and in the Immediate
window there enter

?wdReplaceAll

and

?wdFindContinue

You could, of course, add a reference to the Word object library to your
project, but that would be one more reference that would be susceptible to
getting broken.
 
Back
Top