【问题标题】:Convert HTML to Word and embed file hyperlinks as attachments将 HTML 转换为 Word 并将文件超链接作为附件嵌入
【发布时间】:2015-03-30 03:13:41
【问题描述】:

我有一些 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 文件看起来像我刚刚手动拖入文件并将其附加到文档正文中。

【问题讨论】:

  • 这是从插入文件手动完成的,不是吗?您是否尝试过使用宏记录器来获取此示例? (有时Word中的宏记录器没用……)
  • 看起来它使用了 Selection.InlineShapes.AddOLEObject ClassType:="CompressedFolder" 但我不确定如何将现有的超链接转换为类似的东西。

标签: html vba ms-office ms-word


【解决方案1】:

我认为最好的方法可能是将 HTM 文件解析为纯文本,而不是用 word 打开它,但我目前没有时间这样做。这可能会起作用,使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 2019-03-21
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 2015-11-25
    相关资源
    最近更新 更多