2017-03-17 16 views
0

私の見通しにぶつかるすべての電子メールでスクリプトを実行しています。スクリプトは、指定されたExcel文書を開き、送信者名&の住所、件名、および日付を保存する必要があります。私は実行時エラー1004を取得しています:オブジェクト_globalのメソッド行は、受信したメールの一部で失敗しました。エラーが発生したコード内の行を指定しました。それは参照問題ですか?VBA - OutlookからExcelへ:オブジェクト_globalのメソッド行が失敗しました

Public Sub CoupaQueries(MItem As Outlook.MailItem) 

Dim objOutlook As Outlook.Application 

PersonName = MItem.SenderName 
PersonAddress = MItem.SendUsingAccount 
PersonSubject = MItem.Subject 
PersonDate = MItem.ReceivedTime 

Dim objExcel As Excel.Application 
Dim wks As Excel.Worksheet 
Dim wkb As Excel.Workbook 

Set objExcel = New Excel.Application 

objExcel.Workbooks.Open ("C:\Users\a222012\Desktop\CoupaQueries.xlsx") 
objExcel.Visible = True 
Set wkb = objExcel.ActiveWorkbook 
Set wks = wkb.Sheets("Sheet1") 

'Error occurs on the next line 

wks.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = PersonName 
wks.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Value = PersonAddress 
wks.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Value = PersonSubject 
wks.Cells(Rows.Count, 4).End(xlUp).Offset(1, 0).Value = PersonDate 

objExcel.ActiveWorkbook.Save 
objExcel.Quit 

End Sub 
+1

'wks.Rows.Count'だけではなく、' Rows.Count' – gizlmo

+1

リファレンスブックを使用してみてください'wkb = objExcel.Workbooks.Open(" C:\ Users \ a222012 \ Desktop \ CoupaQueries.xlsx ")'を設定します。それを開いた後に分割されたアクティブワークブックではないかもしれないという小さなチャンスがあります。 @ gizlmo氏によると、Outlookは 'Rows.Count'が何であるか理解していない。 –

+0

@gizlmoに感謝します。問題を修正したようです。 – KoderM16

答えて

0

私はこれがより合理的に見えるR.を設定するための数式を疑い、例えば、それが発生したエラーのショーをできるように

Dim R As Long 

R = Wks.Cells(Rows.Count, 1).End(xlUp) + 1 
Wks.Cells(R, 1).Value = PersonName 
Etc 

をコードを簡素化: -

R = Wks.Cells(Rows.Count, 1).End(xlUp).Row + 1 
2

クライアントアプリケーションがOutlookであるため、暗黙的にExcelアプリケーションオブジェクトモデルが参照されていないため、明示的にrリファレンスすべてのExcelオブジェクト(たとえば、Worksheetオブジェクトは)With object - End With構文はに助けに来て両方このようなエラーを回避し、あなたのオブジェクトモデルの取り扱いをより把握(意識)を得ることができ、そのメンバー(たとえば、そのRowsプロパティ)

にアクセスするにはあなたはどんなオブジェクトを参照たら、あなたはインスタンスのように、離れてすべてのメンバー(プロパティ、メソッド、列挙型)シンプルなドット(.)を持っている場合は、:

Public Sub CoupaQueries(MItem As Outlook.MailItem) 
    Dim objExcel As Excel.Application 
    Dim objOutlook As Outlook.Application '<--| not needed, since you' are in Outlook its object model is implicitly referenced 
    Dim PersonName As String, PersonAddress As String, PersonSubject As String, PersonDate As Date 

    With MItem '<--| reference passed MItem object 
     PersonName = .SenderName 
     PersonAddress = .SendUsingAccount 
     PersonSubject = .Subject 
     PersonDate = .ReceivedTime 
    End With   

    Set objExcel = New Excel.Application '<--| get a new instance of Excel 
    objExcel.Visible = True '<--| not necessary 
    With objExcel.Workbooks.Open("C:\Users\a222012\Desktop\CoupaQueries.xlsx").Sheets("Sheet1") '<--| get and reference an instance of "Sheet1" sheet of wanted workbook 
     With .Cells(.Rows.Count, 1).End(xlUp) '<--| reference referenced sheet column A first empty cell after last not empty one 
      .Offset(1, 0).Value = PersonName 
      .Offset(1, 1).Value = PersonAddress 
      .Offset(1, 2).Value = PersonSubject 
      .Offset(1, 3).Value = PersonDate 
     End With 
     .Parent.Save '<--| save parent object of currently referenced object: being this latter a worksheet, its parent object is the workbook it belongs to 
    End With 
    objExcel.Quit 
    Set objExcel = Nothing '<--| release application variable 
End Sub 
さらに

は、あなたがこれを実行する必要があります多くの郵便物の上のループ内のマクロあなたが完了したら、それと密接全体に使用し、ループを開始する前に1つのExcelの参照を得ることができます:

Sub main() 
    Dim iMail As Long, nMails As Long 
    Dim MItem As Outlook.MailItem 
    Dim objExcel As Excel.Application '<--| declare an Excel Application object in the main sub 

    Set objExcel = New Excel.Application '<--| get a new Excel application instance before starting the loop 

    For iMail = 1 To nMails 

     ... 
     ... code to get ith mail 
     ... 

     CoupaQueries MItem, objExcel '<--| pass your routine the current mail item and the already gotten Excel application 

    Next 

    objExcel.Quit '<--| quit Excel once the loop has finished 
    Set objExcel = Nothing '<--| release application variable 
End Sub 


Public Sub CoupaQueries(MItem As Outlook.MailItem, objExcel As Excel.Application) 
    Dim PersonName As String, PersonAddress As String, PersonSubject As String, PersonDate As Date 

    PersonName = MItem.SenderName 
    PersonAddress = MItem.SendUsingAccount 
    PersonSubject = MItem.Subject 
    PersonDate = MItem.ReceivedTime 

    With objExcel.Workbooks.Open("C:\Users\a222012\Desktop\CoupaQueries.xlsx").Sheets("Sheet1") '<--| get and reference an instance of "Sheet1" sheet of wanted workbook 
     With .Cells(.Rows.Count, 1).End(xlUp) '<--| reference referenced sheet column A first empty cell after last not empty one 
      .Offset(1, 0).Value = "PersonName" 
      .Offset(1, 1).Value = PersonAddress 
      .Offset(1, 2).Value = PersonSubject 
      .Offset(1, 3).Value = PersonDate 
     End With 
     .Parent.Save '<--| save parent object of currently referenced object: being this latter a worksheet, its parent object is the workbook it belongs to 
    End With 
End Sub 
関連する問題