VBA- Data from excel to word with heading style

ehero

New Member
Joined
Jul 3, 2022
Messages
3
Office Version
  1. 2007
Platform
  1. Windows
Hi everyone,
I need help with VBA excel
I have a table below

Untitled.png


I have a code to extract data from table to word and add hyperlink of the file, but I don't know how to add style heading level 1 to folder name and heading level 2 to file name so that they can be shown in Navigation pane.

Please help!

Thanks for reading

Oh, and here is my code:

Option Explicit
Sub InsertHyperLink()
Dim aWord As Object
Dim wDoc As Object
Dim i&, EndR&
Dim Rng As Range

Set aWord = CreateObject("Word.Application")
Set wDoc = aWord.Documents.Add
EndR = Range("A65536").End(xlUp).Row
For i = 9 To EndR

If Cells(i, 3) <> Cells(i - 1, 3) Then
wDoc.Range.InsertAfter Cells(i, 3) & vbCrLf
End If

wDoc.Range.InsertAfter vbCrLf
wDoc.Paragraphs(wDoc.Paragraphs.Count).Range.Hyperlinks.Add Anchor:=wDoc.Paragraphs(wDoc.Paragraphs.Count).Range, _
Address:=Cells(i, 5), TextToDisplay:=Left(Cells(i, 4), InStr(1, Cells(i, 4), ".") - 1)
wDoc.Range.InsertAfter vbCrLf
wDoc.Range.InsertAfter vbCrLf
Next
aWord.Visible = True

aWord.Activate 'Result
Set wDoc = Nothing
Set aWord = Nothing
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
ehero,

Here is the information about WdBuiltinStyle enumeration (Word)


Because Excel doesn't know about Word constants, those need to be declared at the top as follows.

Const wdStyleHeading1 As Long = -2
Const wdStyleHeading2 As Long = -3

Then you can use those in your code line, such as...

wDoc.Paragraphs(wDoc.Paragraphs.Count).Style = wdStyleHeading1

Hope this helps.
 
Upvote 0
ehero,

Here is the information about WdBuiltinStyle enumeration (Word)


Because Excel doesn't know about Word constants, those need to be declared at the top as follows.

Const wdStyleHeading1 As Long = -2
Const wdStyleHeading2 As Long = -3

Then you can use those in your code line, such as...

wDoc.Paragraphs(wDoc.Paragraphs.Count).Style = wdStyleHeading1

Hope this helps.
OMG, It's work properly. Thank you very much
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,206
Members
452,618
Latest member
Tam84

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