Consulting

Results 1 to 17 of 17

Thread: Different code for different documents

  1. #1

    Different code for different documents

    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.

  2. #2
    Use macro enabled templates for your documents and save the code required for them in those templates.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    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.

  4. #4
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,956
    Location
    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)

    Capture.JPG

    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

    Capture2.JPG

    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

    Capture3.JPG
    Last edited by Paul_Hossler; 12-26-2019 at 07:53 PM.
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Post the code so we can see what you are doing.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    No, this does not work on my system. The template works but the document does not.

  7. #7
    Quote Originally Posted by gmayor View Post
    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.
    Last edited by Mike Express; 12-27-2019 at 07:14 PM.

  8. #8
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    Quote Originally Posted by gmayor View Post
    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?

  10. #10
    Exactly that.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  11. #11
    Yes, this works very well. Thank you very much.

  12. #12
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,956
    Location
    Quote Originally Posted by Mike Express View Post
    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?
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  13. #13
    Quote Originally Posted by Paul_Hossler View Post
    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.

  14. #14
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,956
    Location
    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
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  15. #15
    Quote Originally Posted by Paul_Hossler View Post
    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.

  16. #16
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,956
    Location
    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
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  17. #17
    Quote Originally Posted by Mike Express View Post
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •