2012-12-13 82 views
0

這是我在記事本中保存的代碼。我是否需要更改Excel.Applications?VBA在記事本中運行Excel中的代碼錯誤

Option Explicit 

Dim xlApp 
Dim xlBook 

Set xlApp = CreateObject("Excel.Application") 
Set xlBook = xlApp.Workbooks.Open("H:\shane.xlsm", 0, True) 

xlApp.Run "Email" 
xlBook.close 
xlApp.Quit 

Set xlBook = Nothing 
Set xlApp = nothing 

這是我必須發送的電子郵件的代碼,當我測試它的工作正常,並會發送給我一封電子郵件。

Option Explicit 

Const strTo As String = "[email protected]" 
Const strCC As String = "" '<~~ change "[email protected]" to "" if you do not want to CC 
Const strBCC As String = "" '<~~ change "[email protected]" to "" if you do not want to BCC 

Sub Email() 
Dim OutApp As Object, OutMail As Object 
Dim strbody As String, strSubject As String 

strSubject = "Hello World" 
strbody = "This is the message for the body" 

Set OutApp = CreateObject("Outlook.Application") 

Set OutMail = OutApp.CreateItem(0) 

On Error Resume Next 
With OutMail 
    .To = strTo 
    .CC = strCC 
    .BCC = strBCC 
    .Subject = "This is the Subject line" 
    .Body = strbody 
    .Send 
End With 
On Error GoTo 0 

Set OutMail = Nothing 
Set OutApp = Nothing 
End Sub 
+0

發佈您的代碼以向我們展示您嘗試失敗的內容。 –

+0

將宏放入Excel工作簿而不是記事本中。 – GolezTrol

+0

這是在另一個woorkbook中控制宏的最簡單方法嗎? –

回答

0

打開您的Excel文檔。打開VB編輯器。在左側窗口窗格中找到excel文檔。右鍵單擊並選擇插入>>模塊。將代碼移到新創建的模塊中。然後您只需使用方法名稱電子郵件即可調用它。因爲你已經在excel中了,所以你不需要去修改excel應用程序。 - Sorceri

+0

社區wiki。答案在評論中。任何在搜索中發現此主題的人都會看到有答案,並且更有可能尋找有希望的有用答案。 – niton