【问题标题】:Runtime Error - Cannot find this file; verify name & file path correct (Excel / VBA)运行时错误 - 找不到此文件;验证名称和文件路径是否正确(Excel / VBA)
【发布时间】:2019-01-09 08:42:08
【问题描述】:

尝试将附件链接到电子邮件时,标题中出现错误消息。附件存储在与公司“类型”相对应的文件夹名称中,这就是为什么我尝试添加一个 for 循环来从电子表格中检索“类型”。

Sub mailTest()

Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem
Dim olAttachmentLetter As Outlook.Attachments    
Dim fileLocationLetter As String
Dim dType As String

For i = 2 To 3

    Set olApp = New Outlook.Application
    Set olMail = olApp.CreateItem(olMailItem)
    Set olAttachmentLetter = olMail.Attachments
    fileLocationLetter = "C:\...\user\Desktop\FileLocation"
    letterName = "TestLetter1"
    dType = Worksheets("Test1").Cells(i, 2).Value

    mailBody = "Hello " _
                & Worksheets("Test1").Cells(i, 4) _
                & "," _
                & Worksheets("BODY").Cells(2, 1).Value _
                & Worksheets("BODY").Cells(3, 1).Value _
                & Worksheets("BODY").Cells(4, 1).Value & " " & dType _
                & Worksheets("BODY").Cells(5, 1).Value & " TTT" & dType & "xx18" _
                & Worksheets("BODY").Cells(6, 1).Value _
                & Worksheets("BODY").Cells(7, 1).Value

     With olMail
        .To = Worksheets("Test1").Cells(i, 5).Value
        .Subject = Worksheets("Test1").Cells(i, 3).Value & " - "
        .HTMLBody = "<!DOCTYPE html><html><head><style>"
        .HTMLBody = .HTMLBody & "body{font-family: Calibri, ""Times New Roman"", sans-serif; font-size: 13px}"
        .HTMLBody = .HTMLBody & "</style></head><body>"
        .HTMLBody = .HTMLBody & mailBody & "</body></html>"

        ''Adding attachment
        .Attachments.Add fileLocationLetter & letterName & ".pdf"
        .Display
        '' .Send (Once ready to send)
    End With
    Set olMail = Nothing
    Set olApp = Nothing
Next
End Sub

我在这里做错了什么?该文件存储在 'C:...\user\Desktop\FileLocation\TestLetter1.pdf'

谢谢。

【问题讨论】:

  • 你已经验证了路径,你已经验证了名称,但是你还没有验证路径+文件名的组合。必须用“\”字符分隔:)
  • 您正在运行什么操作系统来识别文件规范中的三点?这让我摸不着头脑。 blogs.msdn.microsoft.com/oldnewthing/20160202-00/?p=92953
  • @BillHileman 这(很可能)只是实际路径的占位符,由于某些隐私问题/等而被编辑。
  • 我在 WIndows 10 命令提示符下尝试过,它没有返回错误并且什么都不做,即从 \Windows\User CD ...什么也不做。但是 CD .. 会按预期执行并移至父级。没有祖父母捷径。
  • 它看起来不像那样,就像它只是做“C:\fake\path\to\your\file\directory\”的简写方式,IMO。跨度>

标签: vba excel email-attachments


【解决方案1】:

在@Vityata 的大力帮助下,想通了。

基本上可以制作两个附件,一个是已知文件名的静态附件,第二个附件的名称取决于存储的单元格值。解决方法是将文件的路径/名称分解为存储的字符串。也许有更简单的方法,但这对我有用!

使用的代码:

Sub mailTest()

Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem

'' Identify Attachments
Dim olAttachmentLetter As Outlook.Attachments
Dim olAttachmentSSH As Outlook.Attachments

'' Identify Attachment Locations / Paths
Dim fileLocationLetter As String
Dim fileLocationSSH As String
Dim fileLocationSSHi As String
Dim fileLocationSSHii As String

 '' Type Variable, referencing cell in worksheet where "Type" is stored (in loop below)
 Dim dType As String

 '' Creating the loop - Replace 4 with end of rows. Will eventually create code to automatically identify the last cell with stored value
