Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-07-2016, 10:05 AM
slaycock slaycock is offline Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Windows 7 64bit Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Office 2013
Expert
Two problems: Applying bold affects other tables and canvas doesn't appear at specified range
 
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
  #2  
Old 12-07-2016, 02:09 PM
macropod's Avatar
macropod macropod is offline Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Windows 7 64bit Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The change vis-à-vis the bold effect is probably because you've defined the 'Table Text' Style with the 'automatically update' attribute.

I'm not seeing the errant canvas behaviour, but that may be due to not having the same page layout & content you're starting with.

The following works for me:
Code:
Sub sbInsertScientificResultsDiscussion()
Application.ScreenUpdating = False
With ActiveDocument
  With .Range
    .InsertAfter vbCr & "Results and Discussion" & vbCr
    .Paragraphs.Last.Previous.Style = "Heading 1"
    .Tables.Add Range:=.Characters.Last, numrows:=5, numcolumns:=3, _
        DefaultTableBehavior:=wdWord9TableBehavior
    With .Tables(.Tables.Count)
      .Range.InsertCaption Label:="Table", Position:=wdCaptionPositionAbove, _
          Title:=":" & vbTab & "Add table caption text" & vbCr
      .AllowAutoFit = False
      .BottomPadding = 3
      .TopPadding = 3
      .LeftPadding = 6
      .RightPadding = 6
      .PreferredWidth = CentimetersToPoints(15.5)
      With .Range
        .Style = "Table Text"
        .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
    End With
    .Paragraphs.Last.Style = "Notes"
    .InsertAfter Text:="a." & vbTab & "This is a footnote a. to the table" & vbCr
    .InsertAfter Text:="b." & vbTab & "This is a footnote b. to the table" & vbCr
    .Paragraphs.Last.Style = "Body Text"
  End With
  .Shapes.AddCanvas Left:=0, Top:=0, Anchor:=.Range.Characters.Last, _
      Width:=CentimetersToPoints(15.5), Height:=CentimetersToPoints(7.06)
  .Shapes(.Shapes.Count).WrapFormat.Type = wdWrapInline
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-07-2016, 02:29 PM
slaycock slaycock is offline Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Windows 7 64bit Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Office 2013
Expert
Two problems: Applying bold affects other tables and canvas doesn't appear at specified range
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

Hi Paul

Many thanks for taking the time to look at my code. I find it quite interesting reading your code for different approaches to mine.

I ran your code and it puts the Canvas above the heading "Results and Discussion" rather than below the table.
Reply With Quote
  #4  
Old 12-07-2016, 03:38 PM
macropod's Avatar
macropod macropod is offline Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Windows 7 64bit Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

As I said, in my testing, I wasn't getting such a result - the canvas follows the 'footnotes'. I'm curious, though as to what purpose the canvas serves and whether that purpose might be met by another means.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 12-07-2016, 10:55 PM
slaycock slaycock is offline Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Windows 7 64bit Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Office 2013
Expert
Two problems: Applying bold affects other tables and canvas doesn't appear at specified range
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

For the canvas I was following the lead from creating diagrams. I always ensure I create diagrams in a canvas rather than directly on the page. In this specific case the example I wanted to show was embedding a jpg in the canvas. This works well but I just can't get the canvas to go where the range specifies.

In the end I settled on embedding the jpg as an inlineshape.

My habit (inherited from the dim and distant days of word 2.0 is to put things like jpg inside a single cell table as historically this prevented a number of issues related to images wandering around the document.

I was trying to use a canvas as a more upto date method of doing the same thing and to lead by good(?) example.

For example, I received a document for reformatting from some clients in China. One of the diagrams is a nicely labelled chemical structure. Nice until you look a little closer when you find that the backbone of the structure is a jpg that is free floating and that care has been taken to label bits of the structure by overlaying text in 3 newspaper columns.
Incidentally, following on from your use of last.previous I explored some of the other options available for paragraph and to my delight found .insertparagraphafter. This replaces the need to use vbcrlf or vbcr aabd more importantly emulates the effect of pressing the return/enter key i.e. the style of the new paragraph is the next style as defined in the style definition as opposed to carring on the syle from the previous paragraph.

This means that

Code:
      
      .Style = ActiveDocument.Styles(garcStyleHeading1)
      .InsertAfter Text:="Results and Discussion" & vbCrLf
      .Collapse direction:=wdCollapseEnd
      .Style = ActiveDocument.Styles(garcStyleBodyText)
can be simplified to

Code:
      .Style = ActiveDocument.Styles(garcStyleHeading1)
      .InsertAfter Text:="Results and Discussion"
      .InsertParagraphAfter
Reply With Quote
  #6  
Old 12-08-2016, 02:23 AM
macropod's Avatar
macropod macropod is offline Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Windows 7 64bit Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

FWIW, my own preference is to insert graphics into a table cell with a fixed height & width. That way, I don't have to worry about resizing the object and the table itself provides a means for keeping captions etc. with the image. See, for example: https://www.msofficeforums.com/word/...html#post47919
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 12-08-2016, 02:28 AM
slaycock slaycock is offline Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Windows 7 64bit Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Office 2013
Expert
Two problems: Applying bold affects other tables and canvas doesn't appear at specified range
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

The entertainment continues.

I tested your code on my Surface Pro 4 last night (Windows 10 64 bit, Office 2016) and made the observations above.

I tested the code on my work laptop this morning it works fine but still puts the canvas above the table.

I rejigged the code and came across another 'anomaly'. The code below falls over at the .cells(2) statement with a 'The requested member of the collection is not found' error.

Code:
         With .Tables(.Tables.count)
            .AllowAutoFit = False
            .BottomPadding = 3
            .TopPadding = 3
            .LeftPadding = 6
            .RightPadding = 6
            .PreferredWidth = CentimetersToPoints(15.5)
            .Style = ActiveDocument.Styles("Table Grid")
            With .Range.Tables(1).Range
               .InsertCaption label:="Table", Position:=wdCaptionPositionAbove, Title:=":" & vbTab & "Add table caption text"
               .Cells.VerticalAlignment = wdCellAlignVerticalCenter
               .Cells(1).Range.Text = "Table Text test 2 + local effect: bold"
               .Cells(1).Range.Font.Bold = True
               .Cells(2).Range.Text = "Table text + local effect: Align left"
               .Cells(4).Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
               .Cells(5).Range.Text = "Table Text is centered by default"
               .InsertCaption label:="Table", Position:=wdCaptionPositionAbove, Title:=":" & vbTab & "Add table caption text"
            End With
However if I move the .insertcaption statement to after the .cells statements it works fine.

In the former case using select instead of the cells(2) statement the selected region includes the caption above the table.

Methinks something is not right.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
When changing table style in Word 2010, font size seems to change but doesn't show in new tables heartsoulliving Word 1 12-07-2016 05:17 PM
Two problems: Applying bold affects other tables and canvas doesn't appear at specified range After applying a title style, the row in a table doesn't break Salix Word 6 05-05-2016 12:48 PM
Alignment problems with decimals using tables in Publisher 2013 Beautiful Artist Publisher 0 02-24-2015 07:18 PM
Two problems: Applying bold affects other tables and canvas doesn't appear at specified range Problems with linked tables becoming corrupt word_madness Word 2 02-10-2015 04:00 AM
Tables from Excel to Word: Problems Guillo Word 0 11-19-2014 01:42 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:05 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft