View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
AP[_6_] AP[_6_] is offline
external usenet poster
 
Posts: 5
Default Write to MS Word

I'm trying to write text to MS Word from Excel with the following
code, but nothing seems to happen. The blank Word document comes up
and there are no errors thrown up. Can you please help ? I'm using
Office 2003 on XP Windows.). Thanks.

---

Sub Write_to_Word()
'20/2/10 Modified http://www.excelguru.ca/node/10
'Bind to an existing or created instance of Microsoft Word

Dim objApp as Object

'Attempt to bind to an open instance
On Error Resume Next
Set objApp = GetObject(, "Word.Application")

If Err.Number < 0 Then
'Could not get instance, so create a new one
Err.Clear
On Error GoTo ErrHandler
Set objApp = CreateObject("Word.Application")
With objApp
.Visible = True
.Documents.Add
End With
Else
'Bound to instance, activate error handling
On Error GoTo ErrHandler
End If

objApp.Documents(1).Selection.Font.Name = "Arial"
objApp.Documents(1).Selection.TypeText Text:="Hello" 'Derived
from Word VBE help
objApp.Documents(1).Selection.TypeParagraph
'Derived from Word VBE help
objApp.Documents(1).Selection.TypeText Text:="Hello 2"

ErrHandler:
'Release the object and resume normal error handling
Set objApp = Nothing
On Error GoTo 0 'Display standard run time error message box
End Sub