Chesterdave
12-20-2016, 05:28 AM
Hiya,
The code below adds a row to a table and then sorts the table. The issue I have is that during this process it adds a carriage return to each row, I can get rid of the extra lines but the table will double in size briefly before they are removed. What this means is that once the table gets to a certain size it expands onto the next page shifting everything else following it, once the carriage returns are gone, it goes back to being one page and everything shifts again.
Hopefully its just my sloppy code that is the issue and someone will spot what is wrong.
Public Sub AddRow(ByRef strRef As String, strType As String)
Dim test As Boolean
Dim r As Integer
Dim s As Integer
strRef = Mid(strRef, 3, Len(strRef) - 2) 'creates the text for the first column e.g. 2a, 12a
strBMName = "BM_" & strRef & "N" & strType 'creates a bookmark name e.g. BM_2aNCr
Set oRow = ThisDocument.Tables(1).Rows.Add 'creates new row on the table
oRow.Cells(1).Range.Text = strRef 'Adds to first cell on last row (2A)
oRow.Cells(4).Range.Text = strType 'Adds to fourth cell on last row (R, M, O, Cr)
oRow.Cells(2).Range.Text = "N" 'Adds N to second cell on last row
Set oRng = oRow.Range
oRng.End = oRng.End - 2
oRng.Collapse wdCollapseEnd
ActiveDocument.Bookmarks.Add strBMName, oRng 'adds the bookmark name to the row
'add 0's to string to make all same length and for sorting
With ThisDocument.Tables(1)
For r = 2 To .Rows.Count
If Len(.Cell(r, 1).Range.Text) = 4 Then
.Cell(r, 1).Range.Text = "00" & .Cell(r, 1).Range.Text 'add 00's
ElseIf Len(.Cell(r, 1).Range.Text) = 5 Then
.Cell(r, 1).Range.Text = "0" & .Cell(r, 1).Range.Text 'add 0
End If
Next
End With
'sorts table
ThisDocument.Tables(1).Sort ExcludeHeader:=True, FieldNumber:="Column 1", SortFieldType:=wdSortFieldNumeric
'removes 0's from string and deletes carriage returns
With ThisDocument.Tables(1)
For s = 2 To .Rows.Count
If Left(.Cell(s, 1).Range.Text, 2) = "00" Then
.Cell(s, 1).Range.Text = Right(.Cell(s, 1).Range.Text, Len(.Cell(s, 1).Range.Text) - 2) 'removes 0's
.Cell(s, 1).Range.Text = Replace(.Cell(s, 1).Range.Text, Chr(13), "") 'removes carriage returns
ElseIf Left(.Cell(s, 1).Range.Text, 1) = "0" Then
.Cell(s, 1).Range.Text = Right(.Cell(s, 1).Range.Text, Len(.Cell(s, 1).Range.Text) - 1) 'removes 0's
.Cell(s, 1).Range.Text = Replace(.Cell(s, 1).Range.Text, Chr(13), "") 'removes carriage returns
End If
Next
End With
Countcheckboxes 'calls countcheckboxes
ThisDocument.UndoClear
End Sub
17878
The code below adds a row to a table and then sorts the table. The issue I have is that during this process it adds a carriage return to each row, I can get rid of the extra lines but the table will double in size briefly before they are removed. What this means is that once the table gets to a certain size it expands onto the next page shifting everything else following it, once the carriage returns are gone, it goes back to being one page and everything shifts again.
Hopefully its just my sloppy code that is the issue and someone will spot what is wrong.
Public Sub AddRow(ByRef strRef As String, strType As String)
Dim test As Boolean
Dim r As Integer
Dim s As Integer
strRef = Mid(strRef, 3, Len(strRef) - 2) 'creates the text for the first column e.g. 2a, 12a
strBMName = "BM_" & strRef & "N" & strType 'creates a bookmark name e.g. BM_2aNCr
Set oRow = ThisDocument.Tables(1).Rows.Add 'creates new row on the table
oRow.Cells(1).Range.Text = strRef 'Adds to first cell on last row (2A)
oRow.Cells(4).Range.Text = strType 'Adds to fourth cell on last row (R, M, O, Cr)
oRow.Cells(2).Range.Text = "N" 'Adds N to second cell on last row
Set oRng = oRow.Range
oRng.End = oRng.End - 2
oRng.Collapse wdCollapseEnd
ActiveDocument.Bookmarks.Add strBMName, oRng 'adds the bookmark name to the row
'add 0's to string to make all same length and for sorting
With ThisDocument.Tables(1)
For r = 2 To .Rows.Count
If Len(.Cell(r, 1).Range.Text) = 4 Then
.Cell(r, 1).Range.Text = "00" & .Cell(r, 1).Range.Text 'add 00's
ElseIf Len(.Cell(r, 1).Range.Text) = 5 Then
.Cell(r, 1).Range.Text = "0" & .Cell(r, 1).Range.Text 'add 0
End If
Next
End With
'sorts table
ThisDocument.Tables(1).Sort ExcludeHeader:=True, FieldNumber:="Column 1", SortFieldType:=wdSortFieldNumeric
'removes 0's from string and deletes carriage returns
With ThisDocument.Tables(1)
For s = 2 To .Rows.Count
If Left(.Cell(s, 1).Range.Text, 2) = "00" Then
.Cell(s, 1).Range.Text = Right(.Cell(s, 1).Range.Text, Len(.Cell(s, 1).Range.Text) - 2) 'removes 0's
.Cell(s, 1).Range.Text = Replace(.Cell(s, 1).Range.Text, Chr(13), "") 'removes carriage returns
ElseIf Left(.Cell(s, 1).Range.Text, 1) = "0" Then
.Cell(s, 1).Range.Text = Right(.Cell(s, 1).Range.Text, Len(.Cell(s, 1).Range.Text) - 1) 'removes 0's
.Cell(s, 1).Range.Text = Replace(.Cell(s, 1).Range.Text, Chr(13), "") 'removes carriage returns
End If
Next
End With
Countcheckboxes 'calls countcheckboxes
ThisDocument.UndoClear
End Sub
17878