H
Howard Kaikow
I got bored today, so I decided to rewrite the code in KB article 316383 to
decrease the number of object references.
This resulted in a number of nested With ... End With.
The original code had a
Dim r As Integer, c As Integer
shortly before a For Next.
The original code does not use With ... End With.
I ended up with, no pun intended, the code below. If the Dim statement is
moved into more deeply nested With ... End Withs, there are compile errors.
I've included the statement twice below. I've commented out the case that
does work, so you should see a compile error.
Ordinarily such statements should be at the beginning of the procedure.
Are there any restrictions for placing PROCEDURE level Dim declaration
statement?
Where are these documented?
Imports Microsoft.Office.Interop
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim oWord As Word.Application
Dim Pos As Double
Dim r As Integer, c As Integer
'Start Word and open the document template.
oWord = CreateObject("Word.Application")
With oWord
..Visible = True
Pos = .InchesToPoints(7)
..Documents.Add()
With .ActiveDocument
'Dim r As Integer, c As Integer ' This works
'Insert a paragraph at the beginning of the document.
With .Content
Dim r As Integer, c As Integer ' This fails
..InsertAfter(Text:="Heading 1")
..Font.Bold = True
..Paragraphs(1).SpaceAfter = 24 '24 pt spacing after paragraph.
..InsertParagraphAfter()
..Collapse(Word.WdCollapseDirection.wdCollapseEnd)
'Insert a paragraph at the end of the document.
..InsertAfter(Text:="Heading 2")
..Paragraphs(1).SpaceAfter = 6
..InsertParagraphAfter()
..Collapse(Word.WdCollapseDirection.wdCollapseEnd)
..InsertAfter(Text:="This is a sentence of normal text. Now here is a
table:")
'Insert another paragraph.
..InsertParagraphAfter()
..Paragraphs(.Paragraphs.Count).Range.Font.Bold = False
..InsertParagraphAfter()
..Collapse(Word.WdCollapseDirection.wdCollapseEnd)
'Insert a 3 x 5 table, fill it with data, and make the first row
'bold and italic.
With .Tables.Add(.Bookmarks.Item("\endofdoc").Range, 3, 5)
With .Range
..ParagraphFormat.SpaceAfter = 6
..Font.Bold = False
End With
For r = 1 To 3
For c = 1 To 5
..Cell(r, c).Range.Text = "r" & r & "c" & c
Next
Next
With .Rows.Item(1).Range.Font
..Bold = True
..Italic = True
End With
End With
End With
With .Content
'Add some text after the table.
..Paragraphs(.Paragraphs.Count).Format.SpaceAfter = 24
..Collapse(Word.WdCollapseDirection.wdCollapseEnd)
..InsertParagraphAfter()
End With
With .Paragraphs(.Paragraphs.Count)
..SpaceAfter = 24
With .Range
..Text = "And here's another table:"
..Font.Bold = False
..InsertParagraphAfter()
End With
End With
'Insert a 5 x 2 table, fill it with data, and change the column widths.
With .Tables.Add(.Bookmarks.Item("\endofdoc").Range, 5, 2)
..Range.ParagraphFormat.SpaceAfter = 6
For r = 1 To 5
For c = 1 To 2
..Cell(r, c).Range.Text = "r" & r & "c" & c
Next
Next
With .Columns
..Item(1).Width = oWord.InchesToPoints(2) 'Change width of columns 1 & 2
..Item(2).Width = oWord.InchesToPoints(3)
End With
End With
'Keep inserting text. When you get to 7 inches from top of the
'document, insert a hard page break.
With .Content
..InsertParagraphAfter()
..Collapse(Word.WdCollapseDirection.wdCollapseEnd)
Do
..ParagraphFormat.SpaceAfter = 6
..InsertAfter("A line of text")
..InsertParagraphAfter()
..Collapse(direction:=Word.WdCollapseDirection.wdCollapseEnd)
Loop While Pos >=
..Information(Word.WdInformation.wdVerticalPositionRelativeToPage)
..InsertBreak(Word.WdBreakType.wdPageBreak)
..Collapse(Word.WdCollapseDirection.wdCollapseEnd)
..InsertAfter("We're now on page 2. Here's my chart:")
..InsertParagraphAfter()
End With
'Insert a chart and change the chart.
With .Bookmarks.Item("\endofdoc").Range.InlineShapes.AddOLEObject( _
ClassType:="MSGraph.Chart.8", FileName _
:="", LinkToFile:=False, DisplayAsIcon:=False)
With .OLEFormat.Object
..charttype = 4 'xlLine = 4
With .Application
..Update()
..Quit()
End With
End With
'If desired, you can proceed from here using the Microsoft Graph
'Object model to make additional changes to the
'chart.
..Width = oWord.InchesToPoints(6.25)
..Height = oWord.InchesToPoints(3.57)
End With
'Add text after the chart.
With .Content
..InsertParagraphAfter()
..InsertAfter("THE END.")
End With
End With
End With
oWord = Nothing
'All done. Close this form.
Me.Close()
End Sub
End Class
decrease the number of object references.
This resulted in a number of nested With ... End With.
The original code had a
Dim r As Integer, c As Integer
shortly before a For Next.
The original code does not use With ... End With.
I ended up with, no pun intended, the code below. If the Dim statement is
moved into more deeply nested With ... End Withs, there are compile errors.
I've included the statement twice below. I've commented out the case that
does work, so you should see a compile error.
Ordinarily such statements should be at the beginning of the procedure.
Are there any restrictions for placing PROCEDURE level Dim declaration
statement?
Where are these documented?
Imports Microsoft.Office.Interop
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim oWord As Word.Application
Dim Pos As Double
Dim r As Integer, c As Integer
'Start Word and open the document template.
oWord = CreateObject("Word.Application")
With oWord
..Visible = True
Pos = .InchesToPoints(7)
..Documents.Add()
With .ActiveDocument
'Dim r As Integer, c As Integer ' This works
'Insert a paragraph at the beginning of the document.
With .Content
Dim r As Integer, c As Integer ' This fails
..InsertAfter(Text:="Heading 1")
..Font.Bold = True
..Paragraphs(1).SpaceAfter = 24 '24 pt spacing after paragraph.
..InsertParagraphAfter()
..Collapse(Word.WdCollapseDirection.wdCollapseEnd)
'Insert a paragraph at the end of the document.
..InsertAfter(Text:="Heading 2")
..Paragraphs(1).SpaceAfter = 6
..InsertParagraphAfter()
..Collapse(Word.WdCollapseDirection.wdCollapseEnd)
..InsertAfter(Text:="This is a sentence of normal text. Now here is a
table:")
'Insert another paragraph.
..InsertParagraphAfter()
..Paragraphs(.Paragraphs.Count).Range.Font.Bold = False
..InsertParagraphAfter()
..Collapse(Word.WdCollapseDirection.wdCollapseEnd)
'Insert a 3 x 5 table, fill it with data, and make the first row
'bold and italic.
With .Tables.Add(.Bookmarks.Item("\endofdoc").Range, 3, 5)
With .Range
..ParagraphFormat.SpaceAfter = 6
..Font.Bold = False
End With
For r = 1 To 3
For c = 1 To 5
..Cell(r, c).Range.Text = "r" & r & "c" & c
Next
Next
With .Rows.Item(1).Range.Font
..Bold = True
..Italic = True
End With
End With
End With
With .Content
'Add some text after the table.
..Paragraphs(.Paragraphs.Count).Format.SpaceAfter = 24
..Collapse(Word.WdCollapseDirection.wdCollapseEnd)
..InsertParagraphAfter()
End With
With .Paragraphs(.Paragraphs.Count)
..SpaceAfter = 24
With .Range
..Text = "And here's another table:"
..Font.Bold = False
..InsertParagraphAfter()
End With
End With
'Insert a 5 x 2 table, fill it with data, and change the column widths.
With .Tables.Add(.Bookmarks.Item("\endofdoc").Range, 5, 2)
..Range.ParagraphFormat.SpaceAfter = 6
For r = 1 To 5
For c = 1 To 2
..Cell(r, c).Range.Text = "r" & r & "c" & c
Next
Next
With .Columns
..Item(1).Width = oWord.InchesToPoints(2) 'Change width of columns 1 & 2
..Item(2).Width = oWord.InchesToPoints(3)
End With
End With
'Keep inserting text. When you get to 7 inches from top of the
'document, insert a hard page break.
With .Content
..InsertParagraphAfter()
..Collapse(Word.WdCollapseDirection.wdCollapseEnd)
Do
..ParagraphFormat.SpaceAfter = 6
..InsertAfter("A line of text")
..InsertParagraphAfter()
..Collapse(direction:=Word.WdCollapseDirection.wdCollapseEnd)
Loop While Pos >=
..Information(Word.WdInformation.wdVerticalPositionRelativeToPage)
..InsertBreak(Word.WdBreakType.wdPageBreak)
..Collapse(Word.WdCollapseDirection.wdCollapseEnd)
..InsertAfter("We're now on page 2. Here's my chart:")
..InsertParagraphAfter()
End With
'Insert a chart and change the chart.
With .Bookmarks.Item("\endofdoc").Range.InlineShapes.AddOLEObject( _
ClassType:="MSGraph.Chart.8", FileName _
:="", LinkToFile:=False, DisplayAsIcon:=False)
With .OLEFormat.Object
..charttype = 4 'xlLine = 4
With .Application
..Update()
..Quit()
End With
End With
'If desired, you can proceed from here using the Microsoft Graph
'Object model to make additional changes to the
'chart.
..Width = oWord.InchesToPoints(6.25)
..Height = oWord.InchesToPoints(3.57)
End With
'Add text after the chart.
With .Content
..InsertParagraphAfter()
..InsertAfter("THE END.")
End With
End With
End With
oWord = Nothing
'All done. Close this form.
Me.Close()
End Sub
End Class