I have code in Excel 2003 to paste Excel ranges into Word as pictures. I want to paste the picture into the Word document and resize to a nice printing size then add the next one after a section break and so on. There are several different macros that have the same paste and format into Word so it is broken out into 2 macros.
This code works on the first run but will not format the Word document size or picture sizes on subsequent runs. It will center the pictures on subsequent run. If Excel is closed then reopened, the first run works wonderfully but not subsequently. I can't figure out why. I've tried working with the record macros to no avail.
I added a line of code to test that it was the correct picture by selecting it. It did so I removed that line.
BTW -- I only have a little knowledge about VBA so please keep this in mind.
This code works on the first run but will not format the Word document size or picture sizes on subsequent runs. It will center the pictures on subsequent run. If Excel is closed then reopened, the first run works wonderfully but not subsequently. I can't figure out why. I've tried working with the record macros to no avail.
I added a line of code to test that it was the correct picture by selecting it. It did so I removed that line.
BTW -- I only have a little knowledge about VBA so please keep this in mind.
Code:
Public PageType, RecType
Public SendVar, SendVar2
Sub SendCigPages()
Dim Message, Message2, Title, Response
Dim MyDir, MyPath
Dim WdObjRec As Word.Application
Dim Var
Application.ScreenUpdating = False
MyPath = ActiveWorkbook.Path
Message = "Did you enter the number of pages? "
Title = "Page Number Check"
Message2 = "TheWord document has been created." & Chr(13) & Chr(13) & _
"The document is located in the Print folders in " & MyPath & "."
SendVar = 1
SendVar2 = 0
RecType = "Cig"
Response = MsgBox(Message, vbYesNo, Title)
If Response = 7 Then
MsgBox ("The print job has been cancelled.")
Sheets("INFO").Visible = False
Exit Sub
End If
'Sends AD-7s
Range("AD_7").Copy
PageType = 1
ExceltoWordCig
Range("AD_7EX").Copy
PageType = 1
ExceltoWordCig
SendAdjPagesCig ‘This is code to collect pages that the user can check off, same type of code as is here
SendVar2 = 1
ExceltoWordCig
SendVar = Empty
SendVar2 = Empty
RecType = Empty
PageType = Empty
Application.ScreenUpdating = True
Sheets("Options").Select
Range("A1").Select
ActiveCell.Offset(0, 0).Range("A1").Select
MsgBox (Message2)
End Sub
Sub ExceltoWordCig()
'Code to paste Excel pages into Word as pictures
Dim WdObjRec As Word.Application
Dim Var, Var2, FileName, RcName, MyPath, MyDir, RecFile, MyFile
MyDir = ActiveWorkbook.Path
MyPath = MyDir & "\Print" + RecType + "\" & RecType & "AuditFile.doc"
On Error Resume Next
Set WdObjRec = GetObject(, "Word.application")
If Err <> 0 Then
Set WdObjRec = CreateObject("Word.application")
WdObjRec.WindowState = wdWindowStateNormal
Err.Clear
End If
WdObjRec.Visible = True
If SendVar2 = 0 Then
If SendVar = 1 Then
WdObjRec.Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
With WdObjRec.ActiveDocument.PageSetup
.TopMargin = InchesToPoints(0.6)
.BottomMargin = InchesToPoints(0.6)
.LeftMargin = InchesToPoints(0.7)
.RightMargin = InchesToPoints(0.7)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0)
.FooterDistance = InchesToPoints(0)
.PageWidth = InchesToPoints(8.5)
.PageHeight = InchesToPoints(11)
End With
WdObjRec.ActiveDocument.SaveAs FileName:=MyPath 'Saves file as variable name
WdObjRec.Selection.HomeKey unit:=wdStory 'Moves to first line
End If
If PageType = 1 Then 'Set up for portrait
WdObjRec.Selection.PageSetup.Orientation = wdOrientPortrait
WdObjRec.Selection.PasteSpecial , DataType:=wdPasteMetafilePicture
WdObjRec.ActiveDocument.Shapes(SendVar).Left = wdShapeCenter
WdObjRec.ActiveDocument.Shapes(SendVar).Top = wdShapeCenter
WdObjRec.ActiveDocument.Shapes(SendVar).LockAspectRatio = msoCTrue
WdObjRec.ActiveDocument.Shapes(SendVar).Height = InchesToPoints(9.5)
Else 'Sets page up for landscape pages
WdObjRec.Selection.PageSetup.Orientation = wdOrientLandscape
WdObjRec.Selection.PasteSpecial , DataType:=wdPasteMetafilePicture
WdObjRec.ActiveDocument.Shapes(SendVar).LockAspectRatio = msoTrue
WdObjRec.ActiveDocument.Shapes(SendVar).Width = InchesToPoints(9.2)
WdObjRec.ActiveDocument.Shapes(SendVar).Left = wdShapeCenter
WdObjRec.ActiveDocument.Shapes(SendVar).Top = wdShapeCenter
End If
WdObjRec.Selection.EndKey unit:=wdStory
WdObjRec.Selection.InsertBreak Type:=wdSectionBreakNextPage
Application.CutCopyMode = False
WdObjRec.ActiveDocument.Save
Else
WdObjRec.ActiveDocument.Close SaveChanges:=True
WdObjRec.Application.Quit
End If
SendVar = SendVar + 1
Set WdObjRec = Nothing
End Sub