• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Keep Excel text formatting when transferring data to Word with Excel VBA

burtpri

New Member
Hello everybody!

I've posted this challenge here but no answer so far: https://www.mrexcel.com/board/threa...sferring-data-to-word-with-excel-vba.1134374/

Hopefully someone here will be able to help.

I'm using the bellow Excel VBA code to generate a WordDoc and then transfer text from my ExcelDoc to the WordDoc.

>>> use code - tabs <<<

Code:
Sub Generate_WordDoc()

Dim WordDoc As Object
Dim FileName, TagName, TagValue As String
Dim CustRow As Long
Dim WordContent As Word.Range

With Sheet1
Set wordapp = CreateObject("Word.Application")
      wordapp.Visible = True 
    Set WordDoc = wordapp.Documents.Open(FileName:="C:\File location\Name of file.docx", ReadOnly:=False)
        For CustRow = 2 To 4
        TagName = .Cells(CustRow, 1).Value
        TagValue = .Cells(CustRow, 2).Value
        With WordDoc.Content.Find
        .Text = TagName
        .Replacement.Text = TagValue
        .Wrap = wdFindContinue
        .Execute Replace:=wdReplaceAll
        End With
        Next CustRow   

End With
End Sub

It would work perfectly if wasn't by the fact that my WordDoc doesn't keep important formatting like paragraphs and bold text, as you can see in the images below:


1589732357001.png

-> What I have in my WordDoc:
1589732399794.png



Please note that in my WordDoc template I have only the start text (it's not merge fields, it's only text itself):
Company: <<Company>>
Address: <<Address>>
Phone: <<Phone number>>

I guess I need a "paste special" code or something like that but I can't find it...

Also, and if possible, I would like to not show in my WordDoc the text "Company: " if there's no Value on cell B2 of my ExcelDoc.

Hopefully someone will be able and kind to help me with this challenge.

If you read this far, thank you very much!
 
Last edited by a moderator:
Back
Top