I have a Word document and I need to copy some paragraph of it into a string in VBA. When doing so, the text formatting must be converted to HTML tags. For example if my paragraph looks like this:
Hello I am Alice.
I want to get a string that contains:
Hello I am <b>Alice</b>
(And it would be great if it also work for bulleted list and other kind of formatting).
I am using Microsoft Visual Basic for Applications 7.0. I am new to VBA and a lot of code I found on Internet does not work for me because my version is old. Unfortunately, downloading a more recent version is not an option in my case.
Here is a code sample that works to convert a paragraph to a string without formatting:
Dim pParagraph As Paragraph
'... at some point, pParagraph is set to a paragraph of the document
Dim pRange As Range
Dim pString As String
Set pRange = ActiveDocument.Range(Start:=pParagraph.Range.Start, End:=pParagraph.Range.End - 1)
pString = Trim(pRange.Text)
I did some research on Internet and found the advise to copy the Range to the clipboard and to use Clipboard.getText
. Unfortunately Clipboard.getText
does not even compile for me.
Clipboard.getText
which is why that won't compile. It does haveDataObject
which belongs to theMSForms
library.DataObject
can only retrieve text, however, and won't deliver the HTML or RTF. – Cindy Meister