开发者

How to do a find/replace using word object in access VBA

I am working with access 2003 and I want to open a word document from hard disk and replace some text in it and print it (An action similar to a mail merge, in which I h开发者_如何学Cave a document and I want to add some text from the current record in access to the document and print it). I found the following code, but it doesn't change the text in word document.

With pDoc.Content.Find
    .ClearFormatting
    .Text = "hi"
    .Replacement.ClearFormatting
    .Replacement.Text = "hello"
    .Execute Replace:=wdReplaceAll, Forward:=True, _
        Wrap:=wdFindContinue
End With

And pDoc was set to the opened document.

Any idea why this doesn't change the text? (It runs correctly, but doesn't change hi to hello; the word document is not changed)


Citing from my answer to this question: Excel VBA: How to use ListTemplates in Excel to control Word?

In Excel you need to add a reference to the Word object model:

In the makro editor (Alt+F11) select the "Tools" menu and click "References...". Click the checkbox next to "Microsoft Word Object Library." Click "OK." Now try to run the macro again.

That should get you nearly there.

Maybe all that's missing is the reference to the Word object library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