PDA

View Full Version : Different code for different documents



Mike Express
12-14-2019, 09:20 AM
I'm a beginner with VBA. I have a number of different documents for which I want to write different code. But I get the code (macro) working for one document and save that document. But when I change the code for a different document, then the code no longer works on the first document. How do I permanently associate a macro with only one document. And how do I change the code for a different document? I'm hoping this is easy question. Thanks.

gmayor
12-14-2019, 09:22 PM
Use macro enabled templates for your documents and save the code required for them in those templates.

Mike Express
12-26-2019, 02:39 PM
Yes, of course I save as dotm. The template name is "Macro Testing.dotm". And when I Open the document as a template, the code works. But when I open a New document, it opens as "Document1.doc", and the code only generates an error message, "Object variable or With block variable not set".

My "Macro Testing.dotm" is a test form with various checkboxes and text fields. This dotm template is protected to Allow only this type of editing in the document: Fill in forms. This means that in order to make changes to the document (such as the color of form fields) my code must first unprotect the document, make the changes, and re-protect the document. But this slows down the code too much. So all my code is doing is re-centering the active field and displaying a message box if the values entered are not within tolerance; this make no changes to the document itself, so there is no need to unprotect the document to run this code. It runs much faster.

When I Open "Macro Testing.dotm" and open VB editor, the code is found in Project (Macro Testing)/Microsoft Word Object/ThisDocument; there is no code in Normal/Microsoft Word Object/ThisDocument.

My code is trival, but I'm new to VBA. I can get the code to run well in the dotm. But I can not create a document based on this template and run the code in that new document. So I can't distribute the template so others can open it as a new document. Any help is appreciated.

Paul_Hossler
12-26-2019, 07:10 PM
I think you might be misunderstanding the way Word handles templates (DOTX or DOTM)

If you

1. create templates (e.g. External.DOTX and Internal.DOTM) with the specific styles, boilerplate text, customized macros, ribbon customization, etc. and

2. save them in your templates folder (mine is user/Templates, but I think I changed the default long ago)

25693

3. when you use File, New and pick the desired template, it will create a DOCM based on that template with the formating, styles boilerplate text and macros

25694

It sounds like you're creating a document based on the Normal template

If you want to have the macros available all the time, you can put your DOTM in the Startup folder

25695

gmayor
12-26-2019, 09:28 PM
Post the code so we can see what you are doing.

Mike Express
12-27-2019, 02:48 PM
No, this does not work on my system. The template works but the document does not.

Mike Express
12-27-2019, 02:50 PM
Post the code so we can see what you are doing.

The code is simple,



Sub TP5_0()
'
' TP5_0 Macro
'
'
Dim contents1 As String
contents1 = ActiveDocument.FormFields("Number3").Result
Value1 = Val(contents1)
If ((Value1 > 5.1) Or (Value1 < 4.9)) And Not (contents1 = "---.---") Then Beep: MsgBox ("Out of Tolerance")

End Sub



Sub SelectionScrollIntoMiddleOfView()
Dim pLeft As Long
Dim pTop As Long, lTop As Long, wTop As Long
Dim pWidth As Long
Dim pHeight As Long, wHeight As Long
Dim Direction As Integer

wHeight = PixelsToPoints(ActiveWindow.Height, True)
ActiveWindow.GetPoint pLeft, wTop, pWidth, pHeight, ActiveWindow
ActiveWindow.GetPoint pLeft, pTop, pWidth, pHeight, Selection.Range

Direction = Sgn((pTop + pHeight / 2) - (wTop + wHeight / 2))
Do While Sgn((pTop + pHeight / 2) - (wTop + wHeight / 2)) = Direction And (lTop <> pTop)
ActiveWindow.SmallScroll Direction
lTop = pTop
ActiveWindow.GetPoint pLeft, pTop, pWidth, pHeight, Selection.Range
Loop
End Sub


TP5_0() checks the tolerance on text input form fields. SelectionScrollIntoMiddleOfView() centers the active form field in the middle of the screen. I place SelectionScrollIntoMiddleOfView() in the Run macro on/Entry in the Form Field Options box when I want to place the field in center screen. I place TP5_0() in the Run macro on/Exit in the Text Form Field Option box of the text box I wish to check tolerances. None of this changes the document itself in anyway. So I don't have to unprotect the document and re-protect the document.

