stupid VBA question

563 Views | 7 Replies | Last: 5 yr ago by MidnightYell2003
MidnightYell2003
How long do you want to ignore this user?
AG
Do people still use VBA for Word documents?

Anyways, I inherited a Word doc that has a 10-year-old VBA code written into it. It basically has about 15 different forms that come and go through the course of asking questions that runs through a macro (VBA module). I have been tasked with making a simple CANCEL button.

If you want to stop running the macro and hit the red X on a form, it just pops up the next form and keeps cycling through them until all the forms have been opened and closed. What I can't figure out is, a simple VBA line that will just cancel the whole macro/module when a cancel button is pushed.

Does anyone know of a simple line (or lines) of code that would just kill the macro when a user pushes a cancel button? Thanks!
CapCity12thMan
How long do you want to ignore this user?
AG
how about use Google Forms?

SomeRandomAg#2580
How long do you want to ignore this user?
AG
.
logs10
How long do you want to ignore this user?
AG
One option would be to declare a global variable within the module where your code is written that would dictate whether the code continues to run or not. The code then initialized on a command button click would set the global variable to the value that would fire the Exit Sub line in the if statement, example below:

--Code

Global var1 As Boolean
If var1 = True
Exit Sub
End If

--Command Button

Private Sub myButton Click()
var1 = True
Exit Sub
End Sub

You could also probably do something simple by wrapping your code in a Do While loop and using some parts of the example above.

Hope that helps.

MidnightYell2003
How long do you want to ignore this user?
AG
CapCity12thMan said:

how about use Google Forms?
I would love to. For now, my boss is having me deal with this massive word doc thing since that's what we have for now. The whole point is to generate text in a 'legal' manner so that all users of this would be on the same page, so to speak. The only real usage of this word doc is to generate a block of text based on the inputs of all these forms.
MidnightYell2003
How long do you want to ignore this user?
AG
that1guydrew said:

Should be easy, post the snippet of code for one of the questions so I can see how the inputbox and response are being handled and I'll reply with code on how to cancel.
I'm not sure if this helps of not, but here is the text from one of the forms that goes back into the main module:


Quote:

Private Sub UserForm_Initialize()

SixteenthBox.AddItem "NW1/4 "
SixteenthBox.AddItem "NE1/4 "
SixteenthBox.AddItem "SE1/4 "
SixteenthBox.AddItem "SW1/4 "

QuarterBox.AddItem "NW1/4"
QuarterBox.AddItem "NE1/4"
QuarterBox.AddItem "SE1/4"
QuarterBox.AddItem "SW1/4"

SectionBox.AddItem "1"
SectionBox.AddItem "2"
SectionBox.AddItem "3"
SectionBox.AddItem "4"
SectionBox.AddItem "5"
SectionBox.AddItem "6"
SectionBox.AddItem "7"
SectionBox.AddItem "8"
SectionBox.AddItem "9"
SectionBox.AddItem "10"
SectionBox.AddItem "11"
SectionBox.AddItem "12"
SectionBox.AddItem "13"
SectionBox.AddItem "14"
SectionBox.AddItem "15"
SectionBox.AddItem "16"
SectionBox.AddItem "17"
SectionBox.AddItem "18"
SectionBox.AddItem "19"
SectionBox.AddItem "20"
SectionBox.AddItem "21"
SectionBox.AddItem "22"
SectionBox.AddItem "23"
SectionBox.AddItem "24"
SectionBox.AddItem "25"
SectionBox.AddItem "26"
SectionBox.AddItem "27"
SectionBox.AddItem "28"
SectionBox.AddItem "29"
SectionBox.AddItem "30"
SectionBox.AddItem "31"
SectionBox.AddItem "32"
SectionBox.AddItem "33"
SectionBox.AddItem "34"
SectionBox.AddItem "35"
SectionBox.AddItem "36"

TownDirBox.AddItem "S.,"
TownDirBox.AddItem "N.,"

RangeDirBox.AddItem "W.,"
RangeDirBox.AddItem "E.,"

End Sub

Private Sub OKbtn_Click()

Dim Sixteenth, Quarter, Section, Township, _
TownDir, Range, RangeDir, SectDataString As String

Sixteenth = DescMakerForm.SixteenthBox.Text
Quarter = DescMakerForm.QuarterBox.Text
Section = DescMakerForm.SectionBox.Text
Township = DescMakerForm.TownBox.Text
TownDir = DescMakerForm.TownDirBox.Text
Range = DescMakerForm.RangeBox.Text
RangeDir = DescMakerForm.RangeDirBox.Text

SectDataString = Sixteenth & Quarter & " of Section " & _
Section & ", T." & Township & TownDir & " R." & _
Range & RangeDir & " "

If TakingOption.Value = True Then
Word.Application.Run "Caption", "Taking", _
(SectDataString)

ElseIf PerpEaseOption.Value = True Then
Word.Application.Run "Caption", "Perpetual", _
(SectDataString)

ElseIf TempEaseOption.Value = True Then
Word.Application.Run "Caption", "Temporary", _
(SectDataString)

ElseIf TotalOption.Value = True Then
Word.Application.Run "Caption", "Total", _
(SectDataString)

Else 'STOption.Value = True
Word.Application.Run "Caption", "Severed", _
(SectDataString)

End If

DescMakerForm.Hide

End Sub
MidnightYell2003
How long do you want to ignore this user?
AG
logs10 said:

One option would be to declare a global variable within the module where your code is written that would dictate whether the code continues to run or not. The code then initialized on a command button click would set the global variable to the value that would fire the Exit Sub line in the if statement, example below:

--Code

Global var1 As Boolean
If var1 = True
Exit Sub
End If

--Command Button

Private Sub myButton Click()
var1 = True
Exit Sub
End Sub

You could also probably do something simple by wrapping your code in a Do While loop and using some parts of the example above.

Hope that helps.


I tried that and got an error. This is what I used:
Global btnCancel As Integer
If btnCancel = 1 Then
Exit Sub
End If

and it's in the global declarations section.
When I run it, it gives me a message that says "Compile Error: Invalid outside procedure"
and it highlights my variable of "btnCancel"

Does integer not work here?
EDIT: tried boolean and got the same error message.
Bregxit
How long do you want to ignore this user?
AG
Found the following out on the innertubes...been a long time since I played with VBA so don't know if this works. Looks like you set a boolean value if cancel is clicked then have checks throughout your functions.

Quote:

Add another button called "CancelButton" that sets a flag, and then check for that flag.

If you have long loops in the "stuff" then check for it there too and exit if it's set. Use DoEvents inside long loops to ensure that the UI works.

Bool Cancel
Private Sub CancelButton_OnClick()
Cancel=True
End Sub
...
Private Sub SomeVBASub
Cancel=False
DoStuff
If Cancel Then Exit Sub
DoAnotherStuff
If Cancel Then Exit Sub
AndFinallyDothis
End Sub
MidnightYell2003
How long do you want to ignore this user?
AG
Thanks for all the replies.

It looks like one simple word kills all the macros and what not:

End

From what I can tell, this is working so far. So, in my "cancel" button I made on the form, End just ends it all.


Quote:

Private Sub btnCancel_Click()
End
End Sub
Refresh
Page 1 of 1
 
×
subscribe Verify your student status
See Subscription Benefits
Trial only available to users who have never subscribed or participated in a previous trial.