Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA (Word): Command to close document and cancel all future code?

Status
Not open for further replies.

be17

Technical User
Feb 7, 2001
26
US
I am working in VBA (Word 97) and I have created a template with multiple forms which prompt the user for information. I would like to create a "Close Document" button (to be used if the user gets half way through the prompts and decides that he/she does not want to finish, but instead would like to close out of the document) that will close the document, and also prevent all prompts programmed to appear after that point from showing up. Right now I have code like this:

Private Sub cmdClose_Click()
Unload Me
ActiveDocument.Close
End Sub

But with the above code, the document closes, but the program continues to run and thus the user is prompted through all of the following forms.

Is there a way to close the document and quit the program at that point?
 
Do this:

Private Sub MyCode()
dim bFlag As Boolean

'your code
'goes here
'set bFlag somewhere in this code
if (bFlag = true) then
exit sub
end if
'have code here that you want to run if
'sub is not exited
end sub


Hope this helps, good luck!

Regards,
Michael Bronner
 
That helps a little bit. However, I am very new to programming and I do not know what you mean by "set bFlag somewhere in this code". How do I set it? Also, I have one module that has a Sub that has various forms loading in a particular order. From that point, there is different code within each form, so when the code says "exit sub" will it just exit that particular Sub on that form, or how can I get it to quit the program on the main module that is telling it to open up all of the forms?
 
If I'm not mistaken you can just use "End" by itself to terminate all code execution. Your code for the Close button would look like this:

Private Sub cmdClose_Click()
Unload Me
ActiveDocument.Close
End
End Sub
 
When using "End", it will close your entire program.
Using "Exit Sub" will only terminate your current procedure that is being executed.

bFlag is a boolean variable, meaning that it can be either "true" or "false".
so whenever you would encounter the condition that meets your requirements to terminate the sub procedure, you would do something like this:

if ([determine to end sub procedure]) Then
bFlag = true
else
bFlag = false
end if

The else statement is optional, depending on your use of bFlag.
 
Thanks much. I did not know about End, but that is exactly what I wanted to do - close the entire program.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top