2016-02-17 16 views
0

我是VBA的新手,遇到了一個對我來說沒有意義的錯誤。我正在創建一個宏,該宏將進入工作簿並採用第一列中的所有條目,並使用條目作爲電子郵件地址創建電子郵件。這是我的代碼:嘗試獲取列中的條目數時,下標超出範圍

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 = Cells(iCounter, 1).Value 
      Else 
       SDest = SDest & ";" & 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 

行,上面寫着:

For iCounter = 1 To WorksheetFunction.CountA(Workbooks("Book1.xls").Sheets(1).Columns(1)) 

拋出一個下標超出範圍的錯誤,但我在第1列三個條目,所以我不明白爲什麼它拋出一個徹頭徹尾的邊界錯誤。

+0

可能意味着你沒有與確切名稱的工作簿...... –

+0

我這樣做雖然。我把它保存在我的Documents文件夾中,這是一個問題嗎? –

+0

是運行此代碼時打開的工作簿?如果不是,您需要先打開它。只要它是開放的,它在哪裏被保存並不重要。 –

回答

0

試試這個循環相反,假設電子郵件地址在行2.設置Excel的啓動對象第一

'setup excel objects 
Dim xlApp As Object 
Dim WB As Workbook 
Dim WS As Worksheet 
Set xlApp = CreateObject("Excel.Application") 
Set WB = Workbooks.Open("C:\Users\me\Desktop\Book1.xlsx") 
Set WS = WB.Worksheets("Sheet1") 
WB.Activate 

'loop through ColA to merge email addresses 
Dim last As Long 
last = WS.Range("A1").CurrentRegion.Rows.Count 
SDest = "" 
For i = 2 To last 
    SDest = SDest + Range("A" & i).Value & ";" 
Next i 
0

修正錯誤,並得到它的工作(謝謝你添)。我沒有添加一個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 
    Dim Excel As Object 

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

     'Create excel object. 
    Set Excel = CreateObject("excel.application") 
    Excel.Visible = True 
    Excel.Workbooks.Open ("C:\Users\Deryl Lam\Documents\Book1.xlsx") 
    Excel.Workbooks("Book1.xlsx").Activate 

    '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.xlsx").Sheets(1).Columns(1)) 
      If SDest = "" Then 
       SDest = Cells(iCounter, 1).Value 
      Else 
       SDest = SDest & ";" & 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