View Single Post
 
Old 06-30-2018, 08:37 PM
kkkwj kkkwj is offline Windows 10 Office 2016
Novice
 
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