Tuesday, April 12, 2011

Print Multipe documents in Word in silent mode

printing multiple word documents (e.g. 2000 word document) within a folder takes a long time as MS-Word try to open each document, print it. One my clients have this issue which took about 7 hours to print 2000 word documents.

After a research (thanks to google for bringing the right result in searches most of the time) I find this Forum that someone wrote the script (to be used in MS-Word or Excel) to solve the problem


Thanks to user "Rolling_Again" that wrote the code and share it.  I put the code below as the original didn't work for me. I got error in Application.Filesearch so I find a way to do the same functionality without it. Below is the code,

To run it All you need to do is :
  • open MS-Word or Excel
  • Open "Macro"  section  (under "View" in office 2007)
  • Copy the code on vb script screen that opened  and save it
  • Run "Print All" module, It will open folder selector to let you chose which folder and then it will print all documents with extension "*.doc" within that folder
'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_STATUSTEXT = 4
Private Const WM_USER = &H400
Private Const BFFM_INITIALIZED = 1
Private Const BFFM_SELCHANGED = 2
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.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.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
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:")
ChDir txtDirectory
sFile = Dir("*.doc")
Do While sFile <> ""
Application.PrintOut , , , , , , , , , , , , CStr(txtDirectory & "\" & sFile)
sFile = Dir
End Sub

Sub AccessDeliveryPopUpMenu()
' AccessDeliveryPopUpMenu Macro
End Sub