VBA script to read an excel, write the values into a word file, then format values as Heading1, Heading2 etc

I am writing a macro that will read an excel table and write the values in the table into an excel file. I would like the macro to take the values from column A and format them to Heading1 style, and the values from column B and format them to Heading2 style, and finally the values from column C should be just Normal style.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Sub magicmacro()
Dim rng As Range
Dim row As Range
Dim cell As Range
Set rng = Range("A1:D10")
'word file part
Dim DocApp As Object
Dim DocFile As Object
Dim DocName As String
On Error Resume Next
Set DocApp = GetObject(, "Word.Application")
If Err.Number = 429 Then
Err.Clear
Set DocApp = CreateObject("Word.Application")
End If
DocApp.Visible = True
'DocName = "C:UsersggujadhurDownloadsoutput.docx"
DocName = "C:Usersnicolas.jouffroyDownloadsoutput.docx"
If Dir(DocName) = "" Then
'MsgBox "File " & DocName & vbCrLf & "not found " & vbCrLf & "C:UsersggujadhurDownloads.", vbExclamation, "Document doesn't exist."
MsgBox "File " & DocName & vbCrLf & "not found " & vbCrLf & "C:Usersnicolas.jouffroyDownloads.", vbExclamation, "Document doesn't exist."
Exit Sub
End If
DocApp.Activate
Set DocFile = DocApp.Documents(DocName)
If DocFile Is Nothing Then Set DocFile = DocApp.Documents.Open(DocName)
DocFile.Activate
'Dim wordrange As Word.Range
'Set rng = wdDoc.Content
'word file part
For Each row In rng.Rows
For Each cell In row.Cells
cell.Copy
DocFile.Content.InsertAfter cell.Value
DocFile.Paragraphs.Last.Range.Style = wdStyleHeading1
DocFile.Content.InsertAfter vbCr
Next cell
Next row
'word file save etc.
DocFile.Save
'DocApp.Quit
Set DocFile = Nothing
Set DocApp = Nothing
Application.CutCopyMode = False
'word file save etc.
End Sub
</code>
<code>Sub magicmacro() Dim rng As Range Dim row As Range Dim cell As Range Set rng = Range("A1:D10") 'word file part Dim DocApp As Object Dim DocFile As Object Dim DocName As String On Error Resume Next Set DocApp = GetObject(, "Word.Application") If Err.Number = 429 Then Err.Clear Set DocApp = CreateObject("Word.Application") End If DocApp.Visible = True 'DocName = "C:UsersggujadhurDownloadsoutput.docx" DocName = "C:Usersnicolas.jouffroyDownloadsoutput.docx" If Dir(DocName) = "" Then 'MsgBox "File " & DocName & vbCrLf & "not found " & vbCrLf & "C:UsersggujadhurDownloads.", vbExclamation, "Document doesn't exist." MsgBox "File " & DocName & vbCrLf & "not found " & vbCrLf & "C:Usersnicolas.jouffroyDownloads.", vbExclamation, "Document doesn't exist." Exit Sub End If DocApp.Activate Set DocFile = DocApp.Documents(DocName) If DocFile Is Nothing Then Set DocFile = DocApp.Documents.Open(DocName) DocFile.Activate 'Dim wordrange As Word.Range 'Set rng = wdDoc.Content 'word file part For Each row In rng.Rows For Each cell In row.Cells cell.Copy DocFile.Content.InsertAfter cell.Value DocFile.Paragraphs.Last.Range.Style = wdStyleHeading1 DocFile.Content.InsertAfter vbCr Next cell Next row 'word file save etc. DocFile.Save 'DocApp.Quit Set DocFile = Nothing Set DocApp = Nothing Application.CutCopyMode = False 'word file save etc. End Sub </code>
Sub magicmacro()

Dim rng As Range
Dim row As Range
Dim cell As Range

Set rng = Range("A1:D10")

'word file part
Dim DocApp As Object
Dim DocFile As Object
Dim DocName As String

On Error Resume Next

Set DocApp = GetObject(, "Word.Application")

If Err.Number = 429 Then
    Err.Clear
    Set DocApp = CreateObject("Word.Application")
End If

DocApp.Visible = True
'DocName = "C:UsersggujadhurDownloadsoutput.docx"
DocName = "C:Usersnicolas.jouffroyDownloadsoutput.docx"

If Dir(DocName) = "" Then
    'MsgBox "File " & DocName & vbCrLf & "not found " & vbCrLf & "C:UsersggujadhurDownloads.", vbExclamation, "Document doesn't exist."
    MsgBox "File " & DocName & vbCrLf & "not found " & vbCrLf & "C:Usersnicolas.jouffroyDownloads.", vbExclamation, "Document doesn't exist."
    Exit Sub
End If

DocApp.Activate

Set DocFile = DocApp.Documents(DocName)

If DocFile Is Nothing Then Set DocFile = DocApp.Documents.Open(DocName)

DocFile.Activate

'Dim wordrange As Word.Range
'Set rng = wdDoc.Content

'word file part


For Each row In rng.Rows
  For Each cell In row.Cells   
    cell.Copy
    DocFile.Content.InsertAfter cell.Value
    DocFile.Paragraphs.Last.Range.Style = wdStyleHeading1
    DocFile.Content.InsertAfter vbCr
    
  Next cell
Next row

'word file save etc.
DocFile.Save
'DocApp.Quit

Set DocFile = Nothing
Set DocApp = Nothing
Application.CutCopyMode = False
'word file save etc.


End Sub

The script works for the excel file parsing and pasting into the word file, but it does not format the pasted text to Heading1.

Any help would be greatly appreciated. Thank you!

Nicolas

PS: I tried re-writing the code to test formatting to Bold the last paragraph, and that works. I also tried to program a selection, then change the style to Heading 1.

New contributor

Nicolas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật