0
votes

I want the user to be able to review the word document that the macro generates before stopping the macro. This way, they don't have to enter all the data again, since, when the macro stops, all the data is erased. There are a lot of fields that I don't want the user to have to re-enter if something is wrong.

I've tried activating the macro in a couple different ways, from a button on a word document to starting it from VBA Alt+F11, but I get the same result: The user cannot edit or view the word document while the macro is running, even though it isn't doing anything.

''' Private Sub CommandButton1_Click()

InputForm.Show

End Sub '''

The user is not able to view the word document until the macro is stopped and all the data that was input is erased. Alternatively, if it is possible to save the data that has been input so that it is kept for next time the program runs, that would work since I have a "Clear All" button.

1
Where does this input data come from? How can you have a macro but not have any code?David Zemens
The data is input by the user into a form. I have code for the macro, but no code specifically relating to the issue that I posted about.Stomper
The macro is about 3100 lines of code so I didn't want to post it to SOStomper
Can you reduce it or produce instead a minimal reproducible example? For example, a form that only has one input field like a text box?David Zemens
Or, can you show the code for the subroutine that launches the UserForm?David Zemens

1 Answers

0
votes

Somewhere, you have a subroutine that does something like this:

Sub foo()
UserForm1.Show
End Sub

You will need to change that to display the form vbModeless, in order to allow the user to interact with the Document. Note that this will allow the user to interact with the Document while the form is displayed.

Sub foo()
Dim uf As New UserForm1
uf.Show vbModeless

End Sub

If there is any additional executable code after the uf.Show, it may need to be refactored as well, as it will execute immediately after the form is Shown.

If you don't want the user to be able to interact until after the form does whatever it does, then do not use the vbModeless option, and add a QueryClose event handler to the form. This way we ask the user if they want to review; if they say "Yes", then we hide & re-display the form with the vbModeless option. Now, they'll have the form open and the document will be editable.

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If MsgBox("Review document?", vbYesNo) = vbYes Then
    Cancel = 1
    Me.Hide
    Me.Show vbModeless
End If
End Sub

Of course, that may not be quite what you want, either. But those finer points can be worked out separately. There's a lot of things I don't know about your use-case, which can almost certainly be accommodated with proper use of the UserForm. Do note that I can't entertain too many tangentially related follow-up questions, but this should be enough to get you started.

I'm not sure it's possible to persist values in the form beyond it's Terminate event which will happen when the user closes the form or if any code invokes the Unload <userform object>. It would certainly be possible to serialize that data into a text file, or persist it somehow within the Document's custom properties/customer data properties, but that should probably be a separate question.