How can you view Word documents while macro is running? - vba

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.

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.

Related

Using VBA to allow a checkbox to hide/show content in Microsoft Word

I've gone through a number of tutorials and instructional videos trying to achieve my intended result of simply allowing a checkbox in my form to hide content when selected, or re-show it when being de-selected, but nothing seems to be working.
Currently, I've created a bookmark for the content I want hidden, and try to call his this in VBA with the following statement - which a number of resources indicated as the solution:
Private Sub CheckBox1_Click()
ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = CheckBox1.Value
End Sub
But despite doing this, selecting the checkbox has no affect on the content.
Is there something additional I'm missing (I'm using Microsoft Word 2013).
Your code worked fine when I tested it, but I ran into a few issues since I've never messed with userforms/checkboxs in Word VBA and I suspect you may have the same.
For instance, the first thing I did was create Insert --> Module. This is incorrect. What you want to do is Insert --> Userform then drag a checkbox over from the ToolBox
https://smallbusiness.chron.com/use-check-boxes-word-54673.html
Then double click the checkbox to bring up the "module" it would run, notice there is no module in the side pane! Edit the module, then go back to the userform and press F5. You should be presented with a checkbox that will hide/unhide your text.
Here is my module:
Public Sub CheckBox1_Click()
ActiveDocument.Bookmarks("bookmark").Range.Font.Hidden = CheckBox1.Value
End Sub
Here is an image:
Note: I didn't test how to insert the checkbox into the word doc, I'll leave you some of the learning!
Update:
This sub will make the CheckBox appear when run, but I'm not sure the logic you would use to call it, perhaps an event like opening of document?
Sub Loadform()
Load UserForm1
UserForm1.Show
End Sub
This could be called via a keyboard shortcut or event, but this will cause a "pop-up window". If you want an inform checkbox you may need to look into using this Legacy Form/Checkbox. I was following the URL from above but I think it's dated.
Update:
You could also add this easily to a toolbar, but that isn't likely what you want. I found the best way is to use a "field code" see --> https://word.tips.net/T001571_Assigning_a_Macro_to_a_Button_in_Your_Text.html
I was able to get this to work by pressing CTRL + F9 then typing { MacroButton Loadform ClickMe} then clicking this text. This may be the best bet as using an image seems not to be a great idea.. (https://answers.microsoft.com/en-us/msoffice/forum/all/using-graphic-with-macrobutton/a9c1ef3b-cf1f-48ba-92a8-c44bffcdc131) & (http://www.addbalance.com/usersguide/parts/macrobutton_fields.htm#Different_behavior)
Gif Example:

Click/DoubleClick to open a Userform on Word

I was just creating a template on Word which uses content controls and I have a userform to help auto fill the data which is activated via a command key. You know how on excel VBA you can click a cell to make it refresh or open up a userform, or perform a series of tasks. On Word, can you double or single click a Content Control per say and have a userform pop up?
I think it would be pretty neat to be able to do that, open for any ideas that I should try out.
Unlike Content Controls, ActiveX controls have Click and DoubleClick events. You can add an ActiveX control (e.g., a button or a label) and use its Click event to do whatever you want:
And then simply, use something like this:
Private Sub lblTest_Click()
MsgBox "You clicked on a label."
End Sub
Private Sub btnTest_Click()
MsgBox "You clicked on a button."
End Sub
Result:
Hope that helps.

How to close form without closing the whole Excel?

I've created a Form in excel 2017 with a "Cancel" button, but I don't know how to code it so that it closes itself when clicked. I've found things like "Application.Quit" but it closes everything.
There are two ways to achieve that. Both will only close your form and keep Excel opened.
I've asked a question about it a while ago maybe this can help you.
1. Hide Method
Your userForm has a Hide method, you can call it to hide your form.
Example:
Private Sub btnCancel_Click()
yourFormName.Hide
End Sub
Using the Hide method will only hide the form, it will be totally closed when you close your Excel file.
If you make changes in the form, hide it and then show it again all changes will be kept (ex: change the value of a textbox before hiding it and it will stay the next time you show your form).
The Activate event won't be triggered next time you show the form since the form was still active but hidden.
2. Unload Method
You can call the Unload method to unload your form.
Example:
Private Sub btnCancel_Click()
Unload me
End Sub
The form is unloaded from the memory. If you make changes in your form and then unload it, it won't be kept next time you show your form unlike with the Hide method.
You can use Unload Me to close the Userform.

VB macro to disable "Edit Document" prompt when opening a word document from sharepoint

Is there a way to disable the message (and have the document editable by default):
Server Document To modify document, click Edit Document followed by the button with text "Edit Document".
I cannot find a word setting to do this. In addition I cannot see a way to make a VB macro to do this with a key stroke. I have used a small autohotkey script to position the mouse and click this prompt, but this does not always work since it depends on the position of the window. it is impossible to use the tab key to get to this prompt.
I have to modify about 50+ documents a day from sharepoint, ideally I would like to combine this with another macro which does other automated processing for me. But I can't find a VB solution for clicking the Edit button.
Depending on your security settings (you mentioned that they were blocked), this may or may not work.
Create a new macro enabled template in your Word startup folder (usually at C:\Users[YourID]\AppData\Roaming\Microsoft\Word\STARTUP), and add a new class module. I called mine "AutoEditEnable". You can name it anything, but you'll need it to match how you declare it in the other module.
This code goes in the class:
Option Explicit
Private WithEvents app As Application
Private Sub Class_Initialize()
Set app = Application
End Sub
Private Sub app_ProtectedViewWindowOpen(ByVal PvWindow As ProtectedViewWindow)
PvWindow.Edit
End Sub
Basically, this will hook any Application events you need to - in this case the ProtectedViewWindowOpen event or the ProtectedViewWindowActivate event (either should work).
Put the following code in ThisDocument to grab a reference to it when your template loads:
Option Explicit
Private hook As AutoEditEnable
Private Sub Document_Open()
Set hook = New AutoEditEnable
End Sub
Close Word and restart it, then make sure your new template shows up as a loaded add-in.

How do I access some sort of OnLoad event in Word 2007 VBA?

I am trying to have some code fire when the document is first loaded, but there are two problems. First, I don't know which method to call to get something to fire when the document is first opened. Second, if they have macros disabled, how can I be sure that it gets called when they are enabled?
Thanks!
The Document_Open event is sent when your document is first loaded. To make use of it, enter the following in the VBA code for ThisDocument:
Private Sub Document_Open()
'// your code goes here'
End Sub
As for the disabled macros, I'm not aware of a method that will be called as soon as macros are enabled.