Consulting

Results 1 to 4 of 4

Thread: How to insert table in the next page?

  1. #1
    VBAX Newbie
    Joined
    Apr 2023
    Posts
    1
    Location

    Smile How to insert table in the next page?

    Hi all,

    I need to output tables and paragraph dynamically in the word document.

    I tried the code below, but cursor does not move to next page and nothing is output on the second page. Could you please take a look?
    thank you !

    'Dim wdApp As Object
    'Set wdApp = CreateObject("word.Application")
    'Early Binding
    Dim wdApp As Word.Application
    Set wdApp = New Word.Application
    With wdApp
          .Visible = True
          .Activate
          .Documents.Add
    With .Selection
         .Tables.Add _
         Range:=wdApp.Selection.Range, _
         NumRows:=1, NumColumns:=3, _
         DefaultTableBehavior:=wdWord9TableBehavior, _
         AutoFitBehavior:=wdAutoFitContent
         'Set table format 3 rows
         For counter = 1 To 12
              .TypeText Text:="Cell " & counter
              If counter <> 12 Then
                   .MoveRight Unit:=wdCell
              End If
         Next
    End With
    'Selection.GoTo What:=wdGoToBookmark, Name:="TC_Table"
    Selection.GoTo What:=wdGoToPage, Which:=lNextPage
    Selection.TypeText vbCrLf & vbCrLf
    Selection.TypeText vbCrLf & vbCrLf
    Selection.TypeText vbCrLf & vbCrLf
    Selection.TypeText "This is test."
    .Tables.Add _
    Range:=wdApp.Selection.Range, _
    NumRows:=1, NumColumns:=3, _
    DefaultTableBehavior:=wdWord9TableBehavior, _
    AutoFitBehavior:=wdAutoFitContent
    'Set table format 3 rows
    For counter = 1 To 12
         .TypeText Text:="Cell " & counter
          If counter <> 12 Then
                .MoveRight Unit:=wdCell
          End If
    Next
    End With
    Last edited by Aussiebear; 04-13-2023 at 12:15 PM. Reason: Added code tags to supplied code

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,053
    Location
    Welcome to the VBAX forum applevb. Please note that we normally wrap supplied code by using the # icon. Two methods available to you, either insert your text and highlight then click the # icon, or click the # icon and then insert your code.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    833
    Location
    Hi applevb and Welcome to this forum. I'll share some code that Macropod provided me. You put all your tables in the doc then use styles so the tables don't split your table on to different pages. At least I think that's what your after? Dave
    'prevent tables from splitting page
    Dim Otbl as Object, Ocel as Object
    For Each Otbl In WdApp.ActiveDocument.Tables
    Otbl.Range.Paragraphs.keepwithnext = True
    For Each Ocel In Otbl.Rows.Last.Range.Cells
    Ocel.Range.Paragraphs.Last.keepwithnext = False
    Next Ocel
    Next Otbl

  4. #4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •