Call MSWORD from VB

Posted on 1999-01-20
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 100 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.
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

773 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