PDA

View Full Version : Text to Word document in headers



Reiniervdijk
04-28-2020, 01:45 AM
Dear Sir/Madam,

How do I get a bold text cell into word as heading 1?
And normal text cell into Word as heading 2?

Thank you

gmayor
04-28-2020, 09:21 PM
You are going to have to provide more information about what it is that you are attempting to do. What, for example, is the relationship between the 'cell' and the document?

Reiniervdijk
04-29-2020, 02:25 AM
Oke sorry, this is my first post.

Well, I have a product list that will be different every time.
The list will be made in column A.
Sometimes excel cells are font bold or just normal text.

I want to make a macro that opens Word.
Checks the list and paste the cells in the following way;
Font bold cell will be pasted as Heading 1
New line
Normal cell - will be pasted as Heading 2

etc. etc. till the end of the list

Hopefully is this information more clear.

gmayor
04-29-2020, 04:38 AM
OK, then perhaps something like


Sub Macro1()
Dim wdApp As Object
Dim oDoc As Object
Dim oRng As Object
Dim xlSheet As Worksheet
Dim LastRow As Long, i As Long
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
wdApp.Visible = True
Set oDoc = wdApp.Documents.Add
Set oRng = oDoc.Range
oRng.collapse 0

Set xlSheet = ActiveSheet
With xlSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow 'If there's no header row start from 1
oRng.Text = .Cells(i, 1)
If .Cells(i, 1).Font.Bold = True Then
oRng.Style = "Heading 1"
Else
oRng.Style = "Heading 2"
End If
If i < LastRow Then oRng.InsertAfter vbCr
Set oRng = oDoc.Range
oRng.collapse 0
Next i
End With
Set oRng = Nothing
Set oDoc = Nothing
Set wdApp = Nothing
Set xlSheet = Nothing
End Sub

Reiniervdijk
04-29-2020, 04:45 AM
Damn, thank you very much. This is exact what I needed.

Reiniervdijk
04-29-2020, 05:03 AM
I have something extra actually.

So in column A we have the heading 1 or 2
The extra thing are the next columns.
There we have a subject in that we want in bold in the word file.
And when there is text in the cell crossing name and subject then the paste the text in the Word file as well.

If the cell is empty then the subject should not be transferd to word.

See the pictures in this post for more information.
Excel26487Word26488

Second is actually when there is this [cell] ([1,1]) ([r,c]) in a text cell.
Then print this in the Word document as the actual referred text.
Example
excel file; text text text [1,1] text text
word file; text text text example text text

If someone can help me with this you would be my hero.