Improve company productivity with a Business Account.Sign Up


Call MSWORD from VB

Posted on 1999-01-20
Medium Priority
Last Modified: 2008-03-04
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

Question by:andreaspan
  • 3
  • 3

Expert Comment

ID: 1469560
Have you added a reference to the Word 8.0 object library?
'Project' - 'References'.
LVL 10

Accepted Solution

caraf_g earned 300 total points
ID: 1469561
Please look at the following Previously Answered Question:

If this question gives you enough information to do what you want, you can reject this answer and delete this question - I don't want to be taking points from you twice!

Expert Comment

ID: 1469562
Your code is fine as long as you have the reference. No solution required.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 10

Expert Comment

ID: 1469563
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.

Expert Comment

ID: 1469564
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?
LVL 10

Expert Comment

ID: 1469565
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.


Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

601 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