Hello all,
I've tried so many things to get this to work, but I cannot figure it out. Below is my latest code. I have a spreadsheet that pastes a table into Word and does some formatting to the Word document. I've never used VBA in Word before. The code always works the first time but breaks at this line any time I try to run it afterwards: WordDoc.PageSetup.TopMargin = InchesToPoints(0.5)
Thanks in advance for your help.
I've tried so many things to get this to work, but I cannot figure it out. Below is my latest code. I have a spreadsheet that pastes a table into Word and does some formatting to the Word document. I've never used VBA in Word before. The code always works the first time but breaks at this line any time I try to run it afterwards: WordDoc.PageSetup.TopMargin = InchesToPoints(0.5)
Code:
'open a new Word document, format it, and paste the data in from Excel
Set WordApp = Nothing
Set WordDoc = Nothing
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Add
WordDoc.Activate
WordDoc.Application.ScreenUpdating = False
WordDoc.PageSetup.Orientation = 1
WordDoc.Paragraphs.SpaceAfter = 0
WordDoc.ActiveWindow.Selection.Font.Name = "Times New Roman"
WordDoc.ActiveWindow.Selection.Font.Size = 12
WordDoc.ActiveWindow.Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
WordDoc.PageSetup.TopMargin = InchesToPoints(0.5)
WordDoc.PageSetup.BottomMargin = InchesToPoints(0.5)
WordDoc.PageSetup.LeftMargin = InchesToPoints(0.5)
WordDoc.PageSetup.RightMargin = InchesToPoints(0.5)
WordDoc.PageSetup.HeaderDistance = InchesToPoints(0.2)
WordDoc.PageSetup.FooterDistance = InchesToPoints(0.2)
If WordDoc.ActiveWindow.View.SplitSpecial <> wdPaneNone Then
WordDoc.ActiveWindow.Panes(2).Close
End If
If WordDoc.ActiveWindow.ActivePane.View.Type = wdNormalView Or WordDoc.ActiveWindow.ActivePane.View.Type = wdOutlineView Then
WordDoc.ActiveWindow.ActivePane.View.Type = wdPrintView
End If
WordDoc.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
WordDoc.ActiveWindow.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
WordDoc.ActiveWindow.Selection.TypeText Text:="GUARANTEE"
WordDoc.ActiveWindow.Selection.TypeParagraph
WordDoc.ActiveWindow.Selection.TypeText Text:="GAYLORD OPRYLAND"
WordDoc.ActiveWindow.Selection.TypeParagraph
WordDoc.ActiveWindow.Selection.TypeText Text:=Format(wsPrep.Cells(2, 3), "DDDD MMMM DD, YYYY")
WordDoc.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
wsOutput.Visible = xlSheetVisible
wsOutput.UsedRange.Copy
WordDoc.ActiveWindow.Selection.PasteExcelTable False, False, False
Application.CutCopyMode = False
wsOutput.Visible = xlSheetHidden
WordDoc.ActiveWindow.Selection.Tables(1).Rows(1).HeadingFormat = wdToggle
WordDoc.ActiveWindow.Selection.Tables(1).Rows.Alignment = wdAlignRowCenter
WordDoc.ActiveWindow.Selection.Tables(1).Rows.LeftIndent = InchesToPoints(0)
WordDoc.ActiveWindow.Selection.Tables(1).Rows.AllowBreakAcrossPages = False
WordDoc.ActiveWindow.Selection.Tables(1).LeftPadding = InchesToPoints(0)
WordDoc.ActiveWindow.Selection.Tables(1).RightPadding = InchesToPoints(0)
WordDoc.ActiveWindow.Selection.Tables(1).Spacing = 0
WordDoc.ActiveWindow.Selection.Tables(1).Select
WordDoc.ActiveWindow.Selection.ParagraphFormat.SpaceBefore = 0
WordDoc.ActiveWindow.Selection.ParagraphFormat.SpaceBeforeAuto = False
WordDoc.ActiveWindow.Selection.ParagraphFormat.SpaceAfter = 0
WordDoc.ActiveWindow.Selection.ParagraphFormat.SpaceAfterAuto = False
WordDoc.ActiveWindow.Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
WordDoc.ActiveWindow.Selection.ParagraphFormat.LineUnitBefore = 0
WordDoc.ActiveWindow.Selection.ParagraphFormat.LineUnitAfter = 0
WordApp.Application.ScreenUpdating = True
Dim fd As FileDialog
Set fd = WordApp.FileDialog(msoFileDialogSaveAs)
With fd
.InitialFileName = Format(wsPrep.Cells(2, 3), "M.D.YY") & " GTD Sheet"
If .Show = 0 Then 'user pressed the cancel button
Else
.Execute
End If
End With
Set fd = Nothing
Set WordDoc = Nothing
Set WordApp = Nothing
Thanks in advance for your help.