简体   繁体   English

VBA - Outlook - 保存附件

[英]VBA - Outlook - Saving attachments

I am receiving e-mails from two vendors.我收到了两家供应商的电子邮件。 From both vendors - the e-mails have two types of attachments attached - with extension of xml and pdf. XML can contain data of three types, which is reflected in the name of the XML file The types of XML let say can be: "IE529", "IE599", "ZC299".来自两个供应商 - 电子邮件附有两种类型的附件 - 扩展名为 xml 和 pdf。XML 可以包含三种类型的数据,这反映在 XML 文件的名称中 XML 的类型可以是:“ IE529”、“IE599”、“ZC299”。

XMLs from Vendor "A" are named like this: (...)ZC299(...).xml来自供应商“A”的 XML 命名如下:(...)ZC299(...).xml

XMLs from Vendor "B" are named like this: ZC299 (...).xml --> there is space here.来自供应商“B”的 XML 的命名方式如下:ZC299 (...).xml --> 这里有空格。

I want my script to save only XML files, depending on the type, to one of three different folders, however my script works only for Vendor B, and not for Vendor A.我希望我的脚本根据类型仅将 XML 个文件保存到三个不同文件夹之一,但是我的脚本仅适用于供应商 B,而不适用于供应商 A。

I assume my problem is, that my script search for separate name "ZC299", but doesn't recognize it when it is hidden in the middle of filename我假设我的问题是,我的脚本搜索单独的名称“ZC299”,但当它隐藏在文件名中间时无法识别

  Public Sub Komunikaty(MItem As Outlook.MailItem)

Dim Zalacznik As Outlook.Attachment
Dim KatalogIE529 As String
Dim KatalogIE599 As String
Dim KatalogZC299 As String

KatalogIE529 = "C:(...)"
KatalogIE599 = "C:(...)"
KatalogZC299 = "C:(...)"

For Each Zalacznik In MItem.Attachments

   If InStr(1, Zalacznik.DisplayName, "IE529", vbTextCompare) And InStr(1, Zalacznik.DisplayName, ".xml", vbTextCompare) Then
    Zalacznik.SaveAsFile KatalogIE529 & "\" & Zalacznik.DisplayName
    
    ElseIf InStr(1, Zalacznik.DisplayName, "IE599", vbTextCompare) And InStr(1, Zalacznik.DisplayName, ".xml", vbTextCompare) Then
    Zalacznik.SaveAsFile KatalogIE599 & "\" & Zalacznik.DisplayName

  ElseIf InStr(1, Zalacznik.DisplayName, "ZC299", vbTextCompare) And InStr(1, Zalacznik.DisplayName, ".xml", vbTextCompare) Then
    Zalacznik.SaveAsFile KatalogZC299 & "\" & Zalacznik.DisplayName

End If

Next

    End Sub

Would you be able to help me?你能帮我吗?

The code is valid.代码有效。 The InStr function returns a long specifying the position of the first occurrence of one string within another. InStr function 返回一个 long,指定一个字符串在另一个字符串中第一次出现的 position。

Try to set a breakpoint and see what results you are getting when the attachment display name contains a different string.尝试设置一个断点,看看当附件显示名称包含不同的字符串时会得到什么结果。 Just run the code line-by-line and you will find the cause.只需逐行运行代码,您就会找到原因。

Also you may consider using regular expressions to find a match, see How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops for more information how to use them in VBA.您也可以考虑使用正则表达式来查找匹配项,请参阅How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops有关如何在 VBA 中使用它们的更多信息。

I would look to do a number of tests on the DisplayName then use some arrays to access the correct save locations.我希望对 DisplayName 进行一些测试,然后使用一些 arrays 来访问正确的保存位置。 Your code seems to be somewhat incomplete as not all vendor save places seem to be present, so some extrapolation done.您的代码似乎有些不完整,因为并非所有供应商保存位置似乎都存在,因此进行了一些推断。

Please note this is untested, and some debugging with the Array() assignment probably needed请注意这是未经测试的,可能需要使用 Array() 分配进行一些调试

Public Sub Komunikaty(MItem As Outlook.MailItem)

    Dim Zalacznik As Outlook.Attachment, Vendor As String, i As Long

    Dim KatalogIE529 As String: KatalogIE529 = "C:(...)"
    Dim KatalogIE599 As String: KatalogIE599 = "C:(...)"
    Dim KatalogZC299 As String: KatalogZC299 = "C:(...)"
    Dim Name As String
    
    Dim TextArr() As String: TextArr = Split("IE529", "IE599", "ZC299")
    Dim SaveArrA() As String: SaveArrA = Array(KatalogIE529, KatalogIE599, KatalogZC299)
    Dim SaveArrB() As String: SaveArrB = Array(KatalogIE529, KatalogIE599, KatalogZC299)
    
    For Each Zalacznik In MItem.Attachments
        Name = UCase(Zalacznik.DisplayName)
        If Right(Name, 4) = ".XML" Then 
            For i = LBound(TextArr) To UBound(TextArr)
                Select Case InStr(Name, Arr(i))
                    Case Is = 1
                        Zalacznik.SaveAsFile SaveArrA(i) & "\" & Zalacznik.DisplayName
                    Case Is > 1
                        Zalacznik.SaveAsFile SaveArrB(i) & "\" & Zalacznik.DisplayName
                    Case Default
                End Select
            Next i
        End If
    Next

End Sub

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

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