Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB and Word

Posted on 1999-01-08
6
Medium Priority
?
207 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 240 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 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