John Page
asked on
Auto Close a Microsoft Word document with a VB MACRO - difficulty having file open
I had this question after viewing Auto Close a Microsoft Word document with a VB MACRO.
It was very informative and was a macro for exactly what I wanted.
Unfortunately, when using that code and creating that macro then when I double click on the file then it would only open word and not the file.
Once word was open then if I were to double ckick on the file, or opened the file from within word, then it would open and work fine.
I do not know if there is any way around this or what is specifically causing this in the macro itself that it does not open the document unless word is already running.
If you have any advice I would be most grateful!
Thank you,
Here is the original Macro instructions:
It was very informative and was a macro for exactly what I wanted.
Unfortunately, when using that code and creating that macro then when I double click on the file then it would only open word and not the file.
Once word was open then if I were to double ckick on the file, or opened the file from within word, then it would open and work fine.
I do not know if there is any way around this or what is specifically causing this in the macro itself that it does not open the document unless word is already running.
If you have any advice I would be most grateful!
Thank you,
Here is the original Macro instructions:
- Open the document you want to do this to
- From Word, press Alt-F11 to open the VBA/Macros editor (VBE) window
- Press control-r in the VBE to open/focus the Project window
- Look for the Project for the file you want this to happen on (should look like "Project (YourDocumentName)"
- Expand that project if not already expanded
- Expand the "Microsoft Word Objects" folder if not already expanded
- Double click on the "ThisDocument" object in that folder.
- In the code pane that opens up from there, paste in the following:
Private Sub Document_Open()
StartCheckingIdle
End Sub
- Now, right-click on the ThisDocument object in the Project window
- Go to Insert, then Module
- In the module that appears, paste in the following:
Option Explicit
'Set the constant below for how long you want it to be idle before triggering
' Enter the time in hours:minutes:seconds form, like "00:30:00" for 30 minutes
Private Const TIMEOUTTIME As String = "00:30:00"
Private Declare Function GetQueueStatus Lib "user32" (ByVal fuFlags As Long) As Long
Private Const QS_KEY = &H1
Private Const QS_MOUSEMOVE = &H2
Private Const QS_MOUSEBUTTON = &H4
Private Const QS_MOUSE = (QS_MOUSEMOVE Or QS_MOUSEBUTTON)
Private Const QS_INPUT = (QS_MOUSE Or QS_KEY)
Private bCancel As Boolean
Private Sub WaitForIdle()
Dim t As Double
t = Now
Do While bCancel = False
If GetQueueStatus(QS_INPUT) Then
t = Now
DoEvents
End If
If Now - t >= TimeValue(TIMEOUTTIME) Then Exit Do
Loop
End Sub
Public Sub StartCheckingIdle()
Do Until bCancel
WaitForIdle
If bCancel = False Then
bCancel = True
ThisDocument.Close True
End If
Do Until GetQueueStatus(QS_INPUT) Or bCancel
DoEvents
Loop
Loop
End Sub
Public Sub StopCheckingIdle()
bCancel = True
End Sub
Many thanks for any ideas!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.