PDA

View Full Version : [HELP] Outlook Form File Search Dialog



Gawl
04-26-2005, 03:44 AM
I need to click in a button and open the file search dialog to add attachment's in a form anyone can help me???:banghead: i make the attachment code but to attach the file i need to write the path manually i want this dialog to automate the path!!! Help me plz

Killian
04-27-2005, 06:17 AM
Hi Gawl and welcome to VBAX :hi:

First the bad news... Outlook doesn't support Application.FileDialog the same way that Word Excel and PowerPoint do even though it's part of the Office common control library. For some reason it can't be accessed from within the OL object model which seems very strange, especially when you click the Attach Files button and it appears, but that's how it is and we just have to deal with it.

The good news is that there are at least a couple of ways to get round it...
First option: build your own userform file picker
Second option: Use the file dialog available in another app (lik Word)
To do this you'll need to add a reference to the MS Word Object Model in the VBEditor|Tools|References, then add and adapt the code belowSub GetPathsWithWordDialog()

Dim wdApp As Word.Application
Dim fd As FileDialog
Dim olWin As Object
Dim s
Dim Kill_Word As Boolean

Set olWin = Application.ActiveWindow

On Error Resume Next
Kill_Word = False
Set wdApp = GetObject(, "Word.Application") 'if Word already open
If wdApp Is Nothing Then
Set wdApp = CreateObject("Word.Application")
Kill_Word = True
End If

'switch to Word
wdApp.Visible = True
wdApp.Activate

Set fd = wdApp.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = True
.Filters.Add "All Files", "*.*", 1
.Show
olWin.Activate
For Each s In .SelectedItems
's is a string variant that provides the full
'file path for attaching the selected files
'so do attachments here
Next
End With

Set fd = Nothing
If Kill_Word = True Then wdApp.Quit
Set wdApp = Nothing

End SubThird option: use a function call to the WIN API to use the Windows common dialog.'Declare the required function at the top of the module
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
'declare the type required for the function paramater
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

'procedure to display the dialog
Sub GetPathWithSystemDialog()

Dim fName As OPENFILENAME
fName.lStructSize = Len(fName)
'Select a filter
fName.lpstrFilter = "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
'create a buffer for the file
fName.lpstrFile = Space$(254)
'set the maximum length of a returned file
fName.nMaxFile = 255
'Create a buffer for the file title
fName.lpstrFileTitle = Space$(254)
'Set the maximum length of a returned file title
fName.nMaxFileTitle = 255
'Set the initial directory
'fName.lpstrInitialDir = "C:\"
'Set the title
fName.lpstrTitle = "Attach File..."
'No flags
fName.flags = 0

'Show the 'Open File'-dialog
If GetOpenFileName(fName) Then
MsgBox "File to Attach: " + Trim$(fName.lpstrFile)
Else
MsgBox "Cancel was pressed"
End If

End Sub That should get you started :thumb

MOS MASTER
04-27-2005, 01:36 PM
Hi Gawl and welcome to VBAX :hi:

First the bad news... Outlook doesn't support Application.FileDialog the same way that Word Excel and PowerPoint do even though it's part of the Office common control library. For some reason it can't be accessed from within the OL object model which seems very strange, especially when you click the Attach Files button and it appears, but that's how it is and we just have to deal with it.
@ Gawl,

Welcome to VBAX!

Hi K, :D

Indead that's mighty strange and it frustrated the hell out off me in the past! :banghead:

Why...the thing is that there is even a sample code in the HelpFile and if you type FileDialog in the VBE en press F1 you can see that the help file writers even thought it was going to be in here! The whole FileDialog Object is in the damn puppy! :dunno

This must be one off those things that got mixed up with al those separate code teams that Microsoft uses per Application. (A lot of difference between Office apps are caused by this lack of communication)

In that time I found a simular sollution like yours from Outlook MVP Sue Mosher...and ussualy if she says the object isn't there...it ussualy is:
http://www.outlookcode.com/codedetail.aspx?id=294

Guess we will never trully know why...:whistle:

Gawl
04-28-2005, 02:15 AM
tkx for the help!!! OL2000 don't supports the word file dialog :S:S:S

MOS MASTER
04-28-2005, 10:20 AM
tkx for the help!!! OL2000 don't supports the word file dialog :S:S:S
Hi, :D

No problem..

The API Class sub from Killian will work in 2000 so that fixes you're problem.

The FileDialog object was introduced in 2002 so you're right it won't work in 2000, but you can still use the Dialogs object in Word (Look it up in Word VBA Help)

Enjoy!