2015-06-14 97 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