View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
ardvk ardvk is offline
external usenet poster
 
Posts: 2
Default VB automated pasting of Excel range into Word document

I'm trying to write VB code to paste several Excel ranges into a Word
document, as required by my employer.

I'm able to do this satisfactorily using manual cut and paste (Office 2003),
but when I automate it, I get "Run-time error 438: object doesn't support
this method". I get this error whether I use Selection. or a variable Dim as
Excel.Range or Word.Range or Object. I also get the error regardless of the
type of paste I use. Pasted images aren't acceptable because the range may
span several pages.

Below is a code example. The errant command is flagged between '*****
comments. Otherwise, the code runs as designed. The GetWordApp is from a
previous post that I found useful (thanks!)

I would appreciate any help you can give.

Thanks,

Dave in Madison

Sub PrintToWordFile()

Const fname = "C:\Test.doc"
Const rname = "EntireBudget"

Dim a As Excel.Range ' print range
Dim g As Boolean ' gridlines flag
Dim o As Object ' word application
Dim w As Word.Document ' document within word application

g = ActiveWindow.DisplayGridlines
ActiveWindow.DisplayGridlines = False
Set a = Range(rname)
a.Copy

GetWordApp o
With o
.Visible = True
.Documents.Open Filename:=fname, ReadOnly:=False
Set w = .Documents(fname)
End With

'*****

a.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=False, _
RTF:=True

'*****

w.SaveAs "C:\Test.doc"
o.Application.Quit

ActiveWindow.DisplayGridlines = g
Set w = Nothing
Set o = Nothing
End Sub