For i = 2 To 4

     Set olApp = New Outlook.Application
     Set olMail = olApp.CreateItem(olMailItem)


     Set olAttachmentLetter = olMail.Attachments
     Set olAttachmentSSH = olMail.Attachments


     ''File Location for Letter
     fileLocationLetter = "C:\...\Directory"

     ''File Location for Excel sheet - Need 3 fields as file name is dynamic based on loop value
     fileLocationSSH = "C:\...\Directory\Excel Files"
     fileLocationSSHi = "Beginning of File name..."
     fileLocationSSHii = " ... End of File name"


     letterName = "Name of PDF attachment"


     dType = Worksheets("Test1").Cells(i, 2).Value

     ''Body of Email - Each new line represents new value (linking to hidden worksheet in Excel doc)
     mailBody = "Hello " _
                 & Worksheets("Test1").Cells(i, 4) _
                 & "," _
                 & Worksheets("BODY").Cells(2, 1).Value _
                 & Worksheets("BODY").Cells(3, 1).Value _
                 & Worksheets("BODY").Cells(4, 1).Value & " " & dType _
                 & Worksheets("BODY").Cells(5, 1).Value _
                 & Worksheets("BODY").Cells(6, 1).Value _
                 & Worksheets("BODY").Cells(7, 1).Value


     With olMail
         .To = Worksheets("Test1").Cells(i, 5).Value
         .Subject = Worksheets("Test1").Cells(i, 3).Value 
         .HTMLBody = "<!DOCTYPE html><html><head><style>"
         .HTMLBody = .HTMLBody & "body{font-family: Calibri, ""Times New Roman"", sans-serif; font-size: 13px}"
         .HTMLBody = .HTMLBody & "</style></head><body>"
         .HTMLBody = .HTMLBody & mailBody & "</body></html>"

      '' Adding attachments, referencing file locations and amending file name if needed
         .Attachments.Add fileLocationLetter & "\" & letterName & ".pdf"
         .Attachments.Add fileLocationSSH & "\" & dType & "\" & fileLocationSSHi & dType & fileLocationSSHii & ".xlsx"

            .Display
         '' .Send (Once ready to send)

    End With


    Set olMail = Nothing
    Set olApp = Nothing

Next



End Sub

【讨论】:

    【解决方案2】:

    您在fileLocationletterName 之间缺少\。因此,要么这样写:

    .Attachments.Add fileLocationLetter & "\" & letterName & ".pdf"
    

    或者这个:

    fileLocationLetter = "C:\...\user\Desktop\FileLocation\"
    

    【讨论】:

    • 啊,我太傻了,非常感谢。我还有一个问题——在我知道确切文件名的情况下,您的修改非常有效。对于我要添加的另一个附件,文件名中包含“类型”。所以它就像“Excel 文件类型 A -(请在此处插入您的姓名).xslx”或“Excel 文件类型 B -(请在此处插入您的姓名).xslx”。我正在尝试这样添加路径: .Attachments.Add fileLocationSSH & "\" & dType & "Excel file type " & dType & "- (请在此处插入您的姓名)" & ".xlsx" 它给了我同样的错误。我想要做的甚至可能吗?再次感谢!
    • @Tester_Y - 要查看问题,只需写 MsgBox fileLocationSSH &amp; "\" &amp; dType &amp; "Excel file type " &amp; dType &amp; "- (Please insert your name here)" &amp; ".xlsx"。然后检查路径。甚至是Debug.Print 而不是MsgBox,这样您就可以复制和粘贴。 stackoverflow.com/questions/2916287/…
    • 使用它让我意识到代码没有在实际文件名中使用 & dType&。它显示为“Excel 文件类型 -(请在此处插入您的姓名).xslx”。有没有办法根据不同的文件名修改路径?如果没有,那么我将不得不手动将所有文件名更改为相同...嗯。
    • @Tester_Y - 肯定有办法改变路径。只要确保写一个minimal reproducible example 并提出一个新问题,我想你会在不到 15 分钟内得到答案。现在是美国的中午,人们在 SO 中非常活跃 :)
    • 谢谢,在您的帮助下搞定了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2016-03-17
    • 2012-05-20
    相关资源
    最近更新 更多