Link to home
Start Free TrialLog in
Avatar of mesclun
mesclun

asked on

Error 4605 when pasting from Excel into Word object

Hi, I have some code in my Excel 2002 file that does this:

1. creates a word object
2. copies a range in Excel
3. sets the Word pagesetup parameters
4. pastes the range as an enhanced metafile into the word object
5. closes the word object.

someone wrote in complaining of a runtime 4605 error that i believe exists for Word 2000 users (I'm on Word 2002). according to this http://www.kbalertz.com/Feedback_275966.aspx I seem to be creating a frame.

below is my code. do you have any idea how to avoid or undo this whole Frame thing??
many many thanks
mesclun

*****code******

Sub ExportToWord()

Dim BuildFile As String
Dim wdObj As Object
Dim wdDoc As Object
Dim strFile As String

Const wdPasteEnhancedMetafile = 9
Const wdPageBreak = 7
Const wdGoToPage = 1
Const wdGoToFirst = 1
Const wdGoToLast = -1

Application.ScreenUpdating = False
BuildFile = MsgBox("This will save a Word document to your C:\ drive.", vbOKOnly, "Build")

If BuildFile = vbOK Then

Range("B1:AD52").Select
Selection.Copy

Set wdObj = CreateObject("word.application")
'Set wdDoc = wdObj.Documents.Add("normal.dot")
Set wdDoc = wdObj.Documents.Add("")
'wdObj.Visible = True
wdDoc.PageSetup.TopMargin = 65
wdDoc.PageSetup.LeftMargin = 95
wdDoc.PageSetup.RightMargin = 95
wdDoc.PageSetup.PageWidth = 612
wdDoc.PageSetup.PageHeight = 792

wdObj.Selection.PasteSpecial DataType:=wdPasteEnhancedMetafile

Range("B53:AD79").Select
Selection.Copy

wdObj.Selection.InsertBreak Type:=wdPageBreak
wdObj.Selection.GoTo What:=wdGoToPage, Which:=wdGoToLast
wdObj.Selection.PasteSpecial DataType:=wdPasteEnhancedMetafile

*****etc. until done*****
Avatar of R_Rajesh
R_Rajesh

The problem is that in word 2K, the picture remains selected after its been pasted to word and the insert break command will not be available when a picture object is selected.
The workaround of course is to unselect the picture before inserting the pagebreak
.
.
.
Range("B53:AD79").Select
Selection.Copy
wdObj.Selection.EndKey Unit:=wdStory '<---------------- add this line
wdObj.Selection.InsertBreak Type:=wdPageBreak
.
.
.

Rajesh
Avatar of mesclun

ASKER

Awesome--I will go test this.

Thanks Rajesh.
Avatar of mesclun

ASKER

Hey, I received a 4120 Bad Parameter when I tried that.

After poking around I tried adding the line

wdObj.Selection.TypeParagraph

so:

***********
wdDoc.PageSetup.PageHeight = 792

wdObj.Selection.PasteSpecial DataType:=wdPasteEnhancedMetafile

Range("B53:AD79").Select
Selection.Copy

wdObj.Selection.TypeParagraph ' <----- Added this line
wdObj.Selection.InsertBreak Type:=wdPageBreak
wdObj.Selection.GoTo What:=wdGoToPage, Which:=wdGoToLast
wdObj.Selection.PasteSpecial DataType:=wdPasteEnhancedMetafile
***********

This appeared to take care of the problem.

Mesclun
ASKER CERTIFIED SOLUTION
Avatar of GhostMod
GhostMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial