PDA

View Full Version : [SOLVED:] Abort opening a document in VBA



JimmyTheHand
04-05-2017, 08:35 AM
Hi,
I've not posted here for several years, but I still remember the best experts are here on this forum. So here's my problem.

I'm working on a program that processes all docx documents in a folder. I'm opening the documents one by one.

Set WA = CreateObject("Word.Application")
WA.Visible = True
Set WD = WA.Documents.Open(FileName:=DocPath, ConfirmConversions:=False, ReadOnly:=True, NoEncodingDialog:=True)
However, some of the documents are password protected. And here my program stops working, waiting for user interaction. I would like to automatically abort the document opening, if, for example, it's not successful for 10 seconds (e.g. proper password was not supplied). I was trying to set up a form timer, with interval of 10000, that would quit then recreate the word application:

Private Sub Form_Timer()
WA.Quit
Set WA = CreateObject("Word.Application")
WA.Visible = True
End Sub

But it never gets executed while the Word Application waits for the password.
I thought maybe I could set the Word app/window to be non-modal, but I found nothing that made this possible.
Any ideas how to crack this nut?

Jimmy

JimmyTheHand
04-05-2017, 11:34 PM
I've found another approach.
http://wordmvp.com/FAQs/MacrosVBA/CheckIfPWProtectB4Open.htm
Problem solved :)

HiTechCoach
04-06-2017, 05:28 PM
Thanks for paying it forward by sharing your solution. :bow:

macropod
04-06-2017, 10:01 PM
For code that loops through a folder and its subfolders, plus generates a report of any files it couldn't process, see: http://www.eileenslounge.com/viewtopic.php?f=26&t=14499#p107602 and the modifications in http://www.eileenslounge.com/viewtopic.php?f=26&t=14499#p110877