PDA

View Full Version : Problems Adjusting Table Column with in Word after import from Excel



paddysheeran
10-22-2010, 03:37 AM
Hi All,

Im experiecing an error message when trying to run the following.

The code used to transfer the data from excel to word is:


Public t as Word.Range
Public Rng as Range
Sub WS_Weekly_Tickets()
Set wsSource = ActiveWorkbook.Sheets("Customer_Incidents_WordReport")

Set t = wdDoc.Bookmarks("Incidents_Weekly").Range
wsSource.Activate
Range("A1").CurrentRegion.Select
Set Rng = Selection
Rng.Resize(Selection.Rows.Count, Selection.Columns.Count - 1).Select
Set Rng = Selection
Rng.Copy
t.PasteAndFormat (wdPasteDefault)
Weekly_Tickets_Style
End Sub

The above works fine and pastes the data into the bookmark without an issue. When Weekly_Tickets_Style is called there in lies the problem. The following codes applies the style and font size but when the first line of the Column width adjustment is reached the error message appears:

"Run-time error '4608':

Value out of range

Sub Weekly_Tickets_Style()

With t
.Style = "Weekly_Tickets"
.Font.Size = "8"
.Tables(1).Columns(1).SetWidth ColumnWidth:=5, RulerStyle:= _
wdAdjustNone
.Tables(1).Columns(2).SetWidth ColumnWidth:=5, RulerStyle:= _
wdAdjustNone
.Tables(1).Columns(3).SetWidth ColumnWidth:=6, RulerStyle:= _
wdAdjustNone
.Tables(1).Columns(4).SetWidth ColumnWidth:=8, RulerStyle:= _
wdAdjustNone
.Tables(1).Columns(5).SetWidth ColumnWidth:=10, RulerStyle:= _
wdAdjustNone
.Tables(1).Columns(6).SetWidth ColumnWidth:=6, RulerStyle:= _
wdAdjustNone
.Tables(1).Columns(7).SetWidth ColumnWidth:=9, RulerStyle:= _
wdAdjustNone
.Tables(1).Columns(8).SetWidth ColumnWidth:=10, RulerStyle:= _
wdAdjustNone
.Tables(1).Columns(9).SetWidth ColumnWidth:=2, RulerStyle:= _
wdAdjustNone
.Tables(1).Columns(10).SetWidth ColumnWidth:=5, RulerStyle:= _
wdAdjustNone
End With
End Sub

Any help would be greatly appreciated.

thanks,

Paddy.

Sebastian H
10-22-2010, 05:00 PM
What does the following give you? With t
Debug.Print .Tables.Count
Debug.Print .Tables(1).Columns.Count
End With

paddysheeran
10-25-2010, 02:36 AM
The immediate window says:

1
13

Sebastian H
11-03-2010, 07:46 PM
That rules out any obvious issues. Sorry, in that case, I don't know either. :dunno

Tinbendr
11-04-2010, 07:32 AM
When you use the style sub, 't' is now out of scope. You'll have to pass it to the sub.

......
Weekly_Tickets_Style(t)
End Sub

Sub Weekly_Tickets_Style(t as Word.Range)

Or move all the style code to the other sub.

fumei
11-04-2010, 10:42 AM
But t is declared as a PUBLIC variable, and as a Word.Range.

I take it this is early binding?

Sebastian H
11-04-2010, 11:27 AM
Why would it have no problem with t.Font.Size, then?

Tinbendr
11-04-2010, 12:47 PM
But t is declared as a PUBLIC variable, and as a Word.Range.Rats!! It sure is! :doh:

fumei
11-05-2010, 11:06 AM
You did not answer the early binding question, but try:

.Tables(1).Columns(1).SetWidth ColumnWidth:=5, RulerStyle:= 0
instead of wdAdjustNone