2013-01-05 143 views
1

我剛剛開始學習VBA時遇到了新問題。運行時錯誤'1004'對象'_Global'的方法'行'失敗

現在我正面臨將郵件正文從Outlook導出到Excel的問題。有趣的是,當我第一次運行,它的工作原理。但是當我第二次運行時,出現我的標題中所述的錯誤信息。

我點擊了調試,並強調此代碼:「offsetRow =細胞(Rows.Count,1).END(xlUp).Row」

我已經試過喜歡選擇我想要的工作方式不同將數據粘貼到其中但無濟於事。因此,我希望這裏的專家能夠幫助我調試代碼。如果我已經完成了任何可能會減慢計算機速度的冗餘,也請隨時對我的代碼進行反饋。

僅供參考,這是爲了我的工作,以便我可以將電子郵件內容導出爲ex​​cel。提前致謝。

Sub ExportToExcel() 

Dim appExcel As Excel.Application 
Dim wkb As Excel.Workbook 
Dim wks As Excel.Worksheet 
Dim rng As Excel.Range 
Dim strSheet As String 
Dim strPath As String 
Dim intRowCounter As Integer 
Dim intColumnCounter As Integer 
Dim msg As Outlook.MailItem 
Dim nms As Outlook.NameSpace 
Dim fld As Outlook.MAPIFolder 
Dim itm As Object 
Dim masterData() As String 
Dim subData() As String 
Dim i As Integer 
Dim offsetRow As Long 

strSheet = "For fun.xlsx" 
strPath = "C:\Users\XXXXX\Desktop\New folder\" 
strSheet = strPath & strSheet 

Set nms = Application.GetNamespace("MAPI") 
Set fld = nms.PickFolder 

'Handle potential errors with Select Folder dialog box. 
If fld Is Nothing Then 
    MsgBox "Thank you for using this service.", vbOKOnly, "Error" 
    Exit Sub 
ElseIf fld.DefaultItemType <> olMailItem Then 
    MsgBox "Please select the correct folder.", vbOKOnly, "Error" 
    Exit Sub 
ElseIf fld.Items.Count = 0 Then 
    MsgBox "There are no mail messages to export", vbOKOnly, "Error" 
    Exit Sub 
End If 

'Open and activate Excel workbook. 
Set appExcel = CreateObject("Excel.Application") 
    appExcel.Workbooks.Open (strSheet) 
Set wkb = appExcel.ActiveWorkbook 
Set wks = wkb.Sheets("Sheet1") 

wks.Activate 
appExcel.Application.Visible = True 

'Copy field items in mail folder. 
For Each itm In fld.Items 
    Set msg = itm 
    masterData = Split(msg.Body, vbCrLf) 'Seperate according to lines 
    For i = 0 To UBound(masterData) 
     If masterData(i) = "" Then 
      'Do nothing 
     Else 
      'do the split here 
      subData = Split(masterData(i), vbTab) 
      wks.Activate 
      offsetRow = Cells(Rows.Count, 1).End(xlUp).Row 'This is where the error appears 
      If i = 0 Then 
       intRowCounter = i + offsetRow + 1 
      Else 
       intRowCounter = i + offsetRow 
      End If 
      For intColumnCounter = 0 To UBound(subData) 
       Set rng = wks.Cells(intRowCounter, intColumnCounter + 1) 
       rng.Value = subData(intColumnCounter) 
      Next intColumnCounter 
     End If 
    Next i 
Next itm 

Set appExcel = Nothing 
Set wkb = Nothing 
Set wks = Nothing 
Set rng = Nothing 
Set msg = Nothing 
Set nms = Nothing 
Set fld = Nothing 
Set itm = Nothing 

End Sub 
+0

試試這個'offsetRow = wks.Cells(wks.Rows.Count,1).End(-4162).Row' –

回答

0

我改變了:

offsetRow = Cells(Rows.Count, 1).End(xlUp).Row 

With wks 
    offsetRow = .Cells(.Rows.Count, 1).End(xlUp).Row 
End With 

和現在的工作。

+0

Chris Neilsen的回答是優化的可能性:保存兩行代碼完全相同:) –

5

你的問題,是因爲你沒有資格在Excel範圍引用

變化

offsetRow = Cells(Rows.Count, 1).End(xlUp).Row 'This is where the error appears 

offsetRow = wks.Cells(wks.Rows.Count, 1).End(-4162).Row 

BTW有很多的優化,可以是對此代碼完成

+0

@Siddharth - 沒有看到您的評論,我可能在您發佈時仍然打字。 (關於'-4162'的好處) –

+0

沒有憂慮Chris :) + 1爲了解釋它:) –

+0

順便說一句,如果OP在做早期綁定,那麼你也可以使用'xlUp'。我寫了'-4162'出於習慣:) –

相關問題