Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Word Document from VB

Status
Not open for further replies.

Jefftopia

Programmer
Jul 30, 2002
104
0
0
US
On click event I need to:

1) Open MS Word.
2) Type a sentence to Word.
3) Save the Word document (giving it a specific name) to a specific location.

Can it be done? I hope so or I'm in trouble. Any assistance appreciated.

thx.

 
If you want to just open Microsoft Word, use this code:

Private Sub Command1_Click()
spath = &quot;<path to MS Word>&quot;
whatever = shell(spath, vbNormalFocus)
End Sub

You can just type &quot;whatever&quot;, you don't have to replace it with anything.

Hope this helps As a circle of light increases, so does the circumfrence of darkness surrounding it. -Albert Einstein
 
If you need to do it all programmatically, try this:

Add a reference to Word (Project/references/Word x.x)

Then stick this in your Command1_Click event
Code:
Private Sub Command1_Click()
Dim wdapp As Word.Application
Dim wsdoc As Word.Document
Dim wdrange As Word.Range
Set wdapp = New Word.Application
Set wddoc = New Word.Document
wdapp.Visible = False
Set wdrange = wddoc.Range
wdrange.InsertAfter &quot;This is a sentence&quot;
wddoc.SaveAs &quot;c:\mydoc.doc&quot;
Set wdrange = Nothing
Set wddoc = Nothing
Set wdapp = Nothing
End Sub

Note this is NOT finished code - just enough to start you! Let me know if this helps
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
thnk you all for the suggestions. I have decided to create a template and simply copy the template to a new location under a new name. Achieved same results.

FileCopy Source, Destination


 
Hello, I tried the code pasted above but I keep getting an error stating: &quot;user defined type not defined&quot; and it highlights this line:

'Private Sub Command1_Click()'

I am a newbie, so take it easy on me if this is really stupid !

Oh yeah, I just pasted it in as code for a button in a test prog.
 
Toxodont,
Which code are you using?
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
@johnwm

this is the code I used:

Private Sub Command1_Click()
Dim wdapp As Word.Application
Dim wsdoc As Word.Document
Dim wdrange As Word.Range
Set wdapp = New Word.Application
Set wddoc = New Word.Document
wdapp.Visible = False
Set wdrange = wddoc.Range
wdrange.InsertAfter &quot;This is a sentence&quot;
wddoc.SaveAs &quot;c:\mydoc.doc&quot;
Set wdrange = Nothing
Set wddoc = Nothing
Set wdapp = Nothing
End Sub
 
1. Did you check that your command button is called Command1?
2. Did you add a reference to Word (Project/references/Word x.x)

There is also a typo in the original post:
[tt]Dim wsdoc As Word.Document[/tt]
should be
[tt]Dim wddoc As Word.Document[/tt]

If you set Option Explicit in each form module and code module you will catch typos like that more easily (or set Tools|Options|Require Variable declaration, which sets Option Explicit for you)
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
john,

I didn't have the reference set. It works perfect now thanks!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top