Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-29-2018, 01:32 PM
kkkwj kkkwj is offline what are the rules for how to anchor a shape? Windows 10 what are the rules for how to anchor a shape? Office 2016
Novice
what are the rules for how to anchor a shape?
 
Join Date: Nov 2017
Posts: 6
kkkwj is on a distinguished road
Default what are the rules for how to anchor a shape?

I'm trying to generate a series of pages in Word and add some shapes to each page. The code below works, but I don't understand why (or how) the shape-anchoring works.



The shapes get anchored to the new paragraph that I insert. If I don't insert that paragraph, all three sets of shapes end up stacked on each other on the last page in the series. Why don't the shapes anchor themselves to the paragraphs that I create when I insert the "New page" text? Why is a second new paragraph required? Thank you.

Code:
    npages = 3
    For i = 1 To 3
      If (i <= npages) Then
         Selection.Text = "New Page " + i
         Selection.Collapse wdDirection:=wdCollapseEnd
         
         ' the shapes get anchored to this paragraph below, not to the text above. Why?
         ' If I take out the paragraph, all 3 sets of shapes stack on each other,
         ' on the very last page of the set. Why?
         Selection.InsertParagraph
         'set shape = doc.shapes.addshape(rectangle,left,top,wdith,height)
         
         ' don't generate a new page after the last page of the series
         If (i < npages) Then
            Selection.InsertBreak Type:=wdPageBreak
         End If
      End If
    Next
Reply With Quote
  #2  
Old 06-30-2018, 04:12 AM
slaycock slaycock is offline what are the rules for how to anchor a shape? Windows 7 64bit what are the rules for how to anchor a shape? Office 2016
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

The code you posted doesn't run on my pc.

Selection.Text = "New Page " + i

should be

Selection.Text = "New Page " & i

and
Selection.Collapse wdDirection:=wdCollapseEnd


should be

Selection.Collapse Direction:=wdCollapseEnd


and
Selection.InsertBreak Type:=wdPageBreak

won't run because the selection at that point is the shape that has been inserted.

SO the conclusion is that you've never run the code that has been posted.

Can you post the actual code you are using.
Reply With Quote
  #3  
Old 06-30-2018, 08:37 PM
kkkwj kkkwj is offline what are the rules for how to anchor a shape? Windows 10 what are the rules for how to anchor a shape? Office 2016
Novice
what are the rules for how to anchor a shape?
 
Join Date: Nov 2017
Posts: 6
kkkwj is on a distinguished road
Default

