ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Write to MS Word (https://www.excelbanter.com/excel-programming/439771-write-ms-word.html)

AP[_6_]

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

Mike H

Write to MS Word
 
Hi,

You weren't seeing an error because your error handler was doing what you
told it to do and handling the error

Sub Write_to_Word()
Dim objApp As Object
On Error Resume Next
Set objApp = GetObject(, "Word.Application")

If Err.Number < 0 Then
On Error GoTo ErrHandler
Set objApp = CreateObject("Word.Application")
Else
'Bound to instance, activate error handling
On Error GoTo ErrHandler
End If
With objApp
.Visible = True
.Documents.Add
.Selection.Font.Name = "Arial"
.Selection.TypeText "Hello"
.Selection.TypeParagraph
.Selection.TypeText Text:="Hello 2"
End With
Set objApp = Nothing
Exit Sub
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
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"AP" wrote:

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
.


ryguy7272

Write to MS Word
 
Do a little searching on DocVariables in Word. I'm not sure how to do it in
2007, and that's all I have installed on this computer, but I know it can be
done! Google for it... You can see them in Word with Alt+F9.

Then, enter some named ranges in Excel and run this code
Sub PushToWord()

Dim objWord As New Word.Application
Dim doc As Word.Document
Dim bkmk As Word.Bookmark
sWdFileName = Application.GetOpenFilename(, , , , False)
Set doc = objWord.Documents.Open(sWdFileName)

objWord.activedocument.variables("BrokerFirstName" ).Value =
Range("BrokerFirstName").Value
objWord.activedocument.variables("BrokerLastName") .Value =
Range("BrokerLastName").Value

ActiveDocument.Fields.Update

objWord.Visible = True

End Sub

Remember, set a reference to Word in Excel. Also, the named ranges in Excel
must match the DocVariables in Word. When the macro fires, you will see an
application window open, search for your Word file, open it, and the
variables from Excel will 'flow' into Word.

Good luck!!
--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.


"Mike H" wrote:

Hi,

You weren't seeing an error because your error handler was doing what you
told it to do and handling the error

Sub Write_to_Word()
Dim objApp As Object
On Error Resume Next
Set objApp = GetObject(, "Word.Application")

If Err.Number < 0 Then
On Error GoTo ErrHandler
Set objApp = CreateObject("Word.Application")
Else
'Bound to instance, activate error handling
On Error GoTo ErrHandler
End If
With objApp
.Visible = True
.Documents.Add
.Selection.Font.Name = "Arial"
.Selection.TypeText "Hello"
.Selection.TypeParagraph
.Selection.TypeText Text:="Hello 2"
End With
Set objApp = Nothing
Exit Sub
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
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"AP" wrote:

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
.


AP[_6_]

Write to MS Word
 
On Feb 21, 12:10*am, Mike H wrote:
Hi,

You weren't seeing an error because your error handler was doing what you
told it to do and handling the error

Sub Write_to_Word()
Dim objApp As Object
On Error Resume Next
Set objApp = GetObject(, "Word.Application")

If Err.Number < 0 Then
* * On Error GoTo ErrHandler
* * Set objApp = CreateObject("Word.Application")
Else
* * 'Bound to instance, activate error handling
* * On Error GoTo ErrHandler
End If
With objApp
* * * * .Visible = True
* * * * .Documents.Add
* * * * .Selection.Font.Name = "Arial"
* * * * .Selection.TypeText "Hello"
* * * * .Selection.TypeParagraph
* * * * .Selection.TypeText Text:="Hello 2"
* * End With
Set objApp = Nothing
Exit Sub
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
--
Mike



Thanks very much Mike - it works beautifully !!!

AP


All times are GMT +1. The time now is 01:09 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com