Calling a function within ThisDocument in Microsoft Word (VB.Net/VBA) - vb.net

I have a VB.NET program that adds code to the ThisDocument module in MS Word in the following way:
wrdDoc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString(sCode)
Which creates the following sub:
Public Sub Example_Click()
Msgbox "Working"
End Sub
I used to call this via the click event of a command button however it would be desirable call it from a ribbon button created as part of a COM Add-In.
I have been able to call global macros successfully in the Add-In by adding the following code to the button's click sub,.
Globals.TestWordApp.Application.Run("Macro1")
All attempts to run code contained within ThisDocument have failed however.
Any help would be greatly appreciated.

Application.Run runs macros.
Macros exist in modules, and ThisDocument is a class module.
Try creating a new module or use an existing module instead of ThisDocument.

Related

Error 1004 when running macro in another workbook via Application.Run

I have a number of Report Templates. Each Template calls a CommonMacroMR, which resides in the Documents - Folder. The executed code resides in ThisWorkbook Module.
When opening the report, immediately Excel comes up with
Run-time error '1004':
Cannot run the macro 'CommonMacroMR.xlsm!Workbook_Open'. The macro may not be available in this workbook or all macros may be disabled.
There is only one line of code in the Workbook_Open Sub of the report template:
Sub WorkBook_Open()
Application.Run ("CommonMacroMR.xlsm!Workbook_Open")
End Sub
In Trust - Center / Macro Settings, following Setting is activated:
Enable all macros
Trust access to the VBA project object model
Why don't you use open method for CommonMacroMR.xlsm
workbooks("CommonMacroMR.xlsm").open
workbook_open is called even if you use workbooks.open to open another file. However, you need to ensure that events are not disabled in your routines.

With Word VBA, can I divide the code module of a Form into two code modules?

In my Word file, I have a form called MyNiceForm. There is a code module associated with it, of course, which contains the _Click() routines. (Maybe those are called event handlers, but that doesn't affect my question.)
I have so many _Click() routines that I'd like to split them into a separate code module, but I can't quite figure out how to do that. I'm somewhat sure that VBA does not allow me to put a _Click() routine in a regular code module (a 'non-Form' code module).
No, the Click routines have to stay in the UserForm code module. In the strictest sense, the UserForm is a class within the document "container". The UserForm designer and the code module are both part of the class / UserForm object. All the buttons' "Click" routines depend on this link.
(This is a similar situation with the Document, itself, and the ThisDocument module in a document's VBA project. The Document is a class/object and ThisDocument the code module. ActiveX controls (which are UserForm controls) on the document surface also rely on the link between the visual part and the code.)
What you can do is move the code inside the Click procedures to another (plain) module. Leave just one line of code in the procedure that "calls" the code in that module. If the code works with the UserForm and/or controls on it you need to pass these objects as arguments.
That could look like this:
'Code in the UserForm module
Private Sub CheckBox1_Click()
CheckBoxClick Me, Me.CheckBox1
End Sub
'Code in a regular module
Sub StartUserForm()
Dim frm As frmTest
Set frm = New frmTest
frm.Show
End Sub
Sub CheckBoxClick(frm As frmTest, ck As MSForms.CheckBox)
Debug.Print frm.NAME, ck.Caption, ck.value
frm.CheckBox2.value = Not ck.value
End Sub

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.

Auto displaying form on opening a template file, dotm from explorer

I have written a form based document generation macro (in VBA) for distribution to a sales team.
For their ease of use, I want to provide a self-contained file which will display the form as soon as the document is opened.
Using AutoOpen I can get the form to display as intended if word is already open and the dotm file is opened within. However, if I double click the file from within explorer, nothing happens and I have to launch the macro manually. I thought AutoExec might allow this but no luck there. I've spent considerable time trying to get this to work through googling etc. but I'm not getting anywhere.
How can I make the form display even when the file is opened with a double click? Is it possible to do this without having to change normal.dotm for each user?
For further background, I am using Word 2013 with macros fully enabled during testing. The dotm file is stored in a trusted location.
I am using a macro to launch the form like this...
Public Sub AutoOpen()
StartPage.Show
End Sub
I have tried using AutoExec as well to no avail.
In the "generator.dotm" file got to Visual Basic and go in to the "ThisDocument" Microsoft Word Object.
At the top of the Visual Basic Editor select "Document" in the left hand side and then click on "New" on the right hand side. Private Sub Document_New() method will appear for you to be able to edit. Then you can call your userform in there. Similar to:
Private Sub Document_New()
Dim myForm As UserForm1
Set myForm = New UserForm1
myForm.Show
End Sub
Save your Generator.dotm and double click it through Windows explorer and you should get the results that you would like.

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.