Solved

VB and Word

Posted on 1999-01-08
6
198 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now