Table not inserting correctly

N

NYSA-HD

I am running the following code to insert a table into a document. My
problem is that every so often the table inserts strangely and runs off the
page. I have chalked it up to a word bug, but it is getting annoying. Has
anyone else had this problem when inserting tables using code. Is there
anything I can change below that might eliminate this seemly random problem.
Is there a line of code that will correct this issue?
------------------------------

Public Sub InsertVotes()
If FindVar("&VOTES") Then Selection.Delete unit:=wdCharacter, Count:=1
If (pubYEAS <> "" And pubYEAS <> "00") Or (pubNAYS <> "" And pubNAYS <>
"00") Then
InsertVoteBox
Else
' Do nothing if no vote taken
Selection.Delete unit:=wdCharacter, Count:=1
End If
End Sub
Public Sub InsertVoteBox()
Selection.TypeParagraph
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2,
NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
.PreferredWidthType = wdPreferredWidthPoints
.PreferredWidth = InchesToPoints(0.67)
.Columns(1).PreferredWidth = InchesToPoints(0.42)
.Columns(2).PreferredWidth = InchesToPoints(0.25)
.Rows.SetLeftIndent LeftIndent:=InchesToPoints(3),
RulerStyle:=wdAdjustNone
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
.Borders(wdBorderTop).LineStyle = wdLineStyleNone
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
.Borders.Shadow = False
' Added these characteristics: 7/26/06
.TopPadding = InchesToPoints(0)
.BottomPadding = InchesToPoints(0)
.LeftPadding = InchesToPoints(0)
.RightPadding = InchesToPoints(0)
End With
Selection.TypeText Text:="YEAS"
Selection.MoveRight unit:=wdCell
Selection.TypeText Text:=pubYEAS
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.MoveRight unit:=wdCell
Selection.TypeText Text:="NAYS"
Selection.MoveRight unit:=wdCell
Selection.TypeText Text:=pubNAYS
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.MoveRight unit:=wdCharacter, Count:=2
End Sub
 
J

Jezebel

Your code is a case study in why you can't create reliable macros using the
macro recorder. Hard to be precise about how to fix the problem without a
better description of what the problem actually is ('inserts strangely'
doesn't really tell us much); but the likely problem is that the table is
inserted wherever the selection is at the time --

Selection.TypeParagraph
ActiveDocument.Tables.Add Range:=Selection.Range

If the selection is somewhere odd (like within a table or textbox), then
you're going to get behaviour accordingly. It could also be affected by the
style of that paragraph.

1. Re-write your code without using the Selection object. Be specific about
where you want the table inserted, eg to put it at the end of document --

Dim pRange as Word.Range
Dim pTable as Word.Table

Set pRange = ActiveDocument.Range(ActiveDocument.End - 1,
ActiveDocument.End - 1)
Set pTable = ActiveDocument.Tables.Add(Range:=pRange, NumRows:=2,
NumColumns:=2 )
With pTable
.Columns(1).PreferredWidth = InchesToPoints(0.42)
:


2. Since you're applying a table style, you don't need any of the table
formatting instructions -- make these as part of the table style.
 

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