jerry12302
Active Member
- Joined
- Apr 18, 2005
- Messages
- 456
- Office Version
- 2010
- Platform
- Windows
I'm trying to use Excel VBA to resize a picture in a Word document after pasting an Excel range into the document. I have tried numerous ways and nothing is working.
Here is the code I am using, everything works except the resizing attempts:
Sub TestCopyExcelToWord()
'(1) Open an existing blank Word Doc that has a header and footer (used as a template),
' path and name identified in Excel range "BlankWordDoc".
'(2) Copy the Excel range "Invoice" as a picture
'(3) Paste the picture into the Word Doc
'(4) Resize the picture because it is too small [THIS IS THE CODE NEEDED]
'(5) Save the Word Doc using the file name identified in the Excel range "PathFileName"
'(6) Close Word
'All the code works except step (4) to resize the picture.
Dim oWord
Set oWord = CreateObject("Word.Application")
Dim oDoc
Set oDoc = CreateObject("Word.Document")
Dim myWordDoc As String
myWordDoc = Range("BlankWordDoc").Value
Const sDocPath As String = myWordDoc
Dim rRange1 As Range
Set rRange1 = ActiveSheet.Range("Invoice")
Dim myFile As String
myFile = Range("PathFileName").Value
Set oDoc = GetObject(sDocPath)
Set oWord = oDoc.Parent
oWord.Visible = True
rRange1.CopyPicture xlPrinter
oDoc.ActiveWindow.Selection.PasteSpecial
'BEGIN CODE NEEDED HERE: TO RESIZE THE PICTURE, HEIGHT = 550, WIDTH = 550
'Tried the below code, it does not work, picture size does not change.
'With oDoc
' Set wdImg = .InlineShapes(1)
' With wdImg
' .Height = 550
' .Width = 550
' End With
'End With
'Also tried the below code, it does not work either.
'Selection.ShapeRange.Height = 550
'Selection.ShapeRange.Width = 550
'END CODE NEEDED HERE
oDoc.SaveAs2 myFile, 12, False, "", True
oDoc.Close
oWord.Quit
Set oDoc = Nothing
Set rRange1 = Nothing
End Sub
Here is the code I am using, everything works except the resizing attempts:
Sub TestCopyExcelToWord()
'(1) Open an existing blank Word Doc that has a header and footer (used as a template),
' path and name identified in Excel range "BlankWordDoc".
'(2) Copy the Excel range "Invoice" as a picture
'(3) Paste the picture into the Word Doc
'(4) Resize the picture because it is too small [THIS IS THE CODE NEEDED]
'(5) Save the Word Doc using the file name identified in the Excel range "PathFileName"
'(6) Close Word
'All the code works except step (4) to resize the picture.
Dim oWord
Set oWord = CreateObject("Word.Application")
Dim oDoc
Set oDoc = CreateObject("Word.Document")
Dim myWordDoc As String
myWordDoc = Range("BlankWordDoc").Value
Const sDocPath As String = myWordDoc
Dim rRange1 As Range
Set rRange1 = ActiveSheet.Range("Invoice")
Dim myFile As String
myFile = Range("PathFileName").Value
Set oDoc = GetObject(sDocPath)
Set oWord = oDoc.Parent
oWord.Visible = True
rRange1.CopyPicture xlPrinter
oDoc.ActiveWindow.Selection.PasteSpecial
'BEGIN CODE NEEDED HERE: TO RESIZE THE PICTURE, HEIGHT = 550, WIDTH = 550
'Tried the below code, it does not work, picture size does not change.
'With oDoc
' Set wdImg = .InlineShapes(1)
' With wdImg
' .Height = 550
' .Width = 550
' End With
'End With
'Also tried the below code, it does not work either.
'Selection.ShapeRange.Height = 550
'Selection.ShapeRange.Width = 550
'END CODE NEEDED HERE
oDoc.SaveAs2 myFile, 12, False, "", True
oDoc.Close
oWord.Quit
Set oDoc = Nothing
Set rRange1 = Nothing
End Sub