【问题标题】:VBA to Copy Contents from Embedded Word document and retain formattingVBA 从 Embedded Word 文档中复制内容并保留格式
【发布时间】: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

结束子`

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    如果将其转换为 HTML 可以工作(Word 生成的 HTML 非常难看...),您可以将其保存到临时文件中,然后将其备份:

    Sub HTMLExport()
        Dim Oo As OLEObject
    
        'Search for the embedded Word document
        For Each Oo In Sheet8.OLEObjects
            If InStr(1, Oo.progID, "Word.Document", vbTextCompare) > 0 Then
                Dim temp As String
                'GetSpecialFolder(2) gives the user's temp folder.
                temp = CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2) & "\temp.html"
                '10 = wdFormatFilteredHTML
                Oo.Object.SaveAs2 temp, 10
    
                'Copy the file contents into cell M1...
                Dim handle As Integer
                handle = FreeFile
                Open temp For Input As handle
                Sheet8.Range("M1").Value = Input$(LOF(handle), handle)
                Close handle
                '...and delete the temp file.
                Kill temp
    
                'Select any cell to close the document
                Sheet8.Range("M1").Select
                'Done
                Exit For
            End If
        Next
    End Sub
    

    请注意,如果您使用此方法,则在获取 OLEObject 后无法打开嵌入的 Word 文档,否则 Word 将不允许您保存它。

    【讨论】:

    • 谢谢。我认为这可能有效,但我收到“1004”无法获取 oleobject 类的对象属性“”错误。我似乎无法找到解决此问题的方法。有什么解决办法吗?
    • 这很奇怪。是否在此声明中:Oo.Object.SaveAs2 temp, 10
    • @Lee_Str - 你启用了 ActiveX 控件吗?
    • 是的,它位于Oo.Object.SaveAs2 temp, 10 行。是的,我的 ActiveX 控件已启用。我可以很好地使用按钮控件。
    • 继续对此进行研究,但找不到修复方法。做了一些研究,并找到了类似的解决方案。合并了两个,它们现在可以工作了。谢谢你的帮助共产国际。你让我走上了正确的道路!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    相关资源
    最近更新 更多