Link to home
Start Free TrialLog in
Avatar of andreaspan
andreaspan

asked on

Call MSWORD from VB

Iam using VB 5.0. I want to open winword application from VB and a specific DOC file. I used the following method but it doesn't work:

Method 1
---------------------------
Dim x As Object
Dim wb As Object
Dim ws As Object

Set x = CreateObject("Word.Application")
x.Visible = True
Set wb = x.Documents.Open("C:\Rents\MailMerge.doc")
Set ws = wb.Documents(1)

=============================================

Method 2
--------------
Dim ms As Object
Dim sfilename As String

   Set ms = CreateObject("Word.basic")
   sfilename = "C:\RENTS\MailMerge.Doc" 'CurDir + "\FindPaymentsForCheck.RPT"
   ms.fileopen sfilename


Regards
Andreas
Avatar of sduckett
sduckett

Have you added a reference to the Word 8.0 object library?
'Project' - 'References'.
ASKER CERTIFIED SOLUTION
Avatar of caraf_g
caraf_g

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
Your code is fine as long as you have the reference. No solution required.
Including a reference is problematic as far as applications such as Word are concerned: you're basically forcing your user to use one particular version of Word.

In your Method 2 you're working around this problem by using CreateObject - one of the few situations where CreateObject is preferable over adding a Reference to your Project and using the New keyword. In general, CreateObject is slower and should be avoided but not in this case.

So your Method 2 is getting close to the right solution but if you look at the PAQ mentioned in my answer you can get it cheap.
caraf_g:
Your right the reference isn't completely necessary as the code works fine without it (My false assumption, Sorry).
But if the code works fine in my test app, why was the question asked? You've appraised both solutions as if they are working haven't you?
Hello Andreas,

Thank you for the 305 points but I didn't expect you to give me that much - I pointed you at the PAQ so you could buy that for 5 points and save the other points by rejecting my answer to this question.

But thanks anyway.