View Single Post
 
Old 12-07-2016, 10:05 AM
slaycock slaycock is offline Windows 7 64bit Office 2013
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default Two problems: Applying bold affects other tables and canvas doesn't appear at specified range

The code snippet below is causing my brain to overhead as I can't find the solution to problems

1. When the bold effect is applied to cells in the inserted table, it toggles the bold status of all other occurrences of the underlying style.

2. The inserted canvas always appears at the start of the previous page(hint, the insertion forces the caption to the following page).

Code:
Sub sbInsertScientificResultsDiscussion()
Dim myRange As Range
Dim myTable As Table
Dim myCanvas As Shape
 
Set myRange = ActiveDocument.StoryRanges(wdMainTextStory)
With myRange
  .Collapse direction:=wdCollapseEnd
  .Style = ActiveDocument.Styles("Heading 1")
  .InsertAfter Text:="Results and Discussion" & vbCrLf & vbcflf
  .Collapse direction:=wdCollapseEnd
  .Style = ActiveDocument.Styles("Table Text")
  Set myTable = .Tables.Add(Range:=myRange, numrows:=5, numcolumns:=3, DefaultTableBehavior:=wdWord9TableBehavior)
  myTable.Range.InsertCaption Label:="Table", Position:=wdCaptionPositionAbove, Title:=":" & vbTab & "Add table caption text" & vbCrLf
End With
With myTable
  .Range.Style = ActiveDocument.Styles("Table Text")
  .AllowAutoFit = False
  .BottomPadding = 3
  .TopPadding = 3
  .LeftPadding = 6
  .RightPadding = 6
  .PreferredWidth = CentimetersToPoints(15.5)
  With .Range
    .Cells.VerticalAlignment = wdCellAlignVerticalCenter
    .Cells(1).Range.Text = "Table Text + local effect: bold"
    .Cells(1).Range.Font.Bold = True
    .Cells(4).Range.Text = "Table text + local effect: Align left"
    .Cells(4).Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
    .Cells(5).Range.Text = "Table Text is centered by default"
  End With
  Set myRange = ActiveDocument.StoryRanges(wdMainTextStory)
  myRange.Collapse direction:=wdCollapseEnd
  myRange.Style = ActiveDocument.Styles("Notes")
  myRange.InsertAfter Text:="a." & vbTab & "This is a footnote a. to the table" & vbCrLf
  myRange.InsertAfter Text:="b." & vbTab & "This is a footnote b. to the table" & vbCrLf
  Set myRange = ActiveDocument.StoryRanges(wdMainTextStory)
  myRange.Collapse direction:=wdCollapseEnd
  myRange.Style = ActiveDocument.Styles("Body Text")
  myRange.InsertAfter vbCrLf
  myRange.Collapse direction:=wdCollapseEnd
 
  Set myCanvas = ActiveDocument.Shapes.AddCanvas(Left:=100, Top:=100, Width:=CentimetersToPoints(15.5), Height:=200, Anchor:=myRange)
  myCanvas.WrapFormat.Type = wdWrapInline
<other stuff here that probably isn't relevant>

The styles used are just smaller versions of body text (10pt for table Text and 9pt for Notes text)

I'd appreciate confirming if you have a similar problem or if my version of word is misbehaving.

Last edited by slaycock; 12-07-2016 at 10:10 AM. Reason: putting code in code markers
Reply With Quote