makeitwork09
Technical User
I am using Excel and Word 2007.
This is my first time working with code for Word.
The following code errors with error number 91. Originally the error was 462, but after adding wrdDoc, the error changed to 91.
The following is the full code, but only the above seems to fail.
Thanks
This is my first time working with code for Word.
The following code errors with error number 91. Originally the error was 462, but after adding wrdDoc, the error changed to 91.
Code:
'format each table
For wrdTbl = 1 To wrdDoc.Tables.Count
Set t = wrdDoc.Tables(wrdTbl)
t.Borders(wdBorderTop).LineStyle = wdLineStyleNone
t.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
t.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
t.Borders(wdBorderRight).LineStyle = wdLineStyleNone
t.Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
t.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
t.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
t.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
t.Columns(7).SetWidth ColumnWidth:=71.45, RulerStyle:=wdAdjustNone
Next wrdTbl
The following is the full code, but only the above seems to fail.
Code:
Sub UpdateWordDoc()
Dim wrdApp As Word.Application
Dim wrdDoc As Document
Dim wrdSln As Selection
Dim i As Integer
Dim wrdTbl As Long
Dim t As Table
On Error Resume Next
BkName = ThisWorkbook.Name
On Error Resume Next
Set wrdApp = GetObject(, "Word.Application")
Set wrdDoc = wrdApp.ActiveDocument
If wrdApp Is Nothing Then
Set wrdApp = GetObject("", "Word.Application")
On Error GoTo ErrorHandler
wrdApp.Documents.Add
wrdApp.Visible = True
End If
On Error GoTo 0
On Error GoTo ErrorHandler
wrdApp.Selection.Font.Name = "Arial"
wrdApp.Selection.Font.Size = 10
For i = 4 To Workbooks(BkName).Worksheets.Count
With Workbooks(BkName).Worksheets(i)
Workbooks(BkName).Worksheets(i).Range("C2:I36").Copy
With wrdApp
.Visible = True
.Activate
.Selection.PasteExcelTable False, True, False
.Selection.EndKey unit:=wdStory
.Selection.InsertBreak Type:=7
End With
End With
Next i
'format each table
For wrdTbl = 1 To wrdDoc.Tables.Count
Set t = wrdDoc.Tables(wrdTbl)
t.Borders(wdBorderTop).LineStyle = wdLineStyleNone
t.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
t.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
t.Borders(wdBorderRight).LineStyle = wdLineStyleNone
t.Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
t.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
t.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
t.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
t.Columns(7).SetWidth ColumnWidth:=71.45, RulerStyle:=wdAdjustNone
Next wrdTbl
'Release the Word object to save memory and exit macro
ErrorExit:
Set wrdApp = Nothing
Exit Sub
'Error Handling routine
ErrorHandler:
If Err Then
MsgBox "Error No: " & Err.Number & "; There is a problem"
If Not wrdApp Is Nothing Then
wrdApp.Quit False
End If
Resume ErrorExit
End If
End Sub
Thanks