Hi,
Is it possible to write straight to Microsoft Word document from vba within an excel workbook?
Hi,
Is it possible to write straight to Microsoft Word document from vba within an excel workbook?
It's very possible, but you'll need to set a reference to Microsoft Word Object Library (Tools -> References).
Put this code in a new module. Make sure there is text in A1 & A2 for this test.
This is a very basic example, but hopefully it'll point you in the right direction.
[VBA]Sub WordTest()
Dim doc As Word.Document
Dim myWord As Word.Application
Dim strHead As String
Dim strRecords As String
Dim ws As Worksheet
On Error GoTo ErrorHandler
Set ws = Sheet1
strHead = ws.Range("A1").Text
strRecords = ws.Range("A2").Text
Set myWord = New Word.Application
Set doc = myWord.Documents.Add
myWord.Visible = True
'paste variables into word document
doc.Paragraphs(1).Range.Text = strHead & vbCrLf
doc.Paragraphs(2).Range.Text = strRecords
doc.Close SaveChanges:=wdPromptToSaveChanges
EndProc:
myWord.Quit
Set myWord = Nothing
Exit Sub
ErrorHandler:
If Err = 4198 Then MsgBox "You refused to save this document"
Resume EndProc
End Sub
[/VBA]
-Randy Shea
I'm a programmer, but I'm also pro-grammar!
If your issue is resolved, please use Thread Tools to mark your thread as Solved!
PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003
Hi,
Thanks a lot for the information, i'll give it a go.
With regards to setting up the reference to Microsoft Word Object, will that be saved in the file or within my version of office?
What i mean is, if i created the object reference, then gave the file to another user would they have to set up the reference within their Excel for this to work?
Thanks
Gary
Not necessarily so.Originally Posted by CreganTur
Assuming your code works
[vba]
Sub WordTest()
Dim doc As Object
Dim myWord As Object
Dim strHead As String
Dim strRecords As String
Dim ws As Worksheet
On Error GoTo ErrorHandler
Set ws = Sheet1
strHead = ws.Range("A1").Text
strRecords = ws.Range("A2").Text
Set myWord = CreateObject("Word.Application")
Set doc = myWord.Documents.Add
myWord.Visible = True
'paste variables into word document
doc.Paragraphs(1).Range.Text = strHead & vbCrLf
doc.Paragraphs(2).Range.Text = strRecords
doc.Close SaveChanges:=wdPromptToSaveChanges
EndProc:
myWord.Quit
Set myWord = Nothing
Exit Sub
ErrorHandler:
If Err = 4198 Then MsgBox "You refused to save this document"
Resume EndProc
End Sub
[/vba]
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber
Is there a way of adapting:Originally Posted by xld
to save to a specified path without prompting the user to save?doc.Close SaveChanges:=wdPromptToSaveChanges
Gary
This will save automatically:
[VBA]
doc.Close SaveChanges:=wdSaveChanges[/VBA]
-Randy Shea
I'm a programmer, but I'm also pro-grammar!
If your issue is resolved, please use Thread Tools to mark your thread as Solved!
PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003
Sorry, I should have replaced the Word constants with late binding.
Change
wdPromptToSaveChanges
to
-2
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber
Hi, thanks for the suggestionOriginally Posted by CreganTur
Where will this automatically save to?
Can i specify the path it saves to?
Gary
Sorry, you've confused me with that.Originally Posted by xld
What does that do exactly?
It the value equivalent of wdPromptToSaveChanges as you cannot use Word constants in a late bound procedure. If you don't have Option Explicit, your code will probably treat that as an unitialised variable, a value of 0.
____________________________________________
Nihil simul inventum est et perfectum
Abusus non tollit usum
Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
James Thurber
Ah ok, thanks a lot for your help.
A quick question, in the code above you can specify what data is written to different paragraphs. How can i reference other section of the word document, headers and footers for example.
I've tried searching google for this, as it seems a simple question, but i cannot seem to find the answer.
Gary