none
Textbox Not Anchored to Table Cell in Word 2010 RRS feed

  • Question

  • Greetings,

    We just migrated from Word 2003 to Word 2010.  We have some VBA code that creates Word documents.  Since the migration, the textboxes that are supposed to be anchored and appearing within a table cell, are now appearing at the top left hand side of the page.  Keep in mind that this is all generated through code, not manually.  I have read in a few places that this is a known issue.  Can someone confirm this?  We would like to know of a good solution for this problem.  Before we pursue making changes to the code, I would like to know if Word 2013 also suffers from this problem, or, if there is an easy fix for this issue.

    Thanks.


    • Edited by Freedom321 Tuesday, April 11, 2017 6:35 PM
    Monday, April 10, 2017 9:45 AM

Answers

  • Hello,

    We don't have Office 2003 to test, but I test in Office 2007 using the following code and the Anchor works if the cell is not empty.

    Sub Demo()
    With ActiveDocument
       ActiveDocument.Shapes.AddTextbox Orientation:=msoTextOrientationHorizontal, Left:=1, _
        Top:=1, Width:=72, Height:=12, Anchor:=.Tables(1).Cell(1, 2).Range.Characters.First
    End With
    End Sub
    
     

    To get the same result in Office 2010 and above, we need to add the left and top distance of the cell when specify the left and top parameters.

    Sub Demo()
    With ActiveDocument
        tLeft = .Tables(1).Cell(1, 2).Range.Characters.First.Information(wdHorizontalPositionRelativeToPage)
        tTop = .Tables(1).Cell(1, 2).Range.Characters.First.Information(wdVerticalPositionRelativeToPage)
        cusLeft = 1 ' Left:=1 in Word 2007
        cusTop = 1 ' Top:=1 in Word 2007
        .Shapes.AddTextbox Orientation:=msoTextOrientationHorizontal, Left:=cusLeft + tLeft, _
        Top:=cusTop + tTop , Width:=72, Height:=12
    End With
    End Sub

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Freedom321 Thursday, April 27, 2017 10:43 AM
    Monday, April 24, 2017 6:04 AM
    Moderator

