简体   繁体   English

从 Outlook 2016 到 2013 的 VBA 代码更新

[英]VBA code update from Outlook 2016 to 2013

I wrote this code on an other PC which had Win10 and Office 2016. It is used in an outlook rule.我在另一台装有 Win10 和 Office 2016 的 PC 上编写了此代码。它用于 Outlook 规则。 It saves the xml files from the e-mail to a folder and change it to xlsx file in an other folder.它将电子邮件中的xml文件保存到一个文件夹中,并将其更改为另一个文件夹中的xlsx文件。 In Outlook 2016 it runs properly.在 Outlook 2016 中它运行正常。 I copied it to an other notebook.我把它复制到另一个笔记本上。

This notebook has Win10 and Office 2013 and this code run in Outlook 2013 without any error message but the xml files neither were saved into the given folder and nor were converted to xlsx .这个笔记本有 Win10 和 Office 2013,这段代码在 Outlook 2013 中运行,没有任何错误消息,但xml文件既没有保存到给定的文件夹中,也没有转换为xlsx

What could be wrong in this code?这段代码可能有什么问题?

Option Explicit

Public Sub saveconvAttachtoDisk(itm As Outlook.MailItem)

Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat As String
Dim convFormat As String
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object

saveFolder = "C:\Users\tulaj\Documents\xml\"
convFolder = "C:\Users\tulaj\Documents\xls\"
dateFormat = Format(itm.ReceivedTime, "yyyy-mm-dd HH-mm-ss")

For Each objAtt In itm.Attachments

objAtt.SaveAsFile saveFolder & dateFormat & objAtt.FileName

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(saveFolder)
    If UCase(Right(objAtt.FileName, Len(XML))) = UCase(XML) Then
        NewFileName = convFolder & dateFormat & objAtt.FileName & "_conv.xlsx"

Set ConvertThis = Workbooks.Open(saveFolder & dateFormat & objAtt.FileName)
        ConvertThis.SaveAs FileName:=NewFileName, FileFormat:= _
        xlOpenXMLWorkbook
        ConvertThis.Close
    End If
Next
Set objAtt = Nothing
End Sub

In Tools-References are selected the falowings:在 Tools-References 中选择 falowings:

  • Visual Basic For Aplications Visual Basic 应用程序
  • Microsoft Outlook 15.0 Object Library Microsoft Outlook 15.0 对象库
  • OLE Automation OLE自动化
  • Microsoft Office 15.0 Object Library Microsoft Office 15.0 对象库
  • Microsoft Excel 15.0 Object Library Microsoft Excel 15.0 对象库
  • Microsoft Scripting Runtime Microsoft 脚本运行时

This should work for you...这应该对你有用...

Option Explicit
Public Sub saveconvAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim SaveFolder As String
    Dim convFolder As String
    Dim DateFormat As String
    Dim ConvFormat As String
    Dim NewFileName As String
    Dim ConvertThis As Object
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object

    SaveFolder = "C:\Temp\xml\"
    convFolder = "C:\Temp\xls\"
    DateFormat = Format(itm.ReceivedTime, "yyyy-mm-dd HH-mm-ss ")

    For Each objAtt In itm.Attachments
        Debug.Print objAtt.FileName
        objAtt.SaveAsFile SaveFolder & DateFormat & objAtt.FileName

        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFolder = objFSO.GetFolder(SaveFolder)

        If UCase(Right$(objAtt.FileName, Len("XML"))) = UCase("XML") Then
            NewFileName = convFolder & DateFormat & objAtt.FileName & "_conv.xlsx"

            Set ConvertThis = Workbooks.Open(SaveFolder & DateFormat & objAtt.FileName)
            ConvertThis.SaveAs FileName:=NewFileName, FileFormat:= _
            xlOpenXMLWorkbook
            ConvertThis.Close
        End If
    Next
    Set objAtt = Nothing
End Sub

To Test it, select the Email and run the following code要测试它,请选择电子邮件并运行以下代码

Public Sub Test_Rule()
    Dim Item As MailItem

    Set Item = ActiveExplorer.Selection.Item(1)
    saveconvAttachtoDisk Item

    Set Item = Nothing
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM