excel macro to build a table in word

gagey

New Member
Joined
Sep 20, 2016
Messages
10
Please help with the following code:
Code:
'add table
    Dim intNoOfRows As Integer
    Dim intNoOfColumns As Integer
    Dim objRange2
    Dim objTable


    intNoOfRows = 8
    intNoOfColumns = 7
    
    Set objRange2 = wdApp.Selection.Range
    wdApp.Selection.Range.tables.Add objRange2, intNoOfRows, intNoOfColumns
    Set objTable = wdApp.Selection.Range.tables(1)
    objTable.Borders.Enable = True
    
    For i = 1 To intNoOfRows
        For j = 1 To intNoOfColumns
            objTable.Cell(i, j).Range.text = "Sumit_" & i & j
        Next
    Next
    
    'add and format heading - "My Detailed Recommendations"
    wdApp.Selection.Font.Name = "Calibri"
    wdApp.Selection.Font.Size = 13
    wdApp.Selection.Font.Bold = True
    wdApp.Selection.typetext text:="My Detailed Recommendations"
    wdApp.Selection.typeparagraph

    'set paragraph to txtDetails text, format and add a blank line
    paragraph = UserForm1.txtDetails.text
    wdApp.Selection.Font.Name = "Calibri"
    wdApp.Selection.Font.Size = 11
    wdApp.Selection.Font.Bold = False

this code starting from (add and format heading) used to print the heading "My Detailed Recommendations" and then printed the text from a textbox in excel into the word doc. All of the paragraphs were printing correctly from their respective textbox in excel, however once I added the code to "add table" above this code, the heading and text from the excel text box are now incorporated into the table, which I don't want; I want the heading and paragraph to just print below the table. Help, I am really stumped!
 
Last edited by a moderator:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
The problem is that you're working with a Selection, which is rarely necessary, and you never change it. Also, it's not clear from your code what wdApp refers to; its name suggests it should refer to the Word Application, but your usage suggests you're applying it to a Word Document. That said, try something along the lines of:
Code:
Dim NumRows As Long, i  As Long
Dim NumCols As Long, j As Long
Dim objTable

NumRows = 8: NumCols = 7
    
With wdApp
    'add table
    Set objTable = .Tables.Add(.Range, NumRows, NumCols)
    With objTable
        .Borders.Enable = True
        For i = 1 To NumRows
            For j = 1 To NumCols
                .Cell(i, j).Range.Text = "Sumit_" & i & " " & j
            Next
        Next
    End With
    
    'add and format heading - "My Detailed Recommendations"
    With .Range.Paragraphs.Last.Range
        With .Font
            .Name = "Calibri"
            .Size = 13
            .Bold = True
        End With
        .Text = "My Detailed Recommendations" & vbCr
    End With
    
    'add and format UserForm1.txtDetails.Text
    With .Range.Paragraphs.Last.Range
        With .Font
            .Size = 11
            .Bold = False
        End With
        .Text = UserForm1.txtDetails.Text
    End With
End With
 
Upvote 0
Paul: I though the issue might be that I am using "selection"; sorry but I have never coded word before and am confused about "range". For all of the text paragraphs I used selection as I am not clear how to specify where in the word doc I want stuff to be printed. So I guess what I need to know is how do I specify exactly where in the word doc I want either the text printed or the table printed?
 
Upvote 0
how do I specify exactly where in the word doc I want either the text printed or the table printed?
That depends on what your document already contains and where you want to insert the new content. The use of 'Selection' isn't of itself the problem with where the data were going (it's just unnecessary and inefficient); it's what you were (not) doing with the Selection that was the problem. Presumably, you've already mostly worked out where you want the data to go and, to date, you've been working with Selection and moving it from time to time to achieve that end. The same can be done with range objects.
 
Upvote 0

Forum statistics

Threads
1,223,809
Messages
6,174,759
Members
452,581
Latest member
ruby9c

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top