Sub crashes second time through code

J

Joe

I am running Word 2003 on Windows 2000 and am finding that the following code crashes on a specific line when this code is executed more than one and the previously created Word doc is still closed. In other words, I can run this time after time if I always leave the newly created documents open. If I close them, the code bombs on the line

With .Selection
.Font.Bold = True
.TypeText "Storage Requirements"
.Font.Bold = False
.TypeParagraph

' Create table to hold workstations
.Tables.Add Range:=.Range, NumRows:=4, NumColumns:=5
iLastTable = .Tables.Count

************** Code bombs on this line ***********************
.Tables(LastTable).Columns(1).SetWidth InchesToPoints(1), wdAdjustNone


Is there something about the SetWidth method that I need to know?

Any help is greatly appreciated.

Thank you,

Joe
 
P

Peter Hewett

Hi Joe

There's a bit of code you're not showing prior to the line:
With .Selection

So I'm not quite sure of what you're doing?
But the Selection object refers to the ActiveDocument, so if you don't have
at least one document open it must fail!

If you require further help please post again.

HTH + Cheers - Peter
 
J

Joe

Here is the code. I have cut out quite a bit of it. It is basically the same, but for different parts of the document. The document bombs on each SetWidth as described before

If I should include more or sent the entire code, please let me know

Thank you

Jo

Public Sub CreateSummaryReport(wbk As Excel.Workbook, ByRef wd As Word.Application

Dim i As Intege
Dim j As Intege
Dim k As Intege
Dim iStartColRow As Intege
Dim iStartColCol As Intege
Dim iModalityCount As Intege
Dim iLastTable As Intege
Dim tMessages As Strin

With w
' Create a new Word document and display it in landscape mod
.Documents.Ad
.Visible = Tru
.ActiveDocument.PageSetup.Orientation = wdOrientLandscap
.ActiveWindow.View.Zoom = 11

.ActiveDocument.Selec
.Selection.InsertParagraphAfte
.Selection.InsertParagraphAfte
.ActiveDocument.Paragraphs(.ActiveDocument.Paragraphs.Count).Range.Selec

With .Selectio
.TypeParagrap
.TypeParagrap

.Font.Bold = Tru
.Font.Size = 1
.Font.Color = wdColorWhit
.Font.Shading.BackgroundPatternColor = wdColorGree
.TypeText vbTab & "SYSTEM" & vbTab & vbTa

.Font.Bold = Fals
.Font.Size = 1
.Font.Color = wdColorBlac
.Font.Shading.BackgroundPatternColor = wdColorWhit

.TypeParagrap
.TypeParagrap
End Wit

' --- Storage Sizing --
With .Selectio
.Font.Bold = Tru
.TypeText "Storage Requirements
.Font.Bold = Fals
.TypeParagrap

' Create table to hold workstation
.Tables.Add Range:=.Range, NumRows:=4, NumColumns:=
iLastTable = .Tables.Coun

.Tables(iLastTable).Columns(1).SetWidth InchesToPoints(1), wdAdjustNon
.Tables(iLastTable).Columns(2).SetWidth InchesToPoints(1.2), wdAdjustNon
.Tables(iLastTable).Columns(3).SetWidth InchesToPoints(1.2), wdAdjustNon
.Tables(iLastTable).Columns(4).SetWidth InchesToPoints(1.2), wdAdjustNon
.Tables(iLastTable).Columns(5).SetWidth InchesToPoints(4.7), wdAdjustNon

.Tables(iLastTable).Cell(1, 2).Selec
.Range.Font.Bold = Tru
.TypeText "Online Storage
.Shading.BackgroundPatternColor = wdColorGray1

.Tables(iLastTable).Cell(1, 3).Selec
.Range.Font.Bold = Tru
.TypeText "Nearline Storage
.Shading.BackgroundPatternColor = wdColorGray1

.Tables(iLastTable).Cell(1, 4).Selec
.Range.Font.Bold = Tru
.TypeText "Web Storage
.Shading.BackgroundPatternColor = wdColorGray1

.Tables(iLastTable).Cell(1, 5).Selec
.Range.Font.Bold = Tru
.TypeText "Assumptions
.Shading.BackgroundPatternColor = wdColorGray1

' Ech
.Tables(iLastTable).Cell(2, 1).Selec
.Range.Font.Bold = Tru
.TypeText "Echo
.Shading.BackgroundPatternColor = wdColorGray1

.Tables(iLastTable).Cell(2, 2).Selec
.TypeText wbk.Worksheets("Echo").Range("EchoOnline") & " months
.TypeParagrap
.TypeText "(" & Format(wbk.Worksheets("Echo").Range("EchoOnlineTB") * 1000, "###0") & " GB)

.Tables(iLastTable).Cell(2, 3).Selec
.TypeText wbk.Worksheets("Echo").Range("EchoNearline") & " months
.TypeParagrap
.TypeText "(" & Format(wbk.Worksheets("Echo").Range("EchoNearlineTB") * 1000, "###0") & " GB)

.Tables(iLastTable).Cell(2, 4).Selec
.TypeText wbk.Worksheets("Echo").Range("EchoWeb") & " months
.TypeParagrap
.TypeText "(" & Format(wbk.Worksheets("Echo").Range("EchoWebTB") * 1000, "###0") & " GB)

.Tables(iLastTable).Cell(2, 5).Selec
.TypeText "Based on " & wbk.Worksheets("Echo").Range("EchoStudyVolume") & " annual routine/stress studies (Average Study Size = "
.TypeText Format(wbk.Worksheets("Echo").Range("EchoStudySize") * 1000, "###0") & " MB) and "
.TypeText wbk.Worksheets("Echo").Range("VascularStudyVolume") & " annual vascular studies (Average Study Size = "
.TypeText Format(wbk.Worksheets("Echo").Range("VascularStudySize") * 1000, "###0") & " MB)."

End With
End With
 
P

Peter Hewett

Hi Joe

You don't actually say what the error is when it bombs!!
Although the code looks ok, it obviously aint otherwise there'd be no
error! I'm wondering whether the table index is valid. So try changing:

.Tables.Add Range:=.Range, NumRows:=4, NumColumns:=5
iLastTable = .Tables.Count

.Tables(iLastTable).Columns(1).SetWidth InchesToPoints(1), wdAdjustNone

to:
Dim tblItem As Word.Table
Set tblItem = .Tables.Add(Range:=.Range, NumRows:=4, NumColumns:=5)

tblItem.Columns(1).SetWidth InchesToPoints(1), wdAdjustNone

HTH + Cheers - Peter
 
A

Alex Ivanov

Joe,
Check the value of Count in the expression below when the code crashes. I
believe it's either 0 or too big.
iLastTable = .Tables.Count

I am sure your problem is because of implicit using of automatic variables.
I am not sure if this is an exact location of problem since I don't see the
whole code (it may be the related to cached wd object as well), but it is an
example.

With wd
' Create a new Word document and display it in landscape mode
.Documents.Add 'You create an automatic unnamed variable here but
never use it.
.Visible = True
.ActiveDocument.PageSetup.Orientation = wdOrientLandscape
First time you run the code in the line above, Word creates another unnamed
variable and uses it afterward.
The next time you run the code this variable already exists, though may
point to non-existing object,
already closed document. Since this variable already exists, and you have
only one document open Word does not create a new variable, but tries to
reuse the existing one. This may lead to problems. Even if you did not close
the first document, you can't be absolutely sure what is the ActiveDocument
at the point. Most likely it is a newly created one, but it's not
guaranteed.

The way to avoid this kind of problems is using explicitly declared
variables, in your example
Set doc=.Documents.Add
and later use Doc instead of ActiveDocument. Also
Set tbl= .Tables.Add (Range:=.Range, NumRows:=4, NumColumns:=5)
with tbl
.Columns(1).SetWidth InchesToPoints(1), wdAdjustNone
....
end with

Good advise is to Set AnObject=Nothing when you are done with it. It will
make easier to debug problems like this one if you do so.

HTH,
Alex.

Joe said:
Peter,

Run time Error 462. 'The remote server machine does not exists or is not
available' is the error that appears on the first line with the SetWidth
method.
 
J

Jean-Guy Marcil

Hi Joe,

You have to prefix each method with the appropriate object, "wd" in your
case.

Use:
.Tables(iLastTable).Columns(1).SetWidth wd.InchesToPoints(1),
wdAdjustNone
instead of
.Tables(iLastTable).Columns(1).SetWidth InchesToPoints(1), wdAdjustNone
See
http://support.microsoft.com/default.aspx?scid=kb;en-us;189618
for more details.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org


Joe said:
Here is the code. I have cut out quite a bit of it. It is basically the
same, but for different parts of the document. The document bombs on each
SetWidth as described before.
If I should include more or sent the entire code, please let me know.

Thank you.

Joe

Public Sub CreateSummaryReport(wbk As Excel.Workbook, ByRef wd As Word.Application)

Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim iStartColRow As Integer
Dim iStartColCol As Integer
Dim iModalityCount As Integer
Dim iLastTable As Integer
Dim tMessages As String

With wd
' Create a new Word document and display it in landscape mode
.Documents.Add
.Visible = True
.ActiveDocument.PageSetup.Orientation = wdOrientLandscape
.ActiveWindow.View.Zoom = 112

.ActiveDocument.Select
.Selection.InsertParagraphAfter
.Selection.InsertParagraphAfter
..ActiveDocument.Paragraphs(.ActiveDocument.Paragraphs.Count).Range.Select

With .Selection
.TypeParagraph
.TypeParagraph

.Font.Bold = True
.Font.Size = 16
.Font.Color = wdColorWhite
.Font.Shading.BackgroundPatternColor = wdColorGreen
.TypeText vbTab & "SYSTEM" & vbTab & vbTab

.Font.Bold = False
.Font.Size = 10
.Font.Color = wdColorBlack
.Font.Shading.BackgroundPatternColor = wdColorWhite

.TypeParagraph
.TypeParagraph
End With

' --- Storage Sizing ---
With .Selection
.Font.Bold = True
.TypeText "Storage Requirements"
.Font.Bold = False
.TypeParagraph

' Create table to hold workstations
.Tables.Add Range:=.Range, NumRows:=4, NumColumns:=5
iLastTable = .Tables.Count

.Tables(iLastTable).Columns(1).SetWidth InchesToPoints(1), wdAdjustNone
.Tables(iLastTable).Columns(2).SetWidth InchesToPoints(1.2), wdAdjustNone
.Tables(iLastTable).Columns(3).SetWidth InchesToPoints(1.2), wdAdjustNone
.Tables(iLastTable).Columns(4).SetWidth InchesToPoints(1.2), wdAdjustNone
.Tables(iLastTable).Columns(5).SetWidth InchesToPoints(4.7), wdAdjustNone

.Tables(iLastTable).Cell(1, 2).Select
.Range.Font.Bold = True
.TypeText "Online Storage"
.Shading.BackgroundPatternColor = wdColorGray15

.Tables(iLastTable).Cell(1, 3).Select
.Range.Font.Bold = True
.TypeText "Nearline Storage"
.Shading.BackgroundPatternColor = wdColorGray15

.Tables(iLastTable).Cell(1, 4).Select
.Range.Font.Bold = True
.TypeText "Web Storage"
.Shading.BackgroundPatternColor = wdColorGray15

.Tables(iLastTable).Cell(1, 5).Select
.Range.Font.Bold = True
.TypeText "Assumptions"
.Shading.BackgroundPatternColor = wdColorGray15

' Echo
.Tables(iLastTable).Cell(2, 1).Select
.Range.Font.Bold = True
.TypeText "Echo"
.Shading.BackgroundPatternColor = wdColorGray15

.Tables(iLastTable).Cell(2, 2).Select
.TypeText wbk.Worksheets("Echo").Range("EchoOnline") & " months"
.TypeParagraph
.TypeText "(" &
Format(wbk.Worksheets("Echo").Range("EchoOnlineTB") * 1000, "###0") & " GB)"
.Tables(iLastTable).Cell(2, 3).Select
.TypeText wbk.Worksheets("Echo").Range("EchoNearline") & " months"
.TypeParagraph
.TypeText "(" &
Format(wbk.Worksheets("Echo").Range("EchoNearlineTB") * 1000, "###0") & "
GB)"
.Tables(iLastTable).Cell(2, 4).Select
.TypeText wbk.Worksheets("Echo").Range("EchoWeb") & " months"
.TypeParagraph
.TypeText "(" &
Format(wbk.Worksheets("Echo").Range("EchoWebTB") * 1000, "###0") & " GB)"
.Tables(iLastTable).Cell(2, 5).Select
.TypeText "Based on " &
wbk.Worksheets("Echo").Range("EchoStudyVolume") & " annual routine/stress
studies (Average Study Size = "
.TypeText Format(wbk.Worksheets("Echo").Range("EchoStudySize") * 1000, "###0") & " MB) and "
.TypeText wbk.Worksheets("Echo").Range("VascularStudyVolume")
& " annual vascular studies (Average Study Size = "
.TypeText
Format(wbk.Worksheets("Echo").Range("VascularStudySize") * 1000, "###0") & "
MB)."
 
P

Peter Hewett

Hi Joe

I'm glad JM found the solution, it'll teach me to look at code when I should
be in asleep!

As for books, I've not come across one publication that covers both VBA and
the Word Object model well.

One of the better VBA books is:

Visual Basic Language Developers Handbook
Ken Getz/Mike Gilbert
Sybex
ISBN: 0-7821-2162-4

As for the Word object model:

1. Make sure you have the VBA online help installed
2. Use the VBA/IDE (or VB/IDE) Object Browser
3. The MVP website: http://word.mvps.org/index.html

HTH + Cheers - Peter
 
A

Alex Ivanov

I'm glad it's fixed. Again, it is implicit reference to a global object of
type Word.Application
which has been destroyed when you closed the first instance of Word, but
pointer to it still existed.
using wd. prefix you direct the compiler to use the specific instance of the
object.

Alex.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top