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

时间:2016-08-11 03:01:14

标签: excel vba excel-vba 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

一旦我选择了这些文件,我就必须把它放在O列中。我尝试使用.FullName但是不适用于这个区域,或者我只是误用了它。然后,这将作为附件发送到outlook中的电子邮件中。

顺便说一下,我有一些代码here

任何帮助?

2 个答案:

答案 0 :(得分:1)

我假设您正在尝试获取所选文件的完整路径。 Application.GetOpenFilename已经返回给您,因此,不需要使用GetFileName函数重新处理您的文件?

更改

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

ThisWorkbook.Sheets("Main").Range("O" & lRow).Value = CStr(file(i))
假设我已正确理解你的问题,

应该有效。希望这有帮助!

答案 1 :(得分:1)

在Outlook中包含附件与附件。添加

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