将HTML转换为Word并将文件超链接嵌入为附件

时间:2014-06-26 20:10:50

标签: html vba ms-word ms-office word-vba

我有一些HTML文件,我用Word打开并保存为docx文件。我有一些宏来运行以打破图形上的链接,以便它们嵌入到doc文件中,但我还没有想出任何方法将文件超链接转换为嵌入式附件。

当我用word打开htm文件时,它看起来像这样:

{HYPERLINK "C:\\xxxx\\test.zip \o "test.zip"}

在记事本中打开时的实际html代码看起来基本上就像这个

<a href="C:\xxxx\test.zip" title="test.zip"><img src="C:\xxxx\0000002.gif" style="border-width: 0" /><span style="font-family: Arial; font-size: 9pt; color: #000000">test.zip</span></a>

.gif是一个代表zip文件的小图标。

理想情况下,我希望结束docx文件看起来像我只是手动拖入文件并将其附加到文档正文中。

1 个答案:

答案 0 :(得分:0)

我认为最好的方法可能是将HTM文件解析为纯文本,而不是用文字解析,但我现在没有时间这样做。这可能有效,使用ZIP图标的Windows默认位置。

Sub Macro2()

'Assume example structure like:
'"{HYPERLINK "C:\\xxxx\\test.zip \o "test.zip"}"

Dim htmLink As String
Dim fileName As String
Dim iconLabel As String

Dim v As Variant


'## Do you want to display the file icon?
showIcon = True

'## the htm "link" you need to change:
htmLink = "{HYPERLINK ""c:\\users\\david_zemens\\desktop\\1-8 test.zip \o ""1.8 test.zip""}"


'## reformat the link:
    ' first split it by space (assumes no spaces in the filename...
    v = Split(htmLink, """")

    'v(1) is the location of the file;
    ' you can verify in the Locals window
    fileName = Replace(v(1), Chr(34), vbNullString)
    fileName = Replace(fileName, "\\", "\")
    fileName = Replace(fileName, " \o", vbNullString)
    fileName = Trim(fileName)

    'v(2) is the filename:
    iconLabel = v(2)

'## Add to your document
' modify "Selection" to refer to any Word.Range, I think
    Selection.InlineShapes.AddOLEObject ClassType:="Package", fileName:= _
        fileName, LinkToFile:=False, _
        DisplayAsIcon:=True, IconFileName:="C:\Windows\system32\packager.dll", _
        IconIndex:=0, iconLabel:=iconLabel


End Sub