The file name is "Testing macro distribution" and the code is in project ( Testing macro distribution). There is no code in Normal.

The first thing I want to do is be able to open a new document based on this template and run the code on the document as well. The second thing I want to do is be able to publish the template so that anyone at my company will be able to use this template to create new documents which will also run the code. I do appreciate any help given in this regard.

So far the only thing that works is the template, and it works also when I email the template to myself.

I think I need to understand more about where to place code, in project or in Normal, and why.

gmayor
12-27-2019, 09:44 PM
The code should be in a new module in the document itself, which should then be saved as a macro enabled template. "Testing macro distribution.dotm"
New documents created from that template should then run the macro, subject to security settings on the recipient's PC.

Mike Express
12-27-2019, 10:01 PM
The code should be in a new module in the document itself, which should then be saved as a macro enabled template. "Testing macro distribution.dotm"
New documents created from that template should then run the macro, subject to security settings on the recipient's PC.


Thanks ever-so-much. What is "a new module in the document itself"? Is this right-click on Project (Testing macro distribution) in the VB Editor and choose Insert/Module, and put the code there and only there?

gmayor
12-27-2019, 10:41 PM
Exactly that.

Mike Express
12-29-2019, 10:29 AM
Yes, this works very well. Thank you very much.

Paul_Hossler
12-29-2019, 05:55 PM
No, this does not work on my system. The template works but the document does not.

Did you create a new document based on the Macros Testing DOTM template, or just create a default new document, which would use Normal.dotx as a start, and which wouldn't have your macros in it?

Mike Express
12-30-2019, 09:05 AM
Did you create a new document based on the Macros Testing DOTM template, or just create a default new document, which would use Normal.dotx as a start, and which wouldn't have your macros in it?

I saved my file that has various form fields as a .dotm. Then I added a module to the project section in the VB Editor and put all my code in that module (as described above). There was no code in Normal; it was all in the module. The code ran as expected when I opened the .dotm file. And the code also ran as expected when I double-clicked on the icon to open a New document (.docm) that was based on the template (.dotm). I then changed the code slightly and saved it with a different name as a .dotm. I emailed both of the .dotm files to my self, and the code ran as expected in each file; the altered code ran differently in the changed .dotm as expected. Everything works well when I added the module.

So I'm not sure the purpose of the Normal section in the VB Editor.

Paul_Hossler
12-30-2019, 01:31 PM
I saved my file that has various form fields as a .dotm.

Where did you save the dotm?

When you go to File, New, Custom did you see that dotm as a choice? (similar to para 3 in post #4?

If you create a new document using that template and then save it as a docm the macros, etc. should now be in the docm

Mike Express
12-30-2019, 02:24 PM
Where did you save the dotm?

When you go to File, New, Custom did you see that dotm as a choice? (similar to para 3 in post #4?

If you create a new document using that template and then save it as a docm the macros, etc. should now be in the docm

My File/Option/Save/Default local file location: is C:\Users\Mike\Documents\Custom Office Templates\

This is also my Default personal templates location.

My File/Option/Advanced/General/File locations.../Startup is C:\Users\Mike\AppData\Roaming\Microsoft\Word\STARTUP

When I'm working on the Testing macro distribution.dotm file I saved it to the Desktop for easy access.

Paul_Hossler
12-30-2019, 04:06 PM
When I'm working on the Testing macro distribution.dotm file I saved it to the Desktop for easy access.

Try saving it in "C:\Users\Mike\Documents\Custom Office Templates" and then going to File, New and select the distribution template to make a new document

gmayor
12-30-2019, 09:31 PM
So I'm not sure the purpose of the Normal section in the VB Editor.Macros in the normal template apply to all documents. Macros in the document template apply only to documents created from that template, when the template is available to whoever subsequently edits the document. Macros in document templates take precedence over macros in the normal template. Consider the normal template as a personal scratch pad. There is a third type of template - the add-in template. Macros in the add-in template are available to all documents, but you can only edit the macros in such a template when the template itself is opened.