Solved

How to call MsWord from vb6

Posted on 2006-11-17
6
268 Views
Last Modified: 2010-04-30
Hi Experts,

I'm making an app. in vb6, it generates a lot of text that I need to copy to a word file, I need to put the code in a command button, so on the click event, the word must open with all the information.

Also is it possible to name that file from the app and leave it without chance to change the name?

Thank's

Regards,

EL
0
Comment
Question by:pander
  • 4
  • 2
6 Comments
 
LVL 13

Expert Comment

by:Michael_D
ID: 17967894
Try this:

   Dim objApp As Object
   
    Set objApp = CreateObject("Word.Application")
    objApp.Documents.Add , , 0
    objApp.ActiveDocument.Select
    objApp.Selection.TypeText "Your text goes here"
    objApp.ActiveDocument.SaveAs "C:\test.doc"
    objApp.ActiveDocument.Close
    objApp.Quit
    Set objApp = Nothing
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 17967953
if you want to have word open change the code to this:
Dim objApp As Object
   
    Set objApp = CreateObject("Word.Application")
    objApp.Documents.Add , , 0
    objApp.ActiveDocument.Select
    objApp.Selection.TypeText "Your text goes here"
    objApp.ActiveDocument.SaveAs "C:\test.doc"
    objApp.Visible = True
   
0
 
LVL 13

Accepted Solution

by:
Michael_D earned 500 total points
ID: 17967978
oops...  I shouldnt have to remobve this line
Set objApp = Nothing

so this will open word, create new document with given name and leave it open:

Dim objApp As Object
   
    Set objApp = CreateObject("Word.Application")
    objApp.Documents.Add , , 0
    objApp.ActiveDocument.Select
    objApp.Selection.TypeText "Your text goes here"
    objApp.ActiveDocument.SaveAs "C:\test.doc"
    objApp.Visible = True
    Set objApp = Nothing
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:pander
ID: 17968038
Great !!!

Thank you Michael,

It works good and that's what I was looking for; Another question, Is there any way to disable the toolbar? I want to leave the only "Save" option enabled, if its possible it will be super, if not thats fine.

Regards,
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 17968189
I wold recoment using RTFBox instead of word if you want only limited options.
I never enable/disable toolbars from VB. Also this code uses the "Regular" instance of word - mean if you remove the ttoolbar it will be gone even if user run Word from the start menu

Just my opinion tho...
0
 

Author Comment

by:pander
ID: 17968220
All right Mic, thank you very much.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 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…

856 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