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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel range not pasting to Word Document Help

Status
Not open for further replies.

newbee2

Technical User
Apr 21, 2002
85
0
0
Hi,
I want to take a range from an excel sheet and paste it to a new word document laid out in landscape. I am using the following. I can get word to open but no document shows.

Case Is = 8
MsgBox "it works", vbOKOnly
Set rng7 = Sheet1.Range("a130:g154")
ThisWorkbook.Sheets(1).ActiveRange.Copy
WordStart

Sub WordStart()
On Error Resume Next
'create a word instance to use for the Table uses
'the existing word instance if there is one, otherwise creates
'a new instance.
Dim objWord As Object
Dim fisWordRunning As Boolean

Set objWord = GetObject(, "Word.Application")
If Err.Number = 429 Then
fisWordRunning = False
'word is not running; create a Word object
Set objWord = CreateObject("Word.Application")
Err.Clear
Else
'use current word object
fisWordRunning = True
End If
With objWord
'add new document
.Documents Add
.Documents.PageSetup.Orientation = wdOrientLandscape
'make word visible
.Visible = True
End With

With objWord.Selection
.EndKey Unit:=wdStory
'Paste Range
.Paste
End With
'release object Variable
Set objWord = Nothing

End Sub

Where be I going wrong
Regards
Bill
 
You don't need to make sure Word is not already open. Use this as your WordStart sub instead:

Dim objWord As Word.Application
Set objWord = CreateObject("Word.Application")
With objWord
'add new document
.Documents.Add
.Documents(.Documents.Count).PageSetup.Orientation = wdOrientLandscape '
'make word visible
.Visible = True
End With
With objWord.Selection
.EndKey Unit:=wdStory
'Paste Range
.Paste
End With
 
Thanks for the advise, have placed into module.
still getting word opening and greyed screen, no document showing.
Thanks
Bill
 
It works fine for me. What version of Office are you using?

I wonder if the document is being created, just not shown; or if the document is not being created at all.

If the entire code runs fine, then the document is being created, otherwise you wouldn't be able to modify it.

In this case, try saving it and then opening it again.

Also, you may want to put your new document object in a variable for purposes of ease of coding. And maybe put the .visible line earlier. Try this instead of my previous suggestion:


Dim objWord As Word.Application
Dim objDoc As Word.Document
Set objWord = CreateObject("Word.Application")
With objWord
'add new document
.Documents.Add
'make word visible
.Visible = True
Set objDoc = .Documents(.Documents.Count)
objDoc.PageSetup.Orientation = wdOrientLandscape '
End With
With objWord.Selection
.EndKey Unit:=wdStory
'Paste Range
.Paste
End With
objDoc.SaveAs "C:\testword.doc" 'full path
objDoc.Close
objWord.Documents.Open "C:\testword.doc" 'full path
 
Modification to my other post from earlier today:

Use this instead of the
Code:
With objWord
block:

With objWord
'make word visible
.Visible = True
'add new document
.Documents.Add
Set objDoc = .Documents(.Documents.Count)
objDoc.PageSetup.Orientation = wdOrientLandscape '
End With
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top