I found this code which almost works. I want to be able to print all documents in a given folder. The code allows the user to browse a folder but the Application.Filesearch part is not supported on windows 7
I will post the code and look forward to any suggestions. Thanks in advance
I will post the code and look forward to any suggestions. Thanks in advance
Code:
'API Declares
Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
'API Constants
Private Const MAX_PATH = 260
Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_STATUSTEXT = 4
Private Const WM_USER = &H400
Private Const BFFM_INITIALIZED = 1
Private Const BFFM_SELCHANGED = 2
Private Const BFFM_SETSTATUSTEXTA = (WM_USER + 100)
Private Const BFFM_SETSELECTIONA = (WM_USER + 102)
'BrowseInfo Type
Private Type BrowseInfo
hwndOwner As Long
pIDLRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
'Private Variables
Private m_sDefaultFolder As String
Public Sub BrowseFolder()
frmBrowse.Show
frmBrowse.txtDirectory.Text = ""
End Sub
Public Function BrowseForFolder(DefaultFolder As String, Optional Parent As Long = 0, Optional Caption As String = "") As String
Dim bi As BrowseInfo
Dim sResult As String, nResult As Long
bi.hwndOwner = Parent
bi.pIDLRoot = 0
bi.pszDisplayName = String$(MAX_PATH, Chr$(0))
If Len(Caption) > 0 Then
bi.lpszTitle = Caption
End If
bi.ulFlags = BIF_RETURNONLYFSDIRS
bi.lpfn = GetAddress(AddressOf BrowseCallbackProc)
bi.lParam = 0
bi.iImage = 0
m_sDefaultFolder = DefaultFolder
nResult = SHBrowseForFolder(bi)
If nResult <> 0 Then
sResult = String(MAX_PATH, 0)
If SHGetPathFromIDList(nResult, sResult) Then
BrowseForFolder = Left$(sResult, InStr(sResult, Chr$(0)) - 1)
End If
CoTaskMemFree nResult
End If
End Function
Private Function BrowseCallbackProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal lParam As Long, ByVal lpData As Long) As Long
Select Case uMsg
Case BFFM_INITIALIZED
If Len(m_sDefaultFolder) > 0 Then
SendMessage hwnd, BFFM_SETSELECTIONA, True, ByVal m_sDefaultFolder
End If
End Select
End Function
Private Function GetAddress(nAddress As Long) As Long
GetAddress = nAddress
End Function
Public Sub PrintAll()
Dim txtDirectory As String
txtDirectory = BrowseForFolder(txtDirectory, , "&Select a directory:")
With Application.FileSearch
.NewSearch
.LookIn = txtDirectory
.SearchSubFolders = False
.FileType = msoFileTypeWordDocuments
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
Application.PrintOut , , , , , , , , , , , , CStr(.FoundFiles(i))
Next i
End If
End With
End Sub