Hello,
(VB newbie alert! )
I'm trying to write an application that will allow me to print many Word documents to postscript. It's going okay except for my routine for handling password protected files. I've got an error routine set up to basically ignore the files (and move them).
My code works until the second time a password protected document is encountered. When the second password protected file is encountered, I am getting the prompt for the PrintToFile filename. Any thoughts?
Thanks, Doug
P.S. A similar thing is happening to me while trying to automate Excel in a similar way.
The code I've cobbled together from other places...
Sub mainloop()
Dim sFileName As String
Dim FileOpenError As Boolean
Dim objWordApp As Word.Application
Dim iSearchHandle As Long ' File search handle
Dim pFindFileBuff As WIN32_FIND_DATA ' File search buffer
'keep searching for *.doc files in D:\2Word\, when found convert to *.PS
Do
' Find first file and create search handle
iSearchHandle = FindFirstFile("D:\2Word\*.dat", pFindFileBuff)
' Check if FindFirstFile call was successful
If iSearchHandle <> INVALID_HANDLE_VALUE Then
sFileName = TrimNull(pFindFileBuff.cFileName)
' Close file search handle
Call FindClose(iSearchHandle)
' Create Word Application Instance so a Word document can be opened
If IsWordRunning() Then
Set objWordApp = GetObject(, "Word.Application")
Else
Set objWordApp = CreateObject("Word.Application")
End If
'if *.doc file exists (then print it as *.PS.)
' Open Document referenced by sFileName
' objWordApp.Visible = True
'check for password protection
On Error GoTo Error ' for file opening errors
FileOpenError = True 'will be made False if we can open the file
Set objWord = Documents.Open(FileName:="D:\2Word\" & sFileName & ".doc", PasswordDocument:="")
FileOpenError = False
Application.ActivePrinter = "PSFile"
Documents(sFileName & ".doc").PrintOut PrintToFile:=True, OutputFileName:="D:\3PS\" & sFileName & ".ps"
objWord.Close
' Move the *.dat file to D:\3PS FileCopy "d:\2Word\" & sFileName & ".dat", "d:\3PS\" & sFileName & ".dat"
' Move a copy of the *.dat file to D:\3WordOK FileCopy "d:\2Word\" & sFileName & ".dat", "d:\3WordOK\" & sFileName & ".dat"
' Delete the copy of *.dat in D:\2Word Kill "d:\2Word\" & sFileName & ".dat"
' Move a copy of the *.doc file to D:\3WordOK FileCopy "d:\2Word\" & sFileName & ".doc", "d:\3WordOK\" & sFileName & ".doc"
' Delete the copy of *.doc in D:\2Word Kill "d:\2Word\" & sFileName & ".doc"
' good housekeeping
sFileName = ""
Set objWordApp = Nothing
Else
' wait for 5 seconds a do it again
Wait 1
End If
Error:
If FileOpenError = True Then
'couldn't open file
' Move the *.dat file and *.doc file to D:\OpenErr FileCopy "d:\2Word\" & sFileName & ".doc", "d:\OpenErr\" & sFileName & ".doc"
FileCopy "d:\2Word\" & sFileName & ".dat", "d:\OpenErr\" & sFileName & ".dat"
Kill "d:\2Word\" & sFileName & ".doc"
Kill "d:\2Word\" & sFileName & ".dat"
' good housekeeping
sFileName = ""
Set objWordApp = Nothing
End If
Loop
End Sub
(VB newbie alert! )
I'm trying to write an application that will allow me to print many Word documents to postscript. It's going okay except for my routine for handling password protected files. I've got an error routine set up to basically ignore the files (and move them).
My code works until the second time a password protected document is encountered. When the second password protected file is encountered, I am getting the prompt for the PrintToFile filename. Any thoughts?
Thanks, Doug
P.S. A similar thing is happening to me while trying to automate Excel in a similar way.
The code I've cobbled together from other places...
Sub mainloop()
Dim sFileName As String
Dim FileOpenError As Boolean
Dim objWordApp As Word.Application
Dim iSearchHandle As Long ' File search handle
Dim pFindFileBuff As WIN32_FIND_DATA ' File search buffer
'keep searching for *.doc files in D:\2Word\, when found convert to *.PS
Do
' Find first file and create search handle
iSearchHandle = FindFirstFile("D:\2Word\*.dat", pFindFileBuff)
' Check if FindFirstFile call was successful
If iSearchHandle <> INVALID_HANDLE_VALUE Then
sFileName = TrimNull(pFindFileBuff.cFileName)
' Close file search handle
Call FindClose(iSearchHandle)
' Create Word Application Instance so a Word document can be opened
If IsWordRunning() Then
Set objWordApp = GetObject(, "Word.Application")
Else
Set objWordApp = CreateObject("Word.Application")
End If
'if *.doc file exists (then print it as *.PS.)
' Open Document referenced by sFileName
' objWordApp.Visible = True
'check for password protection
On Error GoTo Error ' for file opening errors
FileOpenError = True 'will be made False if we can open the file
Set objWord = Documents.Open(FileName:="D:\2Word\" & sFileName & ".doc", PasswordDocument:="")
FileOpenError = False
Application.ActivePrinter = "PSFile"
Documents(sFileName & ".doc").PrintOut PrintToFile:=True, OutputFileName:="D:\3PS\" & sFileName & ".ps"
objWord.Close
' Move the *.dat file to D:\3PS FileCopy "d:\2Word\" & sFileName & ".dat", "d:\3PS\" & sFileName & ".dat"
' Move a copy of the *.dat file to D:\3WordOK FileCopy "d:\2Word\" & sFileName & ".dat", "d:\3WordOK\" & sFileName & ".dat"
' Delete the copy of *.dat in D:\2Word Kill "d:\2Word\" & sFileName & ".dat"
' Move a copy of the *.doc file to D:\3WordOK FileCopy "d:\2Word\" & sFileName & ".doc", "d:\3WordOK\" & sFileName & ".doc"
' Delete the copy of *.doc in D:\2Word Kill "d:\2Word\" & sFileName & ".doc"
' good housekeeping
sFileName = ""
Set objWordApp = Nothing
Else
' wait for 5 seconds a do it again
Wait 1
End If
Error:
If FileOpenError = True Then
'couldn't open file
' Move the *.dat file and *.doc file to D:\OpenErr FileCopy "d:\2Word\" & sFileName & ".doc", "d:\OpenErr\" & sFileName & ".doc"
FileCopy "d:\2Word\" & sFileName & ".dat", "d:\OpenErr\" & sFileName & ".dat"
Kill "d:\2Word\" & sFileName & ".doc"
Kill "d:\2Word\" & sFileName & ".dat"
' good housekeeping
sFileName = ""
Set objWordApp = Nothing
End If
Loop
End Sub