Link to home
Start Free TrialLog in
Avatar of ggorp
ggorp

asked on

VB and Word

Who can give me the command to use in VB to control Word:
U want to use the "Find and Replace All function " from Word via VB.  I create an instance of Word, open a template but I need to replace a specific variable x (unknows) times .

How do I print more then 1 copy of a creatd doc??


ASKER CERTIFIED SOLUTION
Avatar of mark2150
mark2150

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chabaud
chabaud

why don't you use VBA like in this (pseudo) code:

    Dim wd As New Word.Application
   
    Dim dc As Word.Document
   
    Set dc = wd.Documents.Open("yourdoc.doc")
   
    dc.Content.Find.Execute findtext:="x", replacewith:="y"
   
    dc.Save
   
    dc.PrintOut Copies:=3
   
    dc.Close

Avatar of ggorp

ASKER

I am using somethin like chabaud did:
Dim wd As New Word.Application
     
    Dim dc As Word.Document
     
    Set dc = wd.Documents.Open("yourdoc.doc")
     
    dc.Content.Find.Execute findtext:="x", replacewith:="y"
     
    dc.Save
     
    dc.PrintOut Copies:=3
     
    dc.Close


The replace action, does ity replace more then 1 time or shoul I repeat it for every time that string is in the text??  I am looking for some thing li,e REPLACEALL
Use Word Automation instead of sending keys.

To get code for any action:
Start Word macro recorder, do your actions, stop recording, and copy code to your VB project.

It can be something like:

EditReplace .Find = "a", .Replace = "b", .Direction = 0, .MatchCase = 0, .WholeWord = 0, .PatternMatch = 0, .SoundsLike = 0, .ReplaceAll, .Format = 0, .Wrap = 1, .FindAllWordForms = 0

In your code, add object reference (dc.EditReplace) and use variables instead of strings "a" and "b".

Use the same method to get code for number of copies.
Why do you accept the response with SendKey ???
Avatar of ggorp

ASKER

there went something wrong.  Sorry.