Create and Customize a Word Table Doc using vb6

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ghaleon
    New Member
    • Aug 2013
    • 28

    Create and Customize a Word Table Doc using vb6

    Hi guys, I need to learn how to create a MS-Word Table using the language VB6.
    This is all I could do so far:

    Code:
    Private Sub Command1_Click()
    Dim Word_App            As Word.Application
    Dim Word_Doc            As Word.Document
    Dim Word_Table          As Word.Table
    Dim Word_Range          As Word.Range
    Dim iCount              As Integer
    
    Set Word_App = New Word.Application
    Set Word_Doc = Word_App.Documents.Add(DocumentType:=wdNewBlankDocument)
    Set Word_Table = Word_Doc.Tables.Add(Word_App.Selection.Range, 3, 5)
    Set Word_Range = Word_App.ActiveDocument.Content
    
    Set Word_Doc = Word_App.ActiveDocument
    Set Word_Table = Word_Doc.Tables.Add(Range:=Word_Doc.Range(Start:=0, End:=0), NumRows:=2, NumColumns:=2)
    iCount = 1
    
    Word_Doc.Tables(1).AutoFitBehavior (wdAutoFitContent)
    
    
    For Each Cell In Word_Table.Range.Cells
        Cell.Range.InsertAfter "Hello World " & iCount
        iCount = iCount + 1
    Next Cell
    
    
    Word_Doc.SaveAs FileName:="C:\p\Test"
    Word_Doc.Close False
    Word_App.Quit False
    
    End Sub
    I'd like to know if this is the best way to create the table. Also I'd like to know how to set the column Width, borders and colors and how may I change the space between cells ?

    Thanks
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    The easiest way to find out the code to do that is to open word and record a macro of you doing all that manually. Then you can just copy and modify the code the macro created for you.

    Comment

    • Ghaleon
      New Member
      • Aug 2013
      • 28

      #3
      How may I do that? Sorry I never used it in ms-word x-x

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        That depends on what version of word you are using. In 2007, you click developer and then click record macro.

        Comment

        • Ghaleon
          New Member
          • Aug 2013
          • 28

          #5
          It's Office 2007, I already checked the checkbox to show the Developer Bar.
          Now I have two options:
          Button & Keyboard. Wich one should I pick ? Also, where am I supposed to get the code after the record?
          Thanks

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            It doesn't matter which you pick because you don't actually need to save the code or document once you're done. You don't even have to pick one.

            After you finish recording, you can see the code for the macro by clicking macro. Select the macro you just recorded. Click edit. It will take you to the code.

            Comment

            • Ghaleon
              New Member
              • Aug 2013
              • 28

              #7
              Thanks, worked perfect !! But what IF I wanted to change manually the size of the rows and cells like Height and Width ? How could I do that?

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                You don't have to use the code it creates exactly. Just use it as a template for the methods and objecst you need to use. You can plug in variables for any of the hard-coded values such as the height and width.

                Comment

                • Ghaleon
                  New Member
                  • Aug 2013
                  • 28

                  #9
                  Thanks man, you're awesome !! Just one more little thing, I need to understand the syntax. When I'm ONLY creating the Table, it works perfect. But if I try to insert a picture before that I create the table. An error happens:
                  This is the code that I use to insert a picture.
                  Code:
                  Word_App.Selection.InlineShapes.AddPicture FileName:="C:\Documents and Settings\All Users\Documentos\Minhas imagens\Amostras de imagens\Inverno.jpg", SaveWithDocument:=True
                  When I use the Word_App again to insert values into the table, the error is fired. Why?
                  Code:
                  Word_App.Selection.TypeText Text:="Débito"
                  Word_App.Selection.MoveRight Unit:=wdCell
                  An error is fired, like:
                  The required member of the collection does not exists.

                  Why?

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    Most likely the selection has changed.

                    Comment

                    Working...