2013-04-12 50 views
0

我有下面的代碼,它工作正常,這一個(我用一個電話X,叫y宏),但是這一次不想上班前10個宏。Excel 2010中VBA錯誤。發送

Sub Send_Tips() 
Application.DisplayAlerts = False 
Dim Email_Subject, Email_Send_From, Email_Body As String, i As Integer 
Dim Mail_Object, nameList As String, o As Variant 
    Email_Send_From = "" 
    For i = 1 To 1 'use cells 1 to 1 in column "A" where names are stored 
If Sheets("Tips").Range("A1").Value <> "" Then 
    nameList = nameList & ";" & Sheets("Tips").Range("A" & i).Value 
End If 
Next 
    Set Mail_Object = CreateObject("Outlook.Application") 
    With Mail_Object.CreateItem(o) 
     .Subject = "Dealer Action Plans" 
     .To = nameList 
     .Body = "Please find attached the latest version of the Dealer Action Plans." 
     'Make a copy of the active sheet and save it to 
'a temporary file 
Worksheets("Tips").Activate 
ActiveSheet.Copy 
Set wb = ActiveWorkbook 
Filename = "Tips.pdf" 
Cells.Select 
Selection.Copy 
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
    :=False, Transpose:=False 

Worksheets("Tips").ExportAsFixedFormat _ 
Type:=xlTypePDF, _ 
Filename:="c:\aaaWork\Tips.pdf", _ 
Quality:=xlQualityStandard, _ 
IncludeDocProperties:=True, _ 
IgnorePrintAreas:=False, _ 
OpenAfterPublish:=False 

' ActiveWorkbook.SaveAs Filename:="C:\aaaWork\NC1.PDF", FileFormat:=17 

     .Attachments.Add "C:\aaaWork\Tips.PDF" 
     .send 'Will send straight away use .display to send manually 
     ActiveWorkbook.Close Saved = True 
     Kill "C:\aaaWork\Tips.PDF" 
End With 
    Application.DisplayAlerts = True 
End Sub 

我得到當行 。發送「將直接送走使用。顯示發送手動 是跑了以下錯誤消息。 運行時錯誤'2147467259(80004005)': Outlook無法識別一個或多個名稱

表名稱提示確實存在,有什麼想法?

謝謝!

+0

您的變量未正確聲明。這可能會幫助您調試。 –

+0

如果將'.send'更改爲'.display',那麼錯誤(或類似的錯誤)是否會持續? –

+0

此外,嘗試移動在其中創建臨時文件('工作表(「提示」)的代碼塊激活... OpenAfterPublish:= FALSE'爲*之前*'設置Mail_Object ...' –

回答

0

就跟進什麼我提到有關變量的聲明...當你這樣做:

Dim Email_Subject, Email_Send_From, Email_Body As String, i As Integer

你沒有指定類型Email_Subject, Email_Send_From所以這些被視爲Variant類型。同樣的,Mail_Object下一行:

Dim Mail_Object, nameList As String, o As Variant

重寫每低於這些線條,讓你的字符串變量是正確聲明:

Dim Email_Subject$, Email_Send_From$, Email_Body$, i As Integer Dim Mail_Object$, nameList As String, o As Variant

雖然這些並沒有引起這個特定的錯誤,如果變量設置不當,他們肯定會導致其他錯誤(例如,Variant可能包含一個對象或數組,而不是其他代碼需要的字符串,等等)。

否則,我會開始僅清理上面的代碼,然後用.Display代替.Send進行故障排除並確保在電子郵件中正確設置了所有內容。