2014-09-23 66 views
0

任何人都可以幫我弄清楚怎麼回事,怎麼解決它?第一次使用document.TypeText和Outlook消息時獲取「Object variable或With block variable not set」

我試圖自動發送一封包含一些日常狀態信息的電子郵件。我試圖從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 

回答

0

我加了一個「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" 
. . . 

我看了很多的做這種事情的代碼示例。他們中沒有人執行過選擇或說任何關於需要的事情。調試更困難,因爲在調試器被調用時隱式選擇發生。

相關問題