none
Moving the anchor of the shape out of the Table RRS feed

  • Question

  • Hello Everyone,

    Programmatically, I have created a rectangle shape (Floating) on a selected text in the first row of the table. After creation, the Anchor of the Shape resides in the first row of the Table (I can say same paragraph of the first cell). Now, I want to move this anchor out of the Table. Manually, I can do it by just dragging the anchor and placing it before the table. But programatically, I am not getting any option to move this anchor out of the Table. Kindly anyone help me to do it using C#.

    Thanks,



    Shahab Abbasi

    Friday, June 10, 2016 12:58 PM

Answers

  • Yes, exactly, cutting the Anchor, shape is also moving too.

    By God's grace, I found the solution of this problem and it works well here. Of course, your some hints made it possible for me.

    Earlier, I was creating the Shape on the same range of the Table cell, which is resulting Anchor creation on the same paragraph of the cell. Now, with new approach, I first calculate the Horizontal and Vertical position of the content where the shape has to be created. Then I am moving the Range/Selection out of the table and using same coordinates, I created shape from there. This way, the anchor is created out of the Table and coordinates creates the shape on the same location.

    Here is the code:

    Microsoft.Office.Interop.Word.Range CurrRange = wdApplication.Selection.Range; //Getting the id of the MS Word shape to be inserted int shapeId = (int)MsoAutoShapeType.msoShapeRoundedRectangle; // Left coordinates float left = wdApplication.Selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdHorizontalPositionRelativeToPage); // Top coordinates float top = wdApplication.Selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdVerticalPositionRelativeToPage); // After calculating the coordinates, move the range up object unit_test = Word.WdUnits.wdLine; object count = 1; CurrRange.Tables[1].Select(); wdApplication.Selection.MoveUp(ref unit_test, ref count, ref obj_missing);

    // Now create the shape from the updated Range

    wdApplication.Selection.Range.Document.Shapes.AddShape(shapeId, left, top, 70, 15);

    Thanks, 


    Shahab Abbasi

    Monday, June 13, 2016 12:49 PM

All replies

  • Hi Shahab,

    >> I want to move this anchor out of the Table. Manually, I can do it by just dragging the anchor and placing it before the table

    Do you develop word add in? Do you want to move the top and left anchor or the entire rectangle shape? If you want to move top and left anchor, you could try to set ScaleWidth and ScaleHeight of the shape. If you want to move the entre shape, you could try IncrementLeft and IncrementTop.

    Here is a simple VBA code, you could modify it according C# object.

        ActiveDocument.Shapes(1).Select
        Selection.ShapeRange.IncrementLeft -10
        Selection.ShapeRange.IncrementTop -10
        Selection.ShapeRange.ScaleWidth 0.5, msoFalse, msoScaleFromBottomRight
        Selection.ShapeRange.ScaleHeight 1, msoFalse, msoScaleFromBottomRight

    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.


    Monday, June 13, 2016 2:41 AM
  • Hi Edward,

    Thanks for replying.

    I did try with the codes that you have provided, but both codes didn't work for me. Might be I didn't explained the problem properly.

    Have a look at the images below: 

    In the first Image, I have created a shape in the first row of the table. You can see that the anchor is at the same level.

    Now, manually I moved the anchor out of the table, then you can easily see that the anchor moved out of the table but shape is still at the same location, its anchor moved out the table.

    I want to achieve this programatically. The code that you suggested, is just changing the height, width, top and left positions of the shape, but the anchor every time remains at the same position. I want to change the position of the anchor. The reason why I am trying to move this anchor out of the table, because I will be creating these shapes on table headers, if my anchor is in the table, it will be repeated in the next page, when table breaks. If the anchor is out of the table then it will not repeated when the table breaks to the next page.


    Shahab Abbasi

    Monday, June 13, 2016 6:25 AM
  • Hi Shahab,

    It seems we could not move anchor directly. For a workaround, I suggest you cut and paste the anchor, at this time, the shape will be moved too, now, we could move the shape back to the original position.

    Here is a simple code, you could modify this code to your own requirement.

    Sub Macro52()
        ActiveDocument.Shapes(1).Anchor.Cut
        ActiveDocument.Tables(1).Select
        Selection.MoveUp Unit:=Word.WdUnits.wdLine, Count:=1
        Selection.Paste
        ActiveDocument.Shapes(1).Select   
        Selection.ShapeRange.IncrementTop 20
    End Sub

    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.


    Monday, June 13, 2016 10:06 AM
  • Yes, exactly, cutting the Anchor, shape is also moving too.

    By God's grace, I found the solution of this problem and it works well here. Of course, your some hints made it possible for me.

    Earlier, I was creating the Shape on the same range of the Table cell, which is resulting Anchor creation on the same paragraph of the cell. Now, with new approach, I first calculate the Horizontal and Vertical position of the content where the shape has to be created. Then I am moving the Range/Selection out of the table and using same coordinates, I created shape from there. This way, the anchor is created out of the Table and coordinates creates the shape on the same location.

    Here is the code:

    Microsoft.Office.Interop.Word.Range CurrRange = wdApplication.Selection.Range; //Getting the id of the MS Word shape to be inserted int shapeId = (int)MsoAutoShapeType.msoShapeRoundedRectangle; // Left coordinates float left = wdApplication.Selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdHorizontalPositionRelativeToPage); // Top coordinates float top = wdApplication.Selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdVerticalPositionRelativeToPage); // After calculating the coordinates, move the range up object unit_test = Word.WdUnits.wdLine; object count = 1; CurrRange.Tables[1].Select(); wdApplication.Selection.MoveUp(ref unit_test, ref count, ref obj_missing);

    // Now create the shape from the updated Range

    wdApplication.Selection.Range.Document.Shapes.AddShape(shapeId, left, top, 70, 15);

    Thanks, 


    Shahab Abbasi

    Monday, June 13, 2016 12:49 PM
  • Now new issue starts prompting.

    Earlier I was using Word 2010 and everything worked well, but when I move to Word 2013, I found that initially the Shape with proper anchors created, but suddenly, the header shrinks and shape moved out of the table. Word 2013 is not behaving properly. Do you have any comments on that?

    Thanks,


    Shahab Abbasi

    Tuesday, July 5, 2016 6:09 AM
  • Hi Shahab,

    For this new issue, I would suggest you post a new thread, and share us more information about your current issue like how to reproduce your issue and a screen shot about your current issue.

    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.


    Wednesday, July 6, 2016 3:09 AM