none
Adding Lines to a Page using Microsoft.Office.Interop.Word (2010 version) RRS feed

  • Question

  • I am developing a C# application which needs to prepare letters for bulk printing, enveloping and mailing.  For the enveloper machines to work correctly I need to add machine readable (OMR) marks at tightly defined positions on every odd-numbered page within the document (letters are printed duplex).

    I am using Microsoft.Office.Interop.Word v8.5.

    I don't have any control over the content of the letters I need to prepare, and am having a problem with where Word decides to place my lines.

    The key components of my code are:

    WordDoc.ActiveWindow.Selection.GoTo( Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, ref pageNo, Missing.Value );
    
    WordDoc.Shapes.AddLine( lineStart, position, lineEnd, position, Missing.Value );
    

    (where WordDoc is an instance of Microsoft.Office.Interop.Word.Document)

    This code goes to the required page and inserts a number of lines using the Shapes.AddLine method as above.

    The documentation of Shapes.AddLine states that the fifth argument is for "Anchor" - an optional Range object that represents the text to which the label is bound. If Anchor is specified, the anchor is positioned at the beginning of the first paragraph in the anchoring range. If this argument is omitted, the anchoring range is selected automatically and the label is positioned relative to the top and left edges of the page. (http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.shapes.addline(v=office.14).aspx)

    I get a problem when the page on which I want to add these OMR lines contains part of a table where the first cell on the page is a continuation from the previous page.  In this case, although I can successfully advance to my required page, the Shapes.AddLine method adds the line to the previous page i.e. where the top of the "continued" table cell sits.

    This only seems to occur on a Word 2010 native document.  If my document was created in an earlier version of Word, then the lines are added to the correct page, similarly if my document is saved as Word 2010 but in compatibility mode.

    Why has this behaviour changed for Word 2010?

    How can I change my code to ensure that the lines always print on the page I want them?

    I have tried setting a Range object to the current page, and using this as the Anchor when calling Shapes.AddLine, but this positions my lines relative to the start of the first paragraph on the page, not to the page itself.

    Any help would be greatly appreciated.

    Wednesday, January 23, 2013 10:47 AM

Answers

  • This article suggests changing Word2010 documents to Word2007 Compatibility Mode:

    http://social.msdn.microsoft.com/Forums/en-AU/worddev/thread/6fff35c7-25e6-46ab-be99-23bf88557e3f

    Doing this in my code resolves my issue:

    if( WordDoc.CompatibilityMode == ( int )Word.WdCompatibilityMode.wdWord2010 )
    {
        WordDoc.SetCompatibilityMode( ( int )Word.WdCompatibilityMode.wdWord2007 ); 
    }
    

    and I don't have to set Relative Horizontal/Vertical Position or reset my shape Top/Left properties.

    This suggests that Microsoft changed something between Word 2007 and Word 2010 although I haven't yet found it in any documentation.

    • Marked as answer by Dan Surridge Wednesday, January 23, 2013 5:05 PM
    Wednesday, January 23, 2013 5:02 PM

All replies

  • Hi Dan

    <<I have tried setting a Range object to the current page, and using this as the Anchor when calling Shapes.AddLine, but this positions my lines relative to the start of the first paragraph on the page, not to the page itself.>>

    You can change what controls a Shape's relative position from "Move with text" to the framework of the page (or margin). Look at the RelativeVerticalPosition property of the Shape object. For example Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 23, 2013 1:35 PM
    Moderator
  • Thanks, Cindy, but that changes the position of the lines if my source document is either Word 2003 or Word 2010 with Compatibility Mode.  Is there any way I can interrogate my document to determine if it is in Compatibility Mode?

    My real problem is with table cells that cross page boundaries and the way Word 2010 appears to have changed the way it determines where to anchor the Shape I am adding - it seems to be locating the first object on the page, moving to the top of that object and placing the anchor there, if the object is a cell and the top of the cell is on a previous page, the anchor is placed on that previous page, hence the Shape I am adding is also placed on that page, not on the page I need it to be.


    • Edited by Dan Surridge Wednesday, January 23, 2013 2:08 PM
    Wednesday, January 23, 2013 2:00 PM
  • <<but that changes the position of the lines if my source document is either Word 2003 or Word 2010 with Compatibility Mode.>>

    But you should be able to check those positions and put it back in the right place if they've changed?

    That will be a whole lot easier than worrying about which version of Word you're in, whether Compatibility Mode is in-force and whether a split table cell is at the top of the page.

    The other possibility would be to go through the various Compatibility options and track down which one is causing the different behavior. You'll find them in File/Options/Advanced, down at the very bottom - click a + to display the list. It might also help to switch between Word 2003 and Word 2010 to see what changes in the list (what's been added). If you can track that down, then your code can make sure the option setting is correct for the behavior you want before inserting your lines.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 23, 2013 2:57 PM
    Moderator
  • That does make a difference - if I reset the Top and Left properties of the shape, it goes back to the correct location on the page, I now get consistent results between native Word 2010 and the Compatibility versions.

    However, when I add the shapes to the page containing the continued table cell, although the anchor is set to the correct page when I call the AddShapes method, the shapes are added to the page that contains the beginning of the cell, but appear to be relative to the cell, not the page, even though the same code is being used.  Again, I get consistent results between native Word 2010 and the Compatibility version.

    Code is now:

    object anchor = WordDoc.ActiveWindow.Selection.GoTo( Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, ref pageNo, Missing.Value );
    Word.Shape omrLine;
    omrLine = WordDoc.Shapes.AddLine( lineStart, position, lineEnd, position, anchor );
    omrLine.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
    omrLine.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
    omrLine.Top = position;
    omrLine.Left = lineStart;
    
    • Edited by Dan Surridge Wednesday, January 23, 2013 4:40 PM
    Wednesday, January 23, 2013 4:38 PM
  • This article suggests changing Word2010 documents to Word2007 Compatibility Mode:

    http://social.msdn.microsoft.com/Forums/en-AU/worddev/thread/6fff35c7-25e6-46ab-be99-23bf88557e3f

    Doing this in my code resolves my issue:

    if( WordDoc.CompatibilityMode == ( int )Word.WdCompatibilityMode.wdWord2010 )
    {
        WordDoc.SetCompatibilityMode( ( int )Word.WdCompatibilityMode.wdWord2007 ); 
    }
    

    and I don't have to set Relative Horizontal/Vertical Position or reset my shape Top/Left properties.

    This suggests that Microsoft changed something between Word 2007 and Word 2010 although I haven't yet found it in any documentation.

    • Marked as answer by Dan Surridge Wednesday, January 23, 2013 5:05 PM
    Wednesday, January 23, 2013 5:02 PM
  • Hi Dan

    <<This suggests that Microsoft changed something between Word 2007 and Word 2010 although I haven't yet found it in any documentation.>>

    Glad you found a work-around :-) It would be unusual for you to find any documtation of this nature. That's why I suggested you test the compatibilty options, to find out what changed (or at least, what they call it).

    MS is always tweaking the way pages lay out, since way back in the 90s. That's why there are so durned many compatibilty options available to change! But they're seldom documented...

    You just have to keep in mind that making a document backwards compatible to an earlier version in a "global" manner, as you're doing, could lock out certain functionality available in the newer version(s). Changing a single option might not have that effect.

    It's possible that someone in the end-user "Community" may know specifically which option is involved. They get more into the general layout stuff...
      http://answers.microsoft.com/en-us/office/forum/word?tm=1358961742482


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 23, 2013 5:22 PM
    Moderator
  • When I changed the Layout options on the File / Options / Advanced tab to Word 2007, only 3 options were checked.  I tested the 3 individually and together (without setting Word 2007 compatibility in my code) and none of them made any difference - it must be another Word 2007 / 2010 difference.
    Wednesday, January 23, 2013 5:30 PM
  • Hi Dan

    That's nasty, then, and almost certainly won't be documented.

    If you're curious, you might be able to find out by saving two otherwise identical documents, then inspecting their Open XML. The Open XML SDK has a tool that makes that reasonably simple. If you can track down what elements in the XML are different that could lead you to something in the object model...


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 23, 2013 5:38 PM
    Moderator
  • Thanks very much for your help, Cindy.

    With regard to using Word 2007 Compatibility mode, I am aware of the constraints this imposes, but we may have to live with that.

    Thursday, January 24, 2013 9:44 AM