Home > Software design >  Having issues exiting a table in Word using VBA
Having issues exiting a table in Word using VBA

Time:12-14

I am working on a macro that, among other thing, will need a table. I am testing a macro currently but cannot figure out how to exit the table. I tried the answer from here but it didn't work. I think I must be missing something.

`


Sub Macro2()
'
    With Selection
        .TypeText Text:="paragraph 1"
        .TypeParagraph
        .TypeText Text:="paragraph 2"
        .TypeParagraph
        ActiveDocument.Tables.Add Range:=.Range, NumRows:=4, NumColumns:= _
            2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
            wdAutoFitFixed
        With .Tables(1).Range
            For x = 1 To 8
                .Cells(x).Width = 180
            Next
        End With
    '    .Tables(1).Borders.Enable = False
        CellFormat
        .TypeText Text:="Sundance Senior Services"
        .MoveRight Unit:=wdCell
        CellFormat
        .TypeText Text:="Bella Pregnancy Center"
        .MoveRight Unit:=wdCell
        CellFormat
        .TypeText Text:="Compassion International"
        .MoveRight Unit:=wdCell
        CellFormat
        .TypeText Text:="Far Reaching Ministries"
        .MoveRight Unit:=wdCell
        CellFormat
        .TypeText Text:="The Ram Center"
        .MoveRight Unit:=wdCell
        CellFormat
        .TypeText Text:="Jail/Prison Ministries"
        .MoveRight Unit:=wdCell
        CellFormat
        .TypeText Text:="Funeral Services"
        
        .TypeParagraph
        .TypeText Text:="paragraph 3"
    End With
End Sub
Sub CellFormat()
    ListGalleries(wdBulletGallery).ListTemplates(1).Name = ""
    Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdBulletGallery).ListTemplates(1), ContinuePreviousList:= _
        False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
        wdWord10ListBehavior

End Sub

Thank you so much

I tried using the solution in the referenced post which is: `

Dim rngTable as Word.Range
Set rngTable = tbl.Range
rngTable.Collapse Word.WdCollapseDirection.wdCollapseEnd

` just before the type paragraph and "paragraph 3" text.

It didn't help. It still put the text in the cell where "Funeral Services" is.

CodePudding user response:

I got it! I got it! No sooner than I posted this and I figured out how to do it. It's hinky but it works. I added a paragraph after paragraph 2, then added a bookmark. Moved up a line, did all the table stuff then went to the bookmark. Works like a champ. The code (with the middle taken out):

    With Selection
    .TypeText Text:="paragraph 1"
    .TypeParagraph
    .TypeText Text:="paragraph 2"
    .TypeParagraph
    .TypeParagraph
    With ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:="docend"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    .MoveUp Unit:=wdLine, Count:=1
    
    ActiveDocument.Tables.Add Range:=.Range, NumRows:=4, NumColumns:= _
        2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
    With .Tables(1).Range
        For x = 1 To 8
            .Cells(x).Width = 180
        Next
    End With
'    .Tables(1).Borders.Enable = False
    CellFormat
    .TypeText Text:="Sundance Senior Services"
    .MoveRight Unit:=wdCell
    CellFormat
    .TypeText Text:="Bella Pregnancy Center"
    .MoveRight Unit:=wdCell
    CellFormat
    .TypeText Text:="Compassion International"
    .MoveRight Unit:=wdCell
    CellFormat
    .TypeText Text:="Far Reaching Ministries"
    .MoveRight Unit:=wdCell
    CellFormat
    .TypeText Text:="The Ram Center"
    .MoveRight Unit:=wdCell
    CellFormat
    .TypeText Text:="Jail/Prison Ministries"
    .MoveRight Unit:=wdCell
    CellFormat
    .TypeText Text:="Funeral Services"
    
    .GoTo What:=wdGoToBookmark, Name:="docend"
    
    .TypeParagraph
    .TypeText Text:="paragraph 3"
End With

End Sub

CodePudding user response:

Here is an alternative solution for you which avoids the shenanigans of a bookmark

Sub Macro2()
'
    Dim mySites As Variant
    mySites = Array("Sundance Senior Services", "Bella Pregnancy Center", "Compassion International", "Far Reaching Ministries", "The Ram Center", "Jail/Prison Ministries", "Funeral Services")
    Dim myRange As Range
    Set myRange = Selection.Range
    With myRange
    
        .InsertAfter "paragraph 1"
        .InsertParagraphAfter
        .InsertAfter "paragraph 2"
        .InsertParagraphAfter
        .Collapse direction:=wdCollapseEnd
        ActiveDocument.Tables.Add Range:=myRange, NumRows:=4, NumColumns:= _
            2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
            wdAutoFitFixed
        With .Tables(1).Range
        Dim x As Long
            For x = 1 To 8
                .Cells(x).Width = 180
            Next
        End With
  
        With .Tables(1).Range.Cells(1).Range
            
            
            Dim mySiteName As Variant
            For Each mySiteName In mySites
            
                .InsertBefore mySiteName
                'cellformat
                .Move unit:=wdCell
                
            Next
            
            .Collapse direction:=wdCollapseEnd
            .Move unit:=wdParagraph, Count:=2
            
            .InsertAfter "Paragraph 3"
            .InsertParagraphAfter
            
        End With
        
    End With
            
End Sub
  • Related