将HTML转换为Word并将文件超链接嵌入为附件
我有一些HTML文件,我正在用Word打开并保存为docx文件。我运行了一些宏来断开图形上的链接,以便它们嵌入到文档文件中,但我还没有找到任何方法将文件超链接转换为嵌入的附件 当我在word中打开htm文件时,如下所示:将HTML转换为Word并将文件超链接嵌入为附件,html,vba,ms-office,ms-word,Html,Vba,Ms Office,Ms Word,我有一些HTML文件,我正在用Word打开并保存为docx文件。我运行了一些宏来断开图形上的链接,以便它们嵌入到文档文件中,但我还没有找到任何方法将文件超链接转换为嵌入的附件 当我在word中打开htm文件时,如下所示: {HYPERLINK "C:\\xxxx\\test.zip \o "test.zip"} 在记事本中打开时,实际的html代码基本上如下所示 <a href="C:\xxxx\test.zip" title="test.zip"><img src="C:\
{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文件看起来像我刚刚手动拖入一个文件并将其附加到文档正文中。我认为最好的方法可能是将HTM文件解析为纯文本,而不是用word打开它,但我目前没有时间这样做。这可能会起作用,使用windows默认的ZIP图标位置
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
这是从插入文件手动完成的,不是吗?您是否尝试过使用宏录制器来获取此示例?(有时Word中的宏记录器是无用的…)看起来它使用Selection.InlineShapes.AddOLEObject ClassType:=“CompressedFolder”,但我不确定如何将现有的超链接转换为类似的内容。