Solved

Call MSWORD from VB

Posted on 1999-01-20
6
153 Views
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


Regards
Andreas
0
Comment
Question by:andreaspan
  • 3
  • 3
6 Comments
 
LVL 3

Expert Comment

by:sduckett
Comment Utility
Have you added a reference to the Word 8.0 object library?
'Project' - 'References'.
0
 
LVL 10

Accepted Solution

by:
caraf_g earned 100 total points
Comment Utility
Please look at the following Previously Answered Question:

http://www.experts-exchange.com/topics/comp/lang/visualbasic/Q.10116990

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!
0
 
LVL 3

Expert Comment

by:sduckett
Comment Utility
Your code is fine as long as you have the reference. No solution required.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
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.
0
 
LVL 3

Expert Comment

by:sduckett
Comment Utility
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?
0
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
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.

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now