Question Word.Document.Shapes.AddTextbox Anchor Property in Table

kslice917

New member
Joined
Jul 12, 2013
Messages
1
Programming Experience
5-10
Alright, so I've been searching around and racking my brain for days on this one. Basically, I'm creating a word document (from a template file) that contains a 10x3 table. In each cell, I have 5 textboxes and a picture. Using the following code (or at least similar), I am able to successfully add the textboxes and images to the entire first column. However, as soon as it switches to the 2nd column, the anchor property doesn't work properly anymore.

VB.NET:
        'First thing we need to do is copy all of the files we will need from our resources.
        Dim prog_location As String = My.Computer.FileSystem.CurrentDirectory                               'Set program location.
        IO.File.WriteAllBytes(prog_location & "\lg_labels.docx", My.Resources.lg_labels)                    'Copy large labels template.
        IO.File.WriteAllBytes(prog_location & "\sm_labels.docx", My.Resources.sm_labels)                    'Copy small labels template.
        My.Resources.lg_logo.Save(prog_location & "\lg_logo.png", System.Drawing.Imaging.ImageFormat.Png)   'Copy large logo.
        My.Resources.sm_logo.Save(prog_location & "\sm_logo.png", System.Drawing.Imaging.ImageFormat.Png)   'Copy small logo.


        'The next thing we MUST do is begin an instance of MS Word.
        Dim oWord As Word.Application               'Create word application.
        oWord = CreateObject("Word.Application")    'Create word application object.
        oWord.Visible = True                        'Show MS Word.


        'Now we will declare all of the objects we will need to use.
        Dim lg_doc As Word.Document                                         'Document for large labels.
        Dim sm_doc As Word.Document                                         'Document for small labels.
        lg_doc = oWord.Documents.Add(prog_location & "\lg_labels.docx")     'Template to use for large labels.
        sm_doc = oWord.Documents.Add(prog_location & "\sm_labels.docx")     'Template to use for small labels.


        Dim lg_table As Word.Table = lg_doc.Tables(1)                       'Set the large table.
        Dim sm_table As Word.Table = sm_doc.Tables(1)                       'Set the small table.
        Dim hor_offset As Single = oWord.InchesToPoints(0)                  'Set our horizontal offset.
        Dim ver_offset As Single = oWord.InchesToPoints(0.1)                'Set our vertical offset.
        Dim textbox1 As Word.Shape                                          'TextBox1
        Dim textbox2 As Word.Shape                                          'TextBox2
        Dim textbox3 As Word.Shape                                          'TextBox3
        Dim textbox4 As Word.Shape                                          'TextBox4
        Dim textbox5 As Word.Shape                                          'TextBox5
        Dim lg_logo_inline As Word.InlineShape                              'Large logo - inline
        Dim lg_logo As Word.Shape                                           'Large logo
        Dim sm_logo_inline As Word.InlineShape                              'Small logo - inline
        Dim sm_logo As Word.Shape                                           'Small logo
        Dim oRng As Word.Range                                              'Selection range
        Dim lg_doc_pages As Integer = Math.Ceiling(lg_stickers / 30)        'Number of pages needed for large stickers.
        Dim sm_doc_pages As Integer = Math.Ceiling(sm_stickers / 60)        'Number of pages needed for small stickers.


        Dim cur_lg As Integer = 1
        Dim cur_sm As Integer = 1


        Dim add_rows_lg As Integer = Math.Ceiling(lg_stickers / 3) - 10     'Add the required number of rows for the large table.
        For i As Integer = 1 To add_rows_lg Step 1
            lg_table.Rows.Add()
        Next
        Dim add_rows_sm As Integer = Math.Ceiling(sm_stickers / 4) - 15     'Add the required number of rows for the small table.
        For i As Integer = 1 To add_rows_sm Step 1
            sm_table.Rows.Add()
        Next


        'This is where our loops will start.
        'Begin with loop for large labels.
        For i As Integer = 1 To lg_doc_pages Step 1
            'For each page in the lg_doc
            For ii As Integer = 1 To 3 Step 1
                'For each column in the large table
                For iii As Integer = 1 To 10 Step 1
                    'For each row in the large table
                    If (cur_lg > lg_stickers) Then
                        Exit For
                    End If
                    If (ii = 1) Then
                        oRng = lg_table.Cell((10 * (i - 1)) + iii, 1).Range
                    ElseIf (ii = 2) Then
                        oRng = lg_table.Cell((10 * (i - 1)) + iii, 3).Range
                    ElseIf (ii = 3) Then
                        oRng = lg_table.Cell((10 * (i - 1)) + iii, 5).Range
                    End If
                    'Add our textboxes and logo image:
                    textbox1 = lg_doc.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, _
                                            oRng.Information(Word.WdInformation.wdHorizontalPositionRelativeToPage) + hor_offset + oWord.InchesToPoints(0.81), _
                                            oRng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage) + ver_offset + oWord.InchesToPoints(-0.03), _
                                                            oWord.InchesToPoints(1.83), oWord.InchesToPoints(0.33), oRng)
                    textbox2 = lg_doc.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, _
                                            oRng.Information(Word.WdInformation.wdHorizontalPositionRelativeToPage) + hor_offset + oWord.InchesToPoints(0.81), _
                                            oRng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage) + ver_offset + oWord.InchesToPoints(0.22), _
                                                            oWord.InchesToPoints(1.83), oWord.InchesToPoints(0.33), oRng)
                    textbox3 = lg_doc.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, _
                                             oRng.Information(Word.WdInformation.wdHorizontalPositionRelativeToPage) + hor_offset + oWord.InchesToPoints(0.81), _
                                             oRng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage) + ver_offset + oWord.InchesToPoints(0.42), _
                                                              oWord.InchesToPoints(1.83), oWord.InchesToPoints(0.33), oRng)
                    textbox4 = lg_doc.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, _
                                             oRng.Information(Word.WdInformation.wdHorizontalPositionRelativeToPage) + hor_offset + oWord.InchesToPoints(0.81), _
                                             oRng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage) + ver_offset + oWord.InchesToPoints(0.66), _
                                                              oWord.InchesToPoints(1.83), oWord.InchesToPoints(0.24), oRng)
                    textbox5 = lg_doc.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, _
                                             oRng.Information(Word.WdInformation.wdHorizontalPositionRelativeToPage) + hor_offset + oWord.InchesToPoints(0.81), _
                                             oRng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage) + ver_offset + oWord.InchesToPoints(0.78), _
                                                              oWord.InchesToPoints(1.83), oWord.InchesToPoints(0.24), oRng)
                    lg_logo_inline = lg_doc.InlineShapes.AddPicture(prog_location & "\lg_logo.png", False, True, oRng)
                    lg_logo = lg_logo_inline.ConvertToShape()
                    'Re-align textboxes and logo image:
                    textbox1.Top = oWord.InchesToPoints(-0.03)
                    textbox2.Top = oWord.InchesToPoints(0.22)
                    textbox3.Top = oWord.InchesToPoints(0.42)
                    textbox4.Top = oWord.InchesToPoints(0.66)
                    textbox5.Top = oWord.InchesToPoints(0.78)
                    lg_logo.Top = oWord.InchesToPoints(0.02)
                    lg_logo.Left = oWord.InchesToPoints(-0.02)
                    'Send logo image behind text:
                    lg_logo.ZOrder(Microsoft.Office.Core.MsoZOrderCmd.msoSendBehindText)

Some things to note: The reason the columns are listed as 1, 3, and 5 is because there are buffer columns at 2 and 4 to space them apart. As the code currently stands, it will add the textboxes and images to each cell in the first column. However, whenever 'oRng' is switched to either column 3 or 5, the textboxes are still anchored to column 1 (but the rows still work fine). However, the logo image works fine for both the column and row. What's really been giving me a headache lately is that the method for adding the logo and the method for adding the textbox is nearly the same and both have the Anchor property as Optional. But I just don't understand why the anchor works fine for the logo and not the textbox.

Mind you, the rest of the code (such as the Next statements) has been cut off as this contains sensitive data. Also, I apologize for the messing coding...I haven't gotten around to cleaning and shortening it up.

Any and all help on this issue is much appreciated!
 
Top Bottom