First, let me agree with you. The code I posted was not the code I was running on my machine. Instead, it was the VBA pseudo-code that I cleaned up and made from the real code that I was running on my machine (shown below in C#). I thought it would offend people this VBA group if I posted VSTO code, even though the concepts would be easily recognizable (and my question was about concepts more than code, or so I thought).

Second, thank you for taking the time to even look at my post. I apologize if you feel that you have wasted your time. I hope that my paragraphs below will be a contribution in the right direction.

How Anchoring Works (at least for this case)

I started with a blank document and inserted some text. There was only one paragraph in the document, so that's the paragraph that the images anchored to. The default rule is that images anchor to the current column and the nearest previous paragraph containing the insertion point.

In my case, I thought moving to the next page by inserting a page break worked by (1) inserting the page break after the insertion point and then (2) moving across the newly inserted page break to the new page.

But that is not how things work. Page breaks are inserted before the insertion point, and the insertion point is now one page down from where it was originally. The anchor point for the paragraph containing the insertion point also moves down. It seems to me that the anchor point is the END of the anchor paragraph, not the beginning of it (at least when there's only one paragraph in the document).

So my code did the following: inserted some text; inserted some images that anchored to the end of the only paragraph in the document (the one containing the insertion point at the end of the text); inserted a page break (BEFORE the paragraph marker after the insertion point); and thereby moved both the insertion point and the images and the anchor to page 2.

The cycle repeated for my test case of three pages. I didn't know it at the time, but I had three sets of images on top of each other, which was confusing. It looked like only one set of images was being inserted, or that two sets were being overwritten. Not so. Next time, I think I'll offset the position of images in a loop and/or maybe cycle their colors too. That way, stacked images will be more obvious.

I found a hint of the problem and solution by reading a post by Cindy (MVP) in Europe from back in 2003? or so, where she suggested to someone else that they might need to insert a paragraph in their loop to avoid their problem (different than mine, but I suppose related).

So, now I know. Inserting a page break pushes your insertion point down, along with anything anchored to that paragraph. Insert a paragraph, then insert images to bind to that (now previous) paragraph. Then insert the page break.

Code:
public static void PageGen (DocSpec spec)
    {
      var npages = 3;
      var doc = Wapp.ActiveDocument;
      for (int i = 1; i <= npages; i++) {
        if (i <= npages) {
          // add text to the top of each page - do I need a paragraph for this?
          Wapp.Selection.Text = "New page" + " " + i.ToString();
          Wapp.Selection.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
          // this blank paragraph below is what the shapes get anchored to
          // they won't anchor to the selection text inserted above
          Wapp.Selection.InsertParagraph ();
          PageGenTopShape(spec);
          PageGenBottomShape(spec);
          // don't insert a page break on the last page
          if (i < npages) {
            Wapp.Selection.InsertBreak (Word.WdBreakType.wdPageBreak);
          }
        }
      }
    }
Reply With Quote
  #4  
Old 07-01-2018, 01:59 AM
slaycock slaycock is offline what are the rules for how to anchor a shape? Windows 7 64bit what are the rules for how to anchor a shape? Office 2016
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

Ah. That explains it.

The following vba code works

Code:
Sub shape_test()
Dim shape As shape
Dim i As Long
For i = 1 To 3
      
         Selection.Text = "New Page " & i
         Selection.Collapse Direction:=wdCollapseEnd
         
         ' the shapes get anchored to this paragraph below, not to the text above. Why?
         ' If I take out the paragraph, all 3 sets of shapes stack on each other,
         ' on the very last page of the set. Why?
         Selection.InsertParagraph
         Set shape = ActiveDocument.Shapes.AddShape(msoShapeRectangle, 100, 100, 300, 300)
         
         ' don't generate a new page after the last page of the series
        ' If (i < npages) Then
        Selection.Collapse Direction:=wdCollapseEnd
            Selection.InsertBreak Type:=wdPageBreak
        ' End If
     ' End If
    Next
End Sub
If you step through this code you can see the following in the word document

1. New Page 1 is inserted and highlighted as the selection range
2. Following the collapse the insertion point is on the left hand side of the paragraph marker.
3. Inserting a paragraph marker puts it on the left hand side of the existing paragraph marker and highlights the inserted paragraph marker as the selected range (as per MS help on .insertParagraph)
4. Inserting the shape puts the anchor at the start of the paragraph 'New Page 1'. The selection range is now the inserted shape.
5. The range is again collapsed so that it does not encompass the inserted shape
6. The insert break works fine and the whole process above is repeated for the next page.

I hope this makes things clearer.
Reply With Quote
  #5  
Old 07-01-2018, 08:29 AM
kkkwj kkkwj is offline what are the rules for how to anchor a shape? Windows 10 what are the rules for how to anchor a shape? Office 2016
Novice
what are the rules for how to anchor a shape?
 
Join Date: Nov 2017
Posts: 6
kkkwj is on a distinguished road
Default

Thank you for your reply. Yes, the steps you describe were _exactly_ what I did to solve the problem myself. I turned on the pilcrow marks in the word document, wrote single lines of VBA code, stepped through them, noted where the insertion point was, and traced how the selection moved around and collapsed as I went through things.

The good news is that anyone who ever reads this thread in the future will sure have a good explanation of how things work! :-) Best regards, Kevin
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word2010 Text Anchor - How To Do It? bluenite Word 3 05-07-2018 05:04 AM
How to change size / shape of a shape in a stencil tomgoodell Visio 1 06-30-2016 04:40 AM
what are the rules for how to anchor a shape? anchor addtextbox to new page Deltaj Word VBA 1 12-05-2014 01:56 AM
Text boxes don't anchor properly in PPT '08 noni PowerPoint 0 11-13-2011 09:16 AM
Question about Tools>Rules and Alerts>E-mail Rules RichGuard Outlook 0 09-02-2010 05:19 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:00 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