none
Anchor of the Image not moving out of the Table in Word 2013 RRS feed

  • Question

  • Hello Everyone,

    I am working on Microsoft Word 2013 Add-In. I am creating a shape programatically over a certain piece of content in the first row, first column of the table. I am setting the anchor of the image on the paragraph before the table starts. In the first step, the shape applied on the right place and anchor placed on the right place (Image 1), but after few fluctuations in the Word document, the anchor moves inside the table again (Image 2). I checked the same functionality in the Word 2010 version and it's working as required, but in Word 2013, it's not working. 

    Here are the images showing the changes happening in the word document:

    Step 1:

    Step 2:

    Let me know, if there is any solution for that. 

    Thanks,


    Shahab Abbasi

    Monday, July 18, 2016 2:04 PM

All replies

  • Hi Shahab,

    >> after few fluctuations in the Word document, the anchor moves inside the table again (Image 2). I

    What do you mean with “few fluctuations”? Which operation make anchor move? It would be helpful if you could share us your code and simple steps, and then we could try to reproduce your issue.

    For a general suggestion, I suggest you check Shape.LockAnchor property whether it will work.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, July 19, 2016 11:50 AM
  • Hi Edward,

    Fluctuations means, some processes word did by itself like page numbering and after performing this shape operation, word re-paginates and looks like some fluctuations in the document and the anchor moves inside the table.

    Actually, we are creating a shape on the "-Continued" text on every first row, first column of the table. 

    Here is the code, that I have written:

    foreach (Word.Table wdTable in wdDocument_Formatted.Tables)
                    {
                        foreach (Word.Paragraph wdParagraph in wdTable.Rows[1].Cells[1].Range.Paragraphs)
                        {
                            Word.Range wdRangePara = wdParagraph.Range; wdRangePara.Find.MatchWildcards = true; wdRangePara.Find.Text = "[—–]Continued";
                            while (wdRangePara.Find.Execute())
                            {
                                object cstart = wdRangePara.Start; object cend = wdRangePara.End; Word.Range rng_continued = wdDocument_Formatted.Range(ref cstart, ref cend);
                                createShapeonContinued(rng_continued);
                                break;
                            }
                        }
                    }


    /// <summary>
            /// This Method actually creates a Shape on the Continued Text on the First Row, First Column of the Table: 
            /// </summary>
            /// <param name="wdRange">Continued Text range</param>
            public static void createShapeonContinued(Word.Range wdRange)
            {
    			wdRange.Select();
                int shapeId = (int)MsoAutoShapeType.msoShapeRoundedRectangle;
                float left = wdApplication.Selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdHorizontalPositionRelativeToPage);
                float top = wdApplication.Selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdVerticalPositionRelativeToPage);
                object unit_test = Word.WdUnits.wdLine; object count = 2; wdRange.Tables[1].Select();
                object obj_move = Word.WdMovementType.wdMove;
                wdApplication.Selection.MoveUp(ref unit_test, ref count, ref obj_move);
                Word.Range obj_range = wdApplication.Selection.Paragraphs[1].Range;
                object o_rangeStart = obj_range.Start; object o_rangeEnd = obj_range.End - 2;
                Word.Range obj_rangeNew = wdApplication.ActiveDocument.Range(o_rangeStart, o_rangeEnd);
                obj_rangeNew.Select();
                wdApplication.Selection.Document.Shapes.AddShape(shapeId, left, top, 65, 7.8f, obj_rangeNew);
                foreach (Word.Shape wdShap in wdApplication.Selection.Range.Document.Shapes)
                {
                    wdShap.Fill.ForeColor.RGB = 16777215; wdShap.Line.ForeColor.RGB = 16777215; wdShap.Line.Visible = MsoTriState.msoFalse;
                }
    		}

    Let me know, if you need anything.

    Thanks,


    Shahab Abbasi

    Tuesday, July 19, 2016 1:12 PM
  • Also, while googling this issue. I have found some blogs for the current issues in the Word 2013. Have a look at the image below:

    Thanks,


    Shahab Abbasi


    Tuesday, July 19, 2016 1:26 PM
  • Hi Shahab,

    >>word re-paginates and looks like some fluctuations in the document and the anchor moves inside the table

    Based on your last reply, it seems this is related with the feature of Word’s new Live Layout feature which makes Anchor position change.

    To check whether it is related with code or Word product feature, I would suggest you to achieve your requirement manually which you achieved by code. If it could be reproduced, I am afraid this is a product limitation.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, July 21, 2016 7:43 AM
  • Hi Edward,

    Yes, i did try to do that manually, and I was able to do it properly.

    I think, why I have been able to do that job, because after opening the word document, all the word internal processes (e.g: repagination, Live Layout updations) gets completed before I created the shape, because manually it took time to create that, whereas programatically, it performs this operation, before all the processes gets completed.

    Any suggestions to perform the same activity.

    Thanks,


    Shahab Abbasi

    Friday, July 22, 2016 6:01 AM
  • Hi Shahab,

    >> i did try to do that manually, and I was able to do it properly.

    Do you mean this issue only exist in code?

    >> because after opening the word document, all the word internal processes (e.g: repagination, Live Layout updations) gets completed before I created the shape,

    When did you create the shape? Did you create it in Startup event? If you add a button in the ribbon to achieve this function, will this issue still exist?

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Saturday, July 23, 2016 4:35 AM
  • Hi Edward,

    Yes, issue majorly occurring with the code.

    Actually, I have added a button on the ribbon that opens a word document which contains these tables and then the above code performs this action. I am not getting any issues while opening the same word document manually, because after opening the word document, it took sometime to create that image inside the tables, but with automation, after running the word document opening code, there is a code that immediately performs this action and then the anchor moved inside the table. One more thing I noticed, that after opening the word document, if I put some time delay code

    System.Threading.Thread.Sleep(4000);

    for few seconds/minutes before this method, then sometimes, the anchor stays at the right position. But with large number of pages in the document, it again fails and required few minutes.

    Thanks, 


    Shahab Abbasi

    Monday, July 25, 2016 6:12 AM
  • Hi Shahab,

    Did you try this option to fix anchor position?

    Select text box >> Fromat >> Position >> More Layout Options >> Enable Lock Anchor check box >> Ok

    Hope this will help you!


    .

    Monday, July 25, 2016 8:27 AM
  • Hi Sam,

    Yes, I did try with Lock Anchor as well, but nothing worked.

    Thanks,


    Shahab Abbasi

    Monday, July 25, 2016 9:17 AM
  • Hi Shahab,

    Based on your description, it seems your issue is related with image is not ready. If so, before moving anchor, I would suggest you check whether the image is ready or exist in a while loop. Or, I would suggest you create all the images first, and then moving anchor one by one. Or, I would suggest you split the function into two buttons.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, July 26, 2016 6:57 AM