?
Solved

VB and Word

Posted on 1999-01-08
6
Medium Priority
?
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

764 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