How do I insert HTML-Formatted Strings into a Microsoft Word Document using Visual Basic while preserving formatting?

前端 未结 4 1306
臣服心动
臣服心动 2021-01-02 15:42

I use Visual Basic and an automation interface to retrieve strings from an external application. These strings contain simple html formatting codes (, , et

相关标签:
4条回答
  • 2021-01-02 16:09

    I'm using 2016. The only thing that worked was Range.InsertFile(path). Pasting Special didn't work.

    0 讨论(0)
  • 2021-01-02 16:09

    AFAIK there is no builtin function to do that in VBA. You will have to write it yourself, which would be not too difficult if you restirct it to parse <b>, <i>, <a> and <p>, for example. All other tags would have to be ignored.

    0 讨论(0)
  • 2021-01-02 16:14

    Here's a link to add HTML to the clipboard using VB:

    http://support.microsoft.com/kb/274326

    Once you have the HTML on the clipboard, paste it into your word doc using something like this:

    ActiveDocument.Range.PasteSpecial ,,,,WdPasteDataType.wdPasteHTML
    

    This is pretty much the equivalent of you cutting and pasting it in manually.

    0 讨论(0)
  • 2021-01-02 16:21

    Use InsertFile

    Set objdoc = objInsp.WordEditor
    Set objword = objdoc.Application
    Set objsel = objword.Selection
    objsel.WholeStory
    vs_html = "<html><body>" + vs_body + "</body></html>"
    vs_file = "C:\temp\1.html"
    Call DumptoFile(vs_file, "", vs_html, False)
    RetVal = objsel.InsertFile(vs_file, , , False, False)
    
    0 讨论(0)
提交回复
热议问题