Link to home
Start Free TrialLog in
Avatar of ghassan99
ghassan99

asked on

MS Word and VB

I have certain forms created in Word.  In place of certain places on the form I want it to be filled from values received from VB.  I want to do this in VB, open the document in Word (OLE), and place the value received from VB variables in there and printing it.  Detailed instructions please, if possible.
Avatar of Mirkwood
Mirkwood

The first thing you need to look into is Word Object Heirarchy...
I think all of the features that you would like to use our exposed through the Document,Range and/or Selection Objects.

For Example to Create a New Document and Insert Some Text At the Current Cursor Location:

' With a Reference to the Microsoft Word Object Library 8.0

Dim moWordApp As Word.Application
Dim moWordDoc As Word.Document
Dim s as string
Set moWordApp = New Word.Application


moWordApp.Visible = True
s = "Hello World"

Set moWordDoc = moWordApp.Documents.Add()
With moWordDoc
   .Selection.TypeText = "THIS IS A NEW WORD DOCUMENT:"  & s
   .SaveAs "C:\SOMEDOCUMENT"
   .Close
End With

moWordApp.Close
Set moWordDoc = Nothing
Set moWordApp = Nothing
Avatar of ghassan99

ASKER

I have the form already in Word, like check this:
"Name:
Address:
Telephone: "
Everything is inside the already saved word, I need to fill in the blanks from the VB prog.  I know how to do all this u explained.  I dont want to create the document in VB!!!!And how about printing?
You need to work with Bookmarks into the template (.dot) files. Then You can send text to these bookmarks via DDE very easy.
Here is an example :
By example open template C:\MSOFFICE\TEMPLATES\EXAMPLE.DOT with in it a bookmark called 'name' and put my name into it.
The text field on the form is Text1

DbWord = "C:\MSOFFICE\WINWORD"
Text1.Text = "Courtheyn Filip"

Set wrd = GetObject(, "Word.Application.8")
wrd.Documents.Add _ Template:="C:MSOFFICE\TEMPLATES\EXAMPLE.DOT"
wrd.ActiveDocument.SaveAs FileName:=DbWord & "TEMPFILE.RTF", FileFormat:=6 '6 = wdFormatRTF
Text1.LinkMode = 0
Text1.LinkTimeout = 500
Text1.LinkTopic = "WinWord|" + DbWord + "FaniSoft.rtf"
Text1.LinkMode = 2
Text1.LinkItem = "name"
Text1.LinkPoke
Text1.LinkMode = 0

Simple do the actions you want in VB and record it as a macro.
Look at the code generated. That code you can copy to VB.
How to interact with Word, see:
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=10176791 

It's still open, if you're quick!
ASKER CERTIFIED SOLUTION
Avatar of Mirkwood
Mirkwood

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
Where does the value txtName reside?
FormFields("txtName").Result = "John Smith"

I'm trying to create a field in the template but I dont find one.
   
txtName is just the name of one of the field inside the word document. You have to create them yourself
Thats what I'm trying to do, I find only predefined fields when I go to insert-fields
Search in the help on how to create a form field...
u have some mistakes in your code regarding variable declaration.  And the WordAPP.Close doesnt work, no such method. I tried .Quit it will ask u if u want to quit and there are print jobs pending!  Is there a work around?
.Visible = false will do the trick
It will do the trick allright, but it will leave word open in the background, so several calls and the system will crash!
I guess if before the function creates a new word doc, it can test if there is already an instance of word open and close it. How can we accomplish that?
ghassan99

I coded this and it seems to close word fine:
Option Explicit
Private moWordApp As Word.Application
Private moWordDoc As Word.Document
Private Sub Command1_Click()

If moWordApp Is Nothing Then
    FillIN
End If

End Sub

Public Sub FillIN()

' Demonstrates
' Opening Word, Creating a Document, Filling in FormField
' Saving, Printing and Closing


Set moWordApp = New Word.Application
moWordApp.Visible = True

Set moWordDoc = moWordApp.Documents.Add  'pass in template name if required:("YOUR TEMPLATE NAME GOES HERE")

With moWordDoc
   'Examples of what you can do with it.
   '.FormFields("txtName").Result = "John Smith"
   '.FormFields("txtCity").Result = "Exeter - UK"
   .Content.InsertBefore "Hello world"
End With

' Look at the Word Document Now....
'do you wish to save?
'moWordDoc.SaveAs "SOMEDOCUMENT"
'moWordDoc.PrintOut

End Sub

Private Sub Command2_Click()

If Not moWordApp Is Nothing Then
    moWordDoc.Close False
   
    moWordApp.Visible = False
   
    Set moWordDoc = Nothing
    Set moWordApp = Nothing
End If

End Sub
can u press ctrl+alt+delete and check the tasks please!:)
ghassan99, I'm not that green! I used task manager and checked it thoroughly. And when I click the Close button Word goes away. Honest. Perhaps it's caused by the statements
    Set moWordDoc = Nothing
    Set moWordApp = Nothing


I'm using Windows NT 4 + SP4 (SP3 until yesterday). I can't see how but maybe that makes a difference. What are you using?
Sorry:)
I'm using VB5 no SP's, Win95.
I tried it your way, I put the exit code in a seperate button, but when I check the task manager I still see 'winword' in there. When I run it again I see another entry of 'winword'
I'll try it at home where I can boot into Win95. See what happens.
I've got it, methinks.

I checked it again, and I found the following:

Even though the application is disappearing from the task manager on my PC, the memory count goes up and up - leak... In other words, you're right, and setting .visible to false will not do the trick

But I found the solution. To close word:

Private Sub Command2_Click()

If Not moWordApp Is Nothing Then
    moWordDoc.Close False  'False = Don't save changes
     
    moWordApp.Quit False 'False = Don't save changes
     
    Set moWordDoc = Nothing
    Set moWordApp = Nothing
End If

End Sub



This is what I did the first time, but the problem if it was spooling to the printer a dialog box will pop up and warn u that closing will mean ending the print. anyway, one probably doesnt get everything!:) So now, who do I offer the points to?
Mirkwood came up with the first answer, but if you think I deserve some points as well, why not post a dummy question "points for caraf_g" so I can answer it instead. If you want to split the 100 points between us, reject Mirkwood's answer here and post two dummy questions, one for Mirkwood and one for me. Anyway, I'll leave it up to you to decide.
I'm sorry I dont have enough points to ask 2 more questions, and at the same time I dont want to increase the number of asked questions. I promise I will post a question later for u only caraf_g...Thanks a lot, u have been moer than helpful.
No problem. If and when you do get around to posting that question, post the URL here so I get another notification.

Good luck!

Pino