Excel to Word table creation - formatting issue

hannerz78

New Member
Joined
Feb 7, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a macro that does what I need it to, it takes data from an Excel table and writes the information in to word. At the moment in just creates a title based on the data and then creates and empty table underneath.

I want the imported text to be collapsable so have set it to use the Heading1 style which also works.

However, it is creating the table using the same font style, rather than normal. I've tried a couple of options like using wdStyleNormalTable and wdStyleNormal, but they either don't work, or change the formatting of the whole document.

Any thoughts would be appreciated.

VBA Code:
Const wdStyleHeading1 As Long = -2
    Const wdStyleNormalTable As Long = -106
    Const wdStyleNormal As Long = -1

For Each cell In rng
        ' Write the cell value as a heading in Word
        With wdDoc
            .Content.InsertAfter vbCrLf
            .Content.InsertAfter cell.Value
            .Content.InsertAfter vbCrLf
            .Paragraphs.Style = wdStyleHeading1
        End With
        
        
        
        ' Insert a table with 3 columns and 3 rows
       With wdDoc.Tables.Add(wdDoc.Bookmarks("\EndOfDoc").Range, 3, 3)
            For rowCounter = 1 To 3
                .Style = wdStyleNormalTable
                .Rows(rowCounter).SetHeight RowHeight:=20, HeightRule:=wdRowHeightExactly
                .Borders.InsideLineStyle = 1 ' wdLineStyleSingle
                .Borders.OutsideLineStyle = 1 ' wdLineStyleSingle
                .Borders.InsideColor = wdColorBlack
                .Borders.OutsideColor = wdColorBlack
                
            Next rowCounter
        End With
        
        
        ' Move to the next cell
        Set rng = rng.Offset(1, 0)
    Next cell
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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