2016-02-16 92 views
1

我是VBA的新手,並且遇到了一個錯誤,指出:「對象需要」。我試圖從列1中的Excel工作表中獲取所有單元格,但是當我運行我的代碼時,會引發該錯誤。對象嘗試獲取列數時需要的錯誤

這是我的代碼:

Public Sub emailList() 
    'Setting up the Excel variables. 
    Dim olApp As Object 
    Dim olMailItm As Object 
    Dim iCounter As Integer 
    Dim Dest As Variant 
    Dim SDest As String 

    'Create the Outlook application and the empty email. 
    Set olApp = CreateObject("Outlook.Application") 
    Set olMailItm = olApp.CreateItem(0) 

    'Using the email, add multiple recipients, using a list of addresses in column A. 
    With olMailItm 
     SDest = "" 
     For iCounter = 1 To WorksheetFunction.CountA(Workbooks("Book1.xls").Sheets(1).Columns(1)) 
      If SDest = "" Then 
       SDest = Range.Cells(iCounter, 1).Value 
      Else 
       SDest = SDest & ";" & Range.Cells(iCounter, 1).Value 
      End If 
     Next iCounter 

    'Do additional formatting on the BCC and Subject lines, add the body text from the spreadsheet, and send. 
     .BCC = SDest 
     .Subject = "FYI" 
     .Body = ActiveSheet.TextBoxes(1).Text 
     .Send 
    End With 

    'Clean up the Outlook application. 
    Set olMailItm = Nothing 
    Set olApp = Nothing 
End Sub 

最後一行拋出錯誤。我如何創建工作表對象?先進的謝謝你。我試過使用

Workbooks("Book1.xls").Sheet1.Columns(1) 

但這也會引發錯誤。

編輯:我正在編輯和運行Outlook中的代碼,並有一個打開的Excel窗口。

+0

工作簿(「Book1.xls」)。表(1).Columns(1) – Ralph

+0

@Ralph Sheet1將工作,如果工作表確實命名爲Sheet1,則默認爲Sheet1。 –

+0

您使用outlook展示的代碼以及您展示給您的問題所在的行使用Excel。你能顯示完整的代碼嗎? –

回答

3

您將需要添加對Excel對象庫的引用,這是在VBA編輯器中,在工具/添加引用下完成的。僅僅打開Excel是不夠的。

相關問題