简体   繁体   中英

Word VBA Text then table then text then table

Hello I am trying to basically create a word document from scratch using excel vba with the word objects. The original document is 2 pages long. On the first page it has text, then table, then text, then another table. On the second page there is just text.

I have figured out part of it so far, however I have no idea how to move the cursor to add the new table. I am new to VBA

Sub CreateBasicWordReport()

    Dim wdApp As Word.Application
    Dim objRange
    Dim objDoc
    Dim Cell
    Dim NewRange

    Set wdApp = New Word.Application


    With wdApp
         .Visible = True
         .Activate

         .Documents.Add

                With .Selection
                     .ParagraphFormat.Alignment = wdAlignParagraphCenter
                     .BoldRun
                     .Font.Size = 11
                     .TypeText "Letter to Proceed with Transfer"
                     .TypeParagraph
                     .Font.Size = 11
                     .TypeText "Determination of Transfer Value and Request for Transfer"
                     .TypeParagraph
                     .TypeParagraph
                     .TypeParagraph

                     .ParagraphFormat.Alignment = wdAlignParagraphLeft


                End With



        End With

      Set objRange = wdApp.ActiveDocument.Range
      objRange.Collapse Direction:=wdCollapseEnd

      wdApp.ActiveDocument.Tables.Add objRange, 4, 2

      With wdApp.ActiveDocument.Tables(1).Cell(1, 1).Range

         .Bold = False
         .Text = "Date"

      End With

      With wdApp.ActiveDocument.Tables(1).Cell(2, 1).Range

         .Bold = False
         .Text = "Exportin"

      End With

      With wdApp.ActiveDocument.Tables(1).Cell(3, 1).Range

         .Bold = False
         .Text = "Importing"

      End With

      With wdApp.ActiveDocument.Tables(1).Cell(4, 1).Range

         .Bold = False
         .Text = "Re"

      End With

      With wdApp.ActiveDocument.Tables(1).Cell(1, 2).Range

         .Bold = False
         .Text = "Re"

      End With

      With wdApp.ActiveDocument.Tables(1).Cell(2, 2).Range

         .Bold = False
         .Text = "Re"

      End With

      With wdApp.ActiveDocument.Tables(1).Cell(3, 2).Range

         .Bold = False
         .Text = "Re"

      End With


      With wdApp.ActiveDocument.Tables(1).Cell(4, 2).Range

         .Bold = False
         .Text = "Re"

      End With


      objRange.Collapse Direction:=wdCollapseEnd

      wdApp.ActiveDocument.Range.InsertAfter "_____________________________________________________________________________________"
      wdApp.ActiveDocument.Range.InsertAfter "Part 1"

      wdApp.ActiveDocument.Bookmarks.Exists ("\EndOfDoc")


     ' wdApp.ActiveDocument.
      'Set NewRange = ActiveDocument.Content
      'NewRange.Collapse Direction:=wdCollapseEnd


      'wdApp.ActiveDocument.Tables.Add (

      'wdApp.ActiveDocument.Range.Collapse Direction:=wdCollapseEnd





      'objRange.Collapse Direction:=wdCollapseEnd

      'wdApp.ActiveDocument.Paragraphs.Add.Range.Text = "FGHFHDFg"

      'wdApp.ActiveDocument.Tables.Add NewRange, 4, 2

      'Set objTable = wdApp.ActiveDocument.Tabl
      'objTable.Cell(1, 1).Range.Text = "WOO"

      'wdApp.Documents.Add.Tables.Add wdApp.ActiveDocument.Range, 5, 4






End Sub

First Page

第一页

Second Page

第二页

Every Word document is based on a template. Every Word document also uses Styles to manage the formatting and layout. This is really, really basic Word stuff. You need to spend some time learning how Word works before rushing off to code anything.

A template is basically a document with all the standard 'boilerplate content' and, depending on your requirements, some variable content, saved as a template (eg a .dotx file). Clearly, you already have such a document, but saved as a template. You then create a new document by referencing the template via Documents.Add.

And, if you insert bookmarks wherever your variable inputs might go (before saving), it's a simple matter for your macro to instruct Word what to insert at those bookmarks - all without ever needing to move the cursor or select anything.

Cross-posted at: https://www.msofficeforums.com/word-vba/44567-text-then-table-then-more-text-table.html

For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM