Inserting selected range into Word document

E

Ed Stevens

Using examples previously found here and on MS website, I'm able to
get my macro to start Word , open a new document., and insert some
text into that document. Now I need to be able to insert a range of
selected cells. My first cut was this:

Sub createDoc()

Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Dim wordRng As Word.Range
Dim wordPara As Word.Paragraph

Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True

With wordApp
.WindowState = wdWindowStateMaximize
.Documents.Add
Set wordDoc = wordApp.ActiveDocument
Set wordRng = wordDoc.Range

With wordRng

' this works
.InsertAfter "Running Word Using Automation"
' this doesn't
.InsertAfter range(cells(1,1),cells(lngLastRow,lngLastCol))
'nor does this
.InsertAfter
range(cells(1,1),cells(lngLastRow,lngLastCol)).select
<snip rest of code>



So, I need someone to get me back on track

Thanks in advance.
 
D

Debra Dalgleish

The following code will insert a line of text, then the selected range
from Excel:

Sub CreateDoc()
Dim WdApp As Object

Selection.Copy
On Error Resume Next
Set WdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set WdApp = CreateObject("Word.Application")
End If

With WdApp
.Visible = True
.Documents.Add DocumentType:=0
.Selection.TypeText "Running Word Using Automation"
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.Paste
End With
Set WdApp = Nothing

End Sub
 
E

Ed Stevens

The following code will insert a line of text, then the selected range
from Excel:

Sub CreateDoc()
Dim WdApp As Object

Selection.Copy
On Error Resume Next
Set WdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set WdApp = CreateObject("Word.Application")
End If

With WdApp
.Visible = True
.Documents.Add DocumentType:=0
.Selection.TypeText "Running Word Using Automation"
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.Paste
End With
Set WdApp = Nothing

End Sub
That got me where I needed to go. Thanks for the assistance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top