Hello,
I am working on a script in Excel 2007, and I keep getting error 462. Here is the entire script:
I believe the problem is with the 'With' loop because when I turn those lines of code into comments, the program runs without errors. The problem is that I want the With loop to create a horizontal line across a Word document. I copied the With loop from recording a macro where I create the line by typing "---" and then hitting Enter; my AutoCorrect settings change this to the desired line. So to be clear, my questions are:
1) How can I fix the code to eliminate the error?
or
2) Is there another way I can add the horizontal line?
I would appreciate any help anyone could provide. Sorry if the formatting of the code makes it difficult to follow.
Sincerely,
Mike
I am working on a script in Excel 2007, and I keep getting error 462. Here is the entire script:
Sub ControlWord()
' You must pick Microsoft Word 8.0 from Tools>References
' in the VB editor to execute Word commands.
' See VB Help topic "Controlling One Microsoft Office Application from Another"
' for more information.
' Originally published by MrExcel.com | Excel Resources | Excel Seminars | Excel Products 2/28/1999
Dim appWD As Word.Application
' Create a new instance of Word & make it visible
Set appWD = CreateObject("Word.Application")
appWD.Visible = True
'Sheets("report-skipped-list-mn").Select
'Find the last row with data in the database
'FinalRow = Sheets("report-skipped-list-mn").Range("A200").End(xlUp).Row
FinalRow = Range("A200").End(xlUp).Row
'Sheets("Template").Range("A6").Value = FinalRow
For i = 2 To FinalRow
'Sheets("report-skipped-list-mn").Select
Dim LeftHeader As String
LeftHeader = Sheets("report-skipped-list-mn").Range("C" & i) & "-" & Sheets("report-skipped-list-mn").Range("D" & i) & "-" & Sheets("report-skipped-list-mn").Range("E" & i)
'Concatenate three cells into desired left side header
Dim RightHeader As String
RightHeader = "AID: " & Sheets("report-skipped-list-mn").Range("F" & i) & " | " & Date
'Put LeftHeader into A1 cell
Sheets("Template").Select
Sheets("Template").Range("A1").Value = LeftHeader
Sheets("Template").Range("H1").Value = RightHeader
'Range("A1").Paste
' Copy the data for the new document to the clipboard
Range("A1:I1").Copy
' Tell Word to create a new document
appWD.Documents.Add
' Tell Word to paste the contents of the clipboard into the new document
appWD.Selection.Paste
'Add Delimiter
'Set appWD = Nothing
appWD.Selection.MoveDown Unit:=wdLine, Count:=2
appWD.Selection.TypeParagraph
appWD.Selection.MoveUp Unit:=wdLine, Count:=1
With appWD.Selection.Borders(wdBorderBottom)
.LineStyle = Options.DefaultBorderLineStyle
.LineWidth = Options.DefaultBorderLineWidth
.Color = Options.DefaultBorderColor
End With
appWD.Selection.MoveDown Unit:=wdLine, Count:=1
appWD.Selection.TypeParagraph
' Change drive
'ChDrive "E"
' Change directory
'ChDir "E:\Documents\Chegg"
' Save the new document with a sequential file name
appWD.ActiveDocument.SaveAs Filename:=LeftHeader
' Close this new word document
appWD.ActiveDocument.Close
Next i
' Close the Word application
appWD.Quit
End Sub
I believe the problem is with the 'With' loop because when I turn those lines of code into comments, the program runs without errors. The problem is that I want the With loop to create a horizontal line across a Word document. I copied the With loop from recording a macro where I create the line by typing "---" and then hitting Enter; my AutoCorrect settings change this to the desired line. So to be clear, my questions are:
1) How can I fix the code to eliminate the error?
or
2) Is there another way I can add the horizontal line?
I would appreciate any help anyone could provide. Sorry if the formatting of the code makes it difficult to follow.
Sincerely,
Mike