2016-08-11 8 views
1

私はこのコードを使用して、選択したディレクトリのファイル名を取得します。私は、ファイルを選択したらファイルのディレクトリを取得して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 

、私は.FullNameを使用してみましたが、この地域では適用されないいる列O.にそれを置くために持っているか、多分私はちょうどそれを悪用してきました。その後、これはOutlookの電子メールで添付ファイルとして送信されます。

ところで、私はそのコードの一部を持っていますhere

助けが必要ですか?

+0

[OK]をクリックして添付ファイルを含め、私はちょうど 'GetFileName'機能を使用する必要はありませんし、そのすべての設定:)私の問題は今、それらを接続する方法であります見通しと添付ファイルとして機能します。ありがとう! – ramj

答えて

1

あなたが選択したファイルへの完全なパスを取得しようとしていると仮定します。 Application.GetOpenFilenameは既にそれを返しているため、GetFileName関数を使ってファイルを再処理する必要はありませんか?

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

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

を変更すると、私が正しくあなたの質問を理解していると仮定すると動作するはずです。お役に立てれば! Outlookで

1

が、私はそれを得た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 
関連する問題