获取文件的目录并在outlook中作为附件发送

我有这个代码获取所选目录的文件名。

Sub browsefile() Dim file As Variant Dim i As Integer Dim lRow As Long Set main = ThisWorkbook.Sheets("Main") file = Application.GetOpenFilename("All Files, *.*", , "Select File", , True) For i = 1 To UBound(file) lRow = Cells(Rows.Count, 15).End(xlUp).Row lRow = lRow + 1 ThisWorkbook.Sheets("Main").Range("O" & lRow).Value = GetFileName(CStr(file(i))) Next i End Sub Function GetFileName(filespec As String) Dim fso As Object Set fso = CreateObject("Scripting.FileSystemObject") GetFileName = fso.GetFileName(filespec) End Function 

一旦我select了这些文件,我必须把它放在列O中。我尝试过使用.FullName但不适用于这个领域,或者我刚刚滥用了它。 然后,这将作为附件在Outlook中的电子邮件发送。

顺便说一句,我在这里有一些代码。

任何帮助?

在Outlook中包含Attachments.Add的附件

 Private Sub browsefile_Att() ' Multiselect = False so file is not an array ' Dim file As Variant Dim file As String Dim lRow As Long Dim main As Worksheet Dim olOlk As Object Dim olNewmail As Object Set main = ThisWorkbook.Sheets("Main") ' Multiselect = False so file is not an array file = Application.GetOpenFilename("All Files, *.*", , "Select File", , False) lRow = Cells(Rows.Count, 15).End(xlUp).Row lRow = lRow + 1 ThisWorkbook.Sheets("Main").Range("O" & lRow).Value = file Set olOlk = CreateObject("Outlook.Application") Set olNewmail = olOlk.CreateItem(olMailItem) olNewmail.Attachments.Add file olNewmail.Display ExitRoutine: Set olNewmail = Nothing Set olOlk = Nothing End Sub 

我假设你正在尝试获取你select的文件的完整path。 Application.GetOpenFilename已经返回你,因此,没有必要重新处理你的文件与GetFileName函数?

更改

 ThisWorkbook.Sheets("Main").Range("O" & lRow).Value = GetFileName(CStr(file(i))) 

 ThisWorkbook.Sheets("Main").Range("O" & lRow).Value = CStr(file(i)) 

应该工作假设我已经正确地理解你的问题。 希望这可以帮助!