2015-06-14 80 views
0

我的代碼插入來自VBA行工作得很好,直到我換辦公室2010至2013年如何使用Outlook規則

此代碼出口附件並運行在插入對導出文件的SQL Server信息的腳本。
文件不導出,沒有Sql條目。沒有生命。

'Addin for Outlook rules to export attachments to disk 
'RAW Component of Outlook Attachment Strip 
'Compiled by Muhammad Abubakar Riaz/COM/LHR to make life a lit bit easier 

Sub RC_OutlookAttachmentStrip(anItem As Outlook.MailItem) 

    Dim anAttachedObject As Outlook.Attachment 

    'Location of folder you want to save attachments to 

    Dim pathLocation As String 
    pathLocation = "G:\FOI Files\Junk" 

    'Date & time to add with attached file name 

    nowDate = Format(Now, "dd-mm-yyyy") 
    nowTime = Format(Now, "hh-mm AM/PM") 

    'Random counter to minimize overwriting same file names in same folder 

    randomCounter = CInt(Int((9999 * Rnd()) + 1)) 
    fileCounter = 0 

    'Email received at 

    receiveTime = anItem.ReceivedTime 
    fromID = anItem.SenderName 

    'SQL connection code 

    Const adOpenStatic = 3 
    Const adLockOptimistic = 3 

    Set objConnection = CreateObject("ADODB.Connection") 
    Set objRecordSet = CreateObject("ADODB.Recordset") 

    objConnection.Open _ 
    "Provider = SQLOLEDB; " & _ 
     "Data Source=C-LHE-CS-68541\CMSA;" & _ 
     "Trusted_Connection=Yes;" & _ 
     "InitialCatalog=CMSA_Console;" & _ 
     "User ID=sa;Password=xxxxxxxxx;" 

    '------------------------- 
    'ended SQL Connection code 

    'Save all files one by one 

    For Each anAttachedObject In anItem.Attachments 
     fileCounter = fileCounter + 1 

     'fixed files types to be exported, like in this case it text files 
     If Right(anItem.Attachments.Item(fileCounter).FileName, 4) = ".txt" Then 

      'Save all files one by one 
      'file format is 1-9999 Date Time OriginalFileName.extension 
      anAttachedObject.SaveAsFile pathLocation & "\" & fileCounter & "-" & randomCounter & " " & nowDate & " " & nowTime & " " & anItem.Attachments.Item(fileCounter).FileName 

      'create query String 
      Myfilename = anItem.Attachments.Item(fileCounter).FileName 
      sqlQuery = "Insert into [CMSATemp].[dbo].[temptest] Values('" & fromID & "','" & receiveTime & "','" & Myfilename & "')" 

      'Insert records into DB 
      objRecordSet.Open sqlQuery, _ 
      objConnection, adOpenStatic, adLockOptimistic 

     End If  
     Set anAttachedObject = Nothing 
    Next 

    objConnection.Close 

End Sub 
+0

什麼叫這個代碼?它是否仍然被調用,你可以通過它來查看它出錯的地方嗎? – ChipsLetten

+0

thnx很多ChipsLetten,忘記調試很有用,發現邏輯錯誤文件extionsion是.csv不是.txt:D,現在工作正常。 –

+0

請將其作爲答案,以便您的帖子不會顯示爲未答覆。 – ChipsLetten

回答

0

我忘了調試是有用的。我發現了一個邏輯錯誤 - 文件擴展名爲.csv而不是.txt - 現在工作正常。

'fixed files types to be exported, like in this case it text files 
    If Right(anItem.Attachments.Item(fileCounter).FileName, 4) = ".csv" Then