Excel VBA文件自动生成带有错误的电子邮件:Microsoft Excel正在等待另一个应用程序完成ole操作

时间:2018-06-27 15:48:28

标签: excel vba excel-vba

我已经阅读了其他各种文章,并尝试修复(重写Word创建脚本以重新安装Office)无济于事。

当前解决方案:我正在使用Excel VBA和Microsoft Word文档作为电子邮件的基础自动生成电子邮件。我有3个表从我的Excel文件链接到Word文件。生成电子邮件的VBA与链接表所在的文件位于同一文件中。

使用Microsoft Word的原因在于,如果最终用户希望修改电子邮件的某些格式(因为他们不是代码素养),并且易于使用,则最终用户可以轻松使用HTML格式的HTML格式。电子邮件生成。

代码:

Option Explicit

Sub doEmail()

Dim sTo As String
Dim sCC As String
Dim sBCC As String
Dim sSub As String
Dim sBody As String
Dim strCC As String
Dim OutApp As Object
Dim OutMail As Object
Dim OutMailEditor As Object
Dim WordApp As Object
Dim WordDoc As Object
Dim varPress As Variant
Dim Today As Date
Dim TodayYear As Integer
Dim wdFile As String
Dim strMess As String
Dim strStyle As String
Dim strTitle As String

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

'Close all Word files currently open
CloseWordDocuments

'Declare variables
wdFile = Some file name
Today = Date
TodayYear = Year(Date)

'MsgBox to confirm sending of email
strMess = "You are about to generate the bi-weekly email." & vbCrLf & vbCrLf
strMess = strMess & "Do you wish to continue?"

strStyle = vbYesNo
strTitle = "PAY ATTENTION"
varPress = MsgBox(strMess, strStyle, strTitle)

If varPress = vbYes Then
        'Set the To and CC address bars
        sTo = Worksheets("Email").Range("R3").Value
        sCC = Worksheets("Email").Range("R4").Value
        'Set the subject
        sSub = "WK" & ISOweeknum(Today)

'Bind Word Application
        On Error Resume Next
    Set WordApp = GetObject(class:="Word.Application")
    On Error GoTo 0

    If WordApp Is Nothing Then
        Set WordApp = CreateObject(class:="Word.Application")
    End If

'Bind Outlook Application
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    Set OutMailEditor = OutMail.GetInspector.WordEditor

'Open Word Doc and copy into email Body
    Set WordDoc = WordApp.documents.Open(Filename:=wdFile)
    WordDoc.Content.Copy
    OutMailEditor.Range.Paste

'Compose the email
    With OutMail
        .To = sTo
        .CC = sCC
        .BCC = sBCC
        .Subject = sSub
        .display          ' This will display the email, but not send it
        '.Send            ' This will send the email
    End With

'Close all instances and free memory
WordApp.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
CloseWordDocuments
    Set OutMail = Nothing
    Set OutApp = Nothing
    Set WordApp = Nothing

End If

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

End Sub

这有时可能会起作用,但是基于测试,标记为“ Microsoft excel正在等待另一个应用程序完成ole action”错误时,命中率约为40%。

这在本地计算机上的两个文件都没有问题,我将Word Doc移到了共享驱动器上,但仍然没有问题。将Excel和Word Doc都移到共享驱动器后,我就遇到了问题。

据我所知,导致错误的挂起似乎是Word访问表的速度,但是通过Event Viewer查看时并没有提供任何细节。仅供参考,Excel文件刚刚超过11MB。

问题: 1)是否有解决此错误原因或降低频率的解决方案? 2)如果对Q1的回答是否定的,是否还有另一种方法可以生成电子邮件,从而使最终用户可以根据需要以友好的方式编辑格式?

0 个答案:

没有答案