views:

903

answers:

1

I wrote a visual basic macro for archiving attachments for Outlook 2007, but did not find a totally satisfactory way for showing a directory picker from the outlook macro. Now, I don't know much about either Windows APIs or VB(A) programming, but the "standard" windows file dialog I see most often in Microsoft applications would seem like an obvious choice but does not seem to be easily available from Outlook's macros.

Ideally, the directory picker should at least allow to manually paste a file path/URI as a starting point for navigation, since I sometimes already have an explorer window open for the same directory.

What are the best choices for directory pickers in Outlook macros?

Two things I already tried and did not find totally satisfactory are (the code is simplified and w/o error handling and probably also runs in older Outlook versions):

1) Using "shell.application" which does not allow me to actually paste a starting point via the clipboard or do other operations like renaming folders

  Set objShell = CreateObject("shell.application")
  sMsg = "Select a Folder"
  cBits = 1
  xRoot = 17
  Set objBFF = objShell.BrowseForFolder(0, sMsg, cBits, xRoot)
  path = objBFF.self.Path

2) Using the "Office.FileDialog" from "Microsoft Word 12.0 Object Library" (via tools/references) and then using Word's file dialog, which somehow takes forever on my Vista system to appear and does not always actually bring Word to the foreground. Instead, sometimes Outlook is blocked and the file dialog lingering somewhere in the background:

  Dim objWord As Word.Application
  Dim dlg As Office.FileDialog
  Set objWord = GetObject(, "Word.Application")
  If objWord Is Nothing Then
     Set objWord = CreateObject("Word.Application")
  End If
  objWord.Activate
  Set dlg = objWord.FileDialog(msoFileDialogFolderPicker)
  path = dlg.SelectedItems(1)

Any other ideas?

Thanks guys!

+2  A: 

Your best bet will probably be to use the Windows32 API for this. See the following MSDN article for sample VBA code on how to interact with the API.

http://msdn.microsoft.com/en-us/library/aa155724(office.10).aspx

The article outlines a few different techniques, but I'd suggest searching the article for "COMDLG32.dll" and following the steps outlined in that section.

Tim Lentine