1

我有以下代码来激活 Excel 2010 中的电子邮件窗口。我在另一个宏中有完全相同的代码,它在那里工作正常。在这两种情况下,宏都由活动工作表上的 CommandButton 调用。我无法弄清楚为什么一个工作簿而不是另一个工作簿的错误。想法?当我运行这个宏时,我得到了错误:

运行时错误“1004”:对象“_Workbook”的方法“EnvelopeVisible”失败

代码:

Private Sub CommandButton3_Click()

ActiveWorkbook.EnvelopeVisible = True

    With ActiveSheet.MailEnvelope
        .Item.To = "recipient"
        .Item.Subject = "some words"
        .Introduction = "some words"
    End With

End Sub
4

2 回答 2

1

想通了——似乎必须选择某些东西才能调用信封。而已。

于 2013-07-31T19:36:13.753 回答
0

我用其他一些替代方法解决了这个问题。

该问题是由于 Citrix 中的信封对象激活而发生的。我搜索并使用了 HTML 转换功能,我们将 Excel 范围转换为 HTML 并将其粘贴到邮件正文中。通过上述方法,我能够实现目标。

Function RangetoHTML(rng As Range)
' By Ron de Bruin.
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.ReadAll
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
于 2017-11-25T14:57:11.630 回答