Solved

VB and Word

Posted on 1999-01-08
6
201 Views
Last Modified: 2010-08-05
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??


0
Comment
Question by:ggorp
6 Comments
 
LVL 12

Accepted Solution

by:
mark2150 earned 120 total points
ID: 1454864
Use SendKeys to drive Word. Figure out the exact keystrokes you'ld use to do it manually, (CTRL this, ALT that, etc.) and send those sequences. Look in the help for SendKeys to see how to send Shift/Ctrl/Alt combo's. This you can use to make it jump thru hoops. Ditto for making it print more than one page. Send it "Alt F, P" and then send it the "Alt whatever" to change to the # of copies field. Then send your number with {enter} on the end. Should work well.

'
' Fire up child application
'
retval = Shell("Appname.exe", vbMaximizedFocus)
'
' Send app a command to two ...
'
sendit ("%FO" & filename & "{enter}")          ' Pop Open file dialog
Call delay(3)
'
. more of the same
' When finished use the following to close the child app
'
' Can't use function here as 2nd app activate will fail . . .
'
On Error Resume Next
AppActivate retval
SendKeys "%FX", True       'Close child window
DoEvents



Private Sub sendit(txt As String)
'
' Send string to child task
'
    On Error Resume Next
    AppActivate retval      'Make sure child task is active
    DoEvents                'Give it a shot to run
    SendKeys txt, True      'Tell it what to do and wait
    DoEvents                'Cede some more time
'
End Sub


M

0
 
LVL 4

Expert Comment

by:chabaud
ID: 1454865
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

0
 

Author Comment

by:ggorp
ID: 1454866
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:ameba
ID: 1454867
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.
0
 
LVL 4

Expert Comment

by:chabaud
ID: 1454868
Why do you accept the response with SendKey ???
0
 

Author Comment

by:ggorp
ID: 1454869
there went something wrong.  Sorry.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adding to a VBA? 6 61
How to compare ms sql hashbytes results within vb6 5 54
Modifying Conditional Format from VBA code 3 48
TT Auto Dashboard 13 80
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now