【发布时间】:2015-04-19 20:30:13
【问题描述】:
我正在使用带有嵌入 Word 文档的 Excel 2010。 Word 文档基本上是一个带有一些格式(粗体/下划线/超链接)的通信模板。
流程:用户打开 Excel 文档,向 Excel 提供输入,并完成模板。 Excel 中的输入与模板内容之间没有交互。
我正在尝试构建这个过程,这样一旦他们编辑了embedded Word Document,用户就会点击button。然后VBA 代码将获取embedded Word document 的内容并将其粘贴(格式化和所有)作为电子邮件的正文。该文件将自己附加到该电子邮件中,然后将其关闭以供批准。
我已经能够找到代码来帮助我到达那里,并在应该提供道具的地方提供道具,我找到了代码here(代码见下文)
但这不会保留 Word 文档的格式。有什么建议吗?也许如果我可以将 Word 内容提取为可以工作的 HTML。但不知道该怎么做。感谢所有帮助。
Sub Test()
Dim Oo As OLEObject
Dim wDoc As Object 'Word.Document
'Search for the embedded Word document
For Each Oo In Sheet8.OLEObjects
If InStr(1, Oo.progID, "Word.Document", vbTextCompare) > 0 Then
'Open the embedded document
Oo.Verb xlVerbPrimary
'Get the document inside
Set wDoc = Oo.Object
'Copy the contents to cell A1
wDoc.Content.Copy
With Sheet8.Range("M1")
.PasteSpecial xlPasteFormats
.PasteSpecial xlPasteValues
End With
'Select any cell to close the document
Sheet8.Range("M1").Select
'Done
Exit For
End If
Next
Set wDoc = Nothing
End Sub
在查看了 comintern 的代码后,我遇到了一个我无法解决的错误。我回到董事会并找到了一些额外的代码。合并两者似乎已经解决了它。
Sub HTMLExport()
Dim objOnSheet As oleObject
Dim strFileName As String
Dim sh As Shape
Dim objWord As Object ''Word.Document
Dim objOLE As oleObject
Sheet8.Activate
Set sh = ActiveSheet.Shapes("RA_Template")
sh.OLEFormat.Activate
Set objOLE = sh.OLEFormat.Object
Set objWord = objOLE.Object
ActiveSheet.Range("M1").Activate
''Easy enough
strFileName = CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2) & "\temp.html"
objWord.SaveAs2 Filename:=strFileName, FileFormat:=10 '10=wdFormatFilteredHTML
'Copy the file contents into cell M1...
Dim handle As Integer
handle = FreeFile
Open strFileName For Input As handle
Sheet8.Range("M1").Value = Input$(LOF(handle), handle)
Close handle
'Delete the Temp File (strFileName)
Kill strFileName
'Select any cell to close the document
Sheet8.Range("M1").Select
结束子`
【问题讨论】: