• Form to Word Tables (Excell 2000/SR-1 & VBA)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Form to Word Tables (Excell 2000/SR-1 & VBA)

    Author
    Topic
    #359603

    I’m using an Excel form to gather data that is then being passed to a complete Word Document. All of my form is working correctly except for two data hand-offs.

    When going to Word, I’m using Tables and code like this:
    WordDoc.Tables.Item(1).Rows.Item(7).Cells.Item(2).Select
    WordAp.Selection.Text = txtPurchStreet.Text

    Where I have to know the item/row/cell I’m pasting to.
    For this particular problem, I’m in a new section, so I’m assuming it’s a new item. I keep getting an error.

    I searched the archives on this but probably don’t know what to search for. Is there a way to tell what object (item) / Row/ Cell you are pasting to without trial and error? In other words, when I look at my Word form, is there a simple way to lable all the cells, etc on my form? Or to see that info?

    Viewing 1 reply thread
    Author
    Replies
    • #539483

      I think Table.Items(1) is the first table in the collection for whatever object it follows. Thus, if the object is the entire document, rather than a section, you would need to use the next table in the document. On the other hand, you could address each section individually:

      Sub TableCountsBySection()
      Dim intCounter As Integer
      With ActiveDocument
          For intCounter = 1 To .Sections.Count
              Debug.Print "Section " & intCounter & " contains " & _
                  .Sections(intCounter).Range.Tables.Count & " table(s)."
          Next
      End With
      End Sub

      You can retrieve a variety of properties of the table using the .Information(wdConstant) construction, including the total number of rows and columns. For example:

      ActiveDocument.Tables(1).Range.Information(wdMaximumNumberOfRows)
      ActiveDocument.Tables(1).Range.Information(wdMaximumNumberOfColumns)

    • #539521

      Just to tack on a couple of bits to Jefferson’s advice:

      You don’t need to use “Item” in this situation; for example instead of

      WordDoc.Tables.Item(1).Rows.Item(7).Cells.Item(2)

      use:

      WordDoc.Tables(1).Rows(7).Cells(2)

      The only common use for .Item that I can think of is when using a ‘With’ construct along the lines of:

      With ActiveDocument.Bookmarks
          .Item("bmk1").Range.Text = strText1
          .Item("bmk2").Range.Text = strText2
      End With
      

      Also, you don’t need to select the cell to write text to it; you can simply use:

      WordDoc.Tables(1).Rows(7).Cells(2).Range.Text = txtPurchStreet.Text
      

      Gary

    Viewing 1 reply thread
    Reply To: Form to Word Tables (Excell 2000/SR-1 & VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: