【问题标题】:Excel VBA opens multiple Word applications leading to errorsExcel VBA 打开多个 Word 应用程序导致错误
【发布时间】:2020-03-24 23:41:15
【问题描述】:

我正在尝试在 Excel VBA 中部署一个程序来发送邮件、复制和粘贴 Word 文档中的文本。

我的程序运行,但在某个时候它会抛出错误,我看到有很多 Word 应用程序打开,所以我必须使用任务管理器关闭它们。我尝试使用Object.Quit 函数将对象设置为Nothing。

我认为程序的随机错误的根源在于我的计算机内存使用不当。我不知道如何使用计算机中的内存,因为我的背景与编程无关。

Sub CustomizedMail()

    Dim wd As Object, editor As Object
    Dim outlookApp As Outlook.Application
    Dim mymail As Outlook.MailItem
    Dim doc As Object
    Dim generalDirectory As String
    Dim document As String
    Dim ActiveRow As Integer
    Dim mailType As String

    Break = Chr(13) + Chr(10)

    'Selects address of letters to Clients
    generalDirectory = "C:\Users\Rodrigo\OneDrive - InBody Co., Ltd\Ventas Rod\Forecast\Ppts informativas x área\Para enviar\"
    'Selects document to be sent according to ppt type value in worksheet
    ActiveRow = ActiveCell.Row
    mailType = ActiveCell.Worksheet.Range("O" & ActiveRow).Value

    'Check mailType
    If mailType = "" Then
        MsgBox "Selecciona un tipo de mail"
        Exit Sub
    End If

    'Opens word document and copies its information
    document = generalDirectory & mailType & ".docx"
    Set wd = CreateObject("Word.Application")
    Set doc = wd.documents.Open(document)
    'wd.Visible = True
    doc.Content.Copy
    doc.Close
    'Set wd = Nothing

    'Opens Outlook and paste

    Set outlookApp = New Outlook.Application
    'CreateObject("Outlook.Application") 'New Outlook.Application
    Set mymail = outlookApp.CreateItem(olMailItem)
    With mymail
        On Error GoTo 1
        .To = ActiveCell.Worksheet.Range("N" & ActiveRow)
        If mailType = "Presentación" Then
            .Subject = "Bioimpedanciómetros profesionales InBody"
        Else
            .Subject = "Bioimpedanciómetros para " & mailType
        End If
        '.BodyFormat = olFormatRichText
        Set editor = .GetInspector.WordEditor
        editor.Content.Paste
        'editor.Quit
        Set editor = Nothing
        .Display
    End With
    'Append corresponding file
    sourceFile = generalDirectory & "INBODY - " & mailType & ".pdf"
    mymail.Attachments.Add sourceFile

    ActiveCell.Worksheet.Range("T" & ActiveRow).Value = "Yes"
    ActiveCell.Worksheet.Range("V" & ActiveRow).Value = Date
    'MsgBox ThisWorkbook.FullName
    'MsgBox ThisWorkbook.Path
    Exit Sub
1:          MsgBox "Excel se puso pendejo, intenta de nuevo"
End Sub

【问题讨论】:

  • 在您的代码中,您没有 wd.QuitSet wd = Nothing 被注释掉。这是故意的吗?
  • Set wd = CreateObject("Word.Application") 将在每次运行时创建一个新的 Word 实例。您应该做的是在Set wd = Word.Application 之前使用On Error Resume Next 并仅在发生错误时创建新实例(意味着没有加载的Word 实例)。 Wd.Quit 似乎没有效果的可能原因是因为您打开了太多实例,以至于关闭一个实例没有任何区别。对 Outlook 应用程序的处理也是如此。
  • 感谢您的回答,.Quit 和 wd = 没有任何评论,因为我进行了实验以查看它们是否有效,并且由于它们似乎在我的代码中不起作用,所以我让他们评论。我将尝试 On Error Resume Next。你知道为什么会发生错误吗?我认为计算机错误是代码问题的主要原因,我应该怎么做才能避免此类错误?

标签: excel vba ms-word


【解决方案1】:

您可以通过重用对象来解决很多问题。试试这样的:

Sub SendALotOfMails()
    Dim wd as Object
    Dim outlookApp as Object

    Set wd = CreateObject("Word.Application")
    Set outlookApp = New Outlook.Application

    ' Reusing word and outlook objects
    CustomizedMail wd, outlookApp
End Sub

Sub CustomizedMail(wd As Object, outlookApp as Object)
    ...
End Sub

这显然只是解决方案的一部分。

【讨论】:

  • 谢谢 Sam,当我用完这些对象后,我应该将它们设置为 Nothing,使用 Object.Quit 还是什么都不做?我昨天读到,当不使用对象时,VBA 会自动清理计算机上的内存,但是当我看到我的任务管理器时,我的代码不会发生这种情况哈哈
  • 在大多数情况下,VBA 确实会自行清理内存,但是当您启动 Word 或 Outlook 等大型复杂应用程序时,还有其他机制在起作用。不过,戒烟是件好事。
  • 谢谢,我注意到的其他想法是,当来自 word app 的 Incopu 文本时,有时副本是“无”(我在打开剪贴板管理器并看到一个空白区域时看到了这个,我想这与我的错误有关,有一刻我认为它可能会发生,因为我关闭 word app 太快了,您建议在什么时候关闭 word application?
  • 我不确定你的情况,所以我会尝试给出一个更通用的答案。像这样的典型脚本由设置部分、循环和结束部分组成。在设置中,您将启动应用程序、准备列表等。在循环中,您使用提供的对象来完成所有创建和发送。关闭部分以很好的方式关闭了所有内容。如果您在循环中过于用力地推动您的系统,那么在杀死它之前给它一些时间赶上它可能是个好主意。
猜你喜欢
  • 2020-08-28
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多