All replies

  • Hello,

    If you create them manually, would you get the same result?

    Please share your code here and we could try to reproduce the issue and see if there is any solution or workaround.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 11, 2017 1:45 AM
    Moderator
  • Thank you for your response.  I will put together some sample code and post.  I have been pulled away from this issue for the moment, but should have something in the next day or so.  I appreciate your help.
    Wednesday, April 12, 2017 10:08 AM
  • Here is some code that demonstrates the issue:

    Sub TestIssue()

    Dim Test_Node As Word.Shape
    Dim Node_Top As Double
    Dim Node_Left As Double
    Dim Node_Width As Double
    Dim Node_Height As Double
    Dim ttrNODE_ROW As Double
    Dim ttrNODE_COLUMN As Double
    Dim ttrNODE_BOX_WIDTH As Double
    Dim ttrNODE_BOX_HEIGHT As Double
    Dim Node_Row_Position As Double
    Dim Node_Column_Position As Double
    Dim Insert_Range As Word.Range
    Dim Translation_Table As Word.Table

    Node_Row_Position = 0.18
    Node_Column_Position = 0.25 - 0.02

    ttrNODE_BOX_WIDTH = 0.23
    ttrNODE_BOX_HEIGHT = 0.23

    ttrNODE_ROW = 1
    ttrNODE_COLUMN = 1

    Set Translation_Table = ActiveDocument.Range.Tables(1)

    'Create the new node in the position specified
    Node_Top = ActiveDocument.Application.InchesToPoints(Node_Row_Position - (ttrNODE_BOX_HEIGHT / 2))
    Node_Left = ActiveDocument.Application.InchesToPoints(Node_Column_Position - (ttrNODE_BOX_WIDTH / 2))
    Node_Width = ActiveDocument.Application.InchesToPoints(ttrNODE_BOX_WIDTH)
    Node_Height = ActiveDocument.Application.InchesToPoints(ttrNODE_BOX_HEIGHT)
    Set Test_Node = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, Node_Left, Node_Top, Node_Width, Node_Height, Translation_Table.Cell(ttrNODE_ROW, ttrNODE_COLUMN).Range.Paragraphs.First.Range)

    End Sub

    I confirmed that in Word 2003, the small square textbox appears within the table and in Word 2010 it appears in the upper left hand corner of the page.  Also, it seems that if the 2010 document is saved as a 97-2003 document, then it works.  We need it to work in Word 2010 or 2013 might be acceptable also.

    Thanks for any help or insights you can provide.

    Thursday, April 13, 2017 2:54 PM
  • We are able to put the textboxes into the table cells manually.
    Thursday, April 13, 2017 3:01 PM
  • Hello,

    The issue does exists in Office 2010 and above.

    i am looking into this issue to see how to work around the issue. There might be some time delay. Appreciate your patience.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 18, 2017 9:40 AM
    Moderator
  • Thank you, we appreciate your help very much.  Look forward to hearing from you.
    Tuesday, April 18, 2017 9:51 AM
  • Hello,

    Since the Anchor parameter would be omitted by Office, we need to re-calculate the postion. We need to add TopMargin and LeftMargin of the page when specifying location.

    For your orignial macro, use the following parameters.

    Node_Top = ActiveDocument.Application.InchesToPoints(ActiveDocument.PageSetup.TopMargin / 72 + Node_Row_Position - (ttrNODE_BOX_HEIGHT / 2))
    Node_Left = ActiveDocument.Application.InchesToPoints(ActiveDocument.PageSetup.LeftMargin / 72 + Node_Column_Position - (ttrNODE_BOX_WIDTH / 2))
    Node_Width = ActiveDocument.Application.InchesToPoints(ttrNODE_BOX_WIDTH)
    Node_Height = ActiveDocument.Application.InchesToPoints(ttrNODE_BOX_HEIGHT)
    Set Test_Node = ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, Node_Left, Node_Top, Node_Width, Node_Height)

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 19, 2017 9:49 AM
    Moderator
  • Thank you, I will implement this in our application and let you know how it goes.
    Wednesday, April 19, 2017 12:42 PM
  • Greetings,

    The code you posted does put the textbox in the same place as the code that I provided, but we need a solution to place the textbox relative to the table cell.  If the table were in a different place, then your code would have the textbox in an incorrect location.    Maybe I can get the location of the table and then calculate the position.

    Friday, April 21, 2017 12:46 PM
  • If I am able to come up with a solution I will post it here.  If any one else has any ideas that would be great.
    Friday, April 21, 2017 1:46 PM
  • Hello,

    We don't have Office 2003 to test, but I test in Office 2007 using the following code and the Anchor works if the cell is not empty.

    Sub Demo()
    With ActiveDocument
       ActiveDocument.Shapes.AddTextbox Orientation:=msoTextOrientationHorizontal, Left:=1, _
        Top:=1, Width:=72, Height:=12, Anchor:=.Tables(1).Cell(1, 2).Range.Characters.First
    End With
    End Sub
    
     

    To get the same result in Office 2010 and above, we need to add the left and top distance of the cell when specify the left and top parameters.

    Sub Demo()
    With ActiveDocument
        tLeft = .Tables(1).Cell(1, 2).Range.Characters.First.Information(wdHorizontalPositionRelativeToPage)
        tTop = .Tables(1).Cell(1, 2).Range.Characters.First.Information(wdVerticalPositionRelativeToPage)
        cusLeft = 1 ' Left:=1 in Word 2007
        cusTop = 1 ' Top:=1 in Word 2007
        .Shapes.AddTextbox Orientation:=msoTextOrientationHorizontal, Left:=cusLeft + tLeft, _
        Top:=cusTop + tTop , Width:=72, Height:=12
    End With
    End Sub

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Freedom321 Thursday, April 27, 2017 10:43 AM
    Monday, April 24, 2017 6:04 AM
    Moderator
  • Your solution works well, thank you very much!
    Thursday, April 27, 2017 10:43 AM