【问题标题】:Getting "Object variable or With block variable not set" on first use of document.TypeText with Outlook Message在第一次使用带有 Outlook 消息的 document.TypeText 时获取“对象变量或未设置块变量”
【发布时间】:2015-01-23 03:39:59
【问题描述】:

谁能帮我找出问题所在以及如何解决?

我正在尝试自动发送包含一些日常状态信息的电子邮件。我曾尝试从 Access 自动执行此操作,但在 Windows 8.1 64 和 Outlook 2013 中一直遇到 GetObject(, "Outlook.Application") 问题(已知但显然未解决)。所以我决定从 Outlook 开始自动执行。

无论如何,我将邮件消息创建代码移动到 Outlook vba 并让它启动 Access 并运行 Access 代码。在我开始创建邮件消息之前,这一切都很好。一切都很好,直到写入消息正文(使用 Word 作为正文编辑器)。在第一个“TypeText”命令中,我在标题中收到错误消息。如果我在错误通知对话框上单击调试,然后单步执行有问题的代码行,它就可以正常工作。我认为有一些时间问题,所以我在代码中等待了 2 秒。没运气。有问题的代码,以及与测试相关的其他一些奇怪的东西(特别是尝试输入然后删除文本),如下所示:

Public Sub CreateMetrics()
   ' Mail-sending variables
    Dim mailApp As Outlook.Application
    Dim accessApp As Access.Application

    Dim mail As MailItem
    Dim wEditor As Word.Document
    Dim boolCreatedApp As Boolean

    Dim i As Integer

    Set mailApp = Application

    ' Create an Access application object and open the database
    Set accessApp = CreateObject("Access.Application")
    accessApp.OpenCurrentDatabase dbLoc
    accessApp.Visible = True

    ' Open the desired form and run the click event hander for the start button
    accessApp.DoCmd.OpenForm ("ProcessStatus")
    accessApp.Forms![ProcessStatus].StartButton_Click

    ' Create the outgoing mail message
    Set mail = Application.CreateItem(olMailItem)
    mail.Display
    mail.BodyFormat = olFormatHTML
    Set wEditor = mailApp.ActiveInspector.WordEditor

    With accessApp.Forms![ProcessStatus]
        Debug.Print .lblToList.Caption
        Debug.Print .lblSubject.Caption
        Debug.Print .lblIntroduction.Caption
        Debug.Print .lblAttachFilepath.Caption
    End With

        mail.To = accessApp.Forms![ProcessStatus].lblToList.Caption
        mail.Recipients.ResolveAll

        mail.Subject = accessApp.Forms![ProcessStatus].lblSubject.Caption
        mail.Attachments.Add accessApp.Forms![ProcessStatus].lblAttachFilepath.Caption

        Sleep 2000

        ' Error occurs in the next line ***********************************************
        wEditor.Application.Selection.TypeText Text:="Test"
        wEditor.Application.Selection.HomeKey
        wEditor.Application.Selection.Delete Count:=4

        wEditor.Application.Selection.PasteSpecial DataType:=wdPasteBitmap
        wEditor.Application.Selection.HomeKey
        wEditor.Application.Selection.TypeText accessApp.Forms![ProcessStatus].lblIntroduction.Caption
        wEditor.Application.Selection.TypeText Text:=Chr(13) & Chr(13)
        wEditor.Application.Selection.EndKey

'        wEditor.Application.Selection.EndKey
'        wEditor.Application.Selection.TypeText Text:=Chr(13)
'        wEditor.Application.Selection.TypeText Text:=configs("EmailSignature")
'    End With

    With mailApp.Session.Accounts
        i = 1
        Do While i <= .Count
            ' Use either the specified email address OR the last outlook email address
            If RegEx_IsStringMatching(.Item(i).SmtpAddress, accessApp.Forms![ProcessStatus].lblSenderRegex.Caption) Or i = .Count Then
                mail.SendUsingAccount = .Item(i)
                i = .Count + 1
            Else
                i = i + 1
            End If
        Loop
    End With
    mail.Save

    accessApp.Quit
End Sub

【问题讨论】:

    标签: vba outlook email


    【解决方案1】:

    我在导致故障的行之前添加了一个“mail.Display”,这似乎错误地解决了问题。

    我现在通过在与我正在创建的电子邮件关联的文档上执行document.select 解决了这个问题。为了选择正确的文档(似乎无法保证哪个文档会在 wEditor.Application.Documents 集合中,尽管它通常是第一个文档),我创建了一个几乎肯定是唯一的文本并分配了它到电子邮件的正文,然后我可以去查找。这是我添加到上面代码中的新代码: 将 aDoc 调暗为 Word.Document 将 strUniqueID 调暗为字符串

     . . .
    
    mail.Attachments.Add accessApp.Forms![ProcessStatus].lblAttachFilepath.Caption
    strUniqueID = accessApp.Forms![ProcessStatus].lblSubject.Caption & Rnd(Now()) & Now()
    mail.Body = strUniqueID
    
    ' Search for the unique text. aDoc.Content has extra characters at the
    ' end, so compare only for the length of the unique text
    For Each aDoc In wEditor.Application.Documents
        If Left(aDoc.Content, Len(strUniqueID)) = strUniqueID Then
            aDoc.Select
            mail.Body = ""
        End If
    Next aDoc
    
    wEditor.Application.Selection.TypeText Text:="Test"
    . . .
    

    我查看了很多执行此类操作的代码示例。他们都没有进行选择,也没有说过需要一个。调试变得更加困难,因为在调用调试器时选择隐式发生。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 2013-07-01
      相关资源
      最近更新 更多