Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-12-2024, 08:17 AM
Paulk66 Paulk66 is offline Macro in template documents wont run in other documents Windows 10 Macro in template documents wont run in other documents Office 2021
Novice
Macro in template documents wont run in other documents
 
Join Date: Apr 2024
Posts: 6
Paulk66 is on a distinguished road
Default Macro in template documents wont run in other documents

I wrote some code to colour lines in a table when I run the macro. The works fine in the template document itself (.docm), but when I open a document from the template it doesn't work.

I've a button to press, and also one on the ribbon (although I believe this is just the same thing in two different places). I've been doing VBA for Excel for years, but this is my first foray into Word. I feel I'm missing something, can anyone help?

Code is: (Also a snipping tool version in the attachments) - can't provide the actual document, sorry

Sub ColourTable()


Dim objTable As Table
Dim iRowCounter As Integer
Dim iColumnCounter As Integer

Dim lngBadColor As Long
Dim lngGoodColor As Long
' Predefine colors
lngBadColor = RGB(255, 0, 0)
lngGoodColor = RGB(169, 208, 142)

' Loop through all tables in document
For Each objTable In ThisDocument.Tables

For iRowCounter = 1 To objTable.Rows.Count

For iColumnCounter = 1 To objTable.Columns.Count

' Get Table cell
With objTable.Cell(iRowCounter, iColumnCounter)

If InStr(.Range.Text, "TO BE DONE") > 0 Then .Shading.BackgroundPatternColor = lngBadColor
If InStr(.Range.Text, "Yes") > 0 Then .Shading.BackgroundPatternColor = lngGoodColor
If InStr(.Range.Text, "N/A") > 0 Then .Shading.BackgroundPatternColor = lngGoodColor
If InStr(.Range.Text, "None") > 0 Then .Shading.BackgroundPatternColor = lngGoodColor

' If InStr(.Range.Text, "Very good") > 0 Then .Shading.BackgroundPatternColor = lngVeryGoodColor
' If InStr(.Range.Text, "Minor") > 0 Then .Shading.BackgroundPatternColor = lngGoodColor

End With

Next



Next

Next

End Sub
Attached Images
File Type: png Code.PNG (46.3 KB, 13 views)
File Type: png Organiser.PNG (12.9 KB, 13 views)
File Type: png Toolbar.PNG (32.3 KB, 15 views)
Reply With Quote
  #2  
Old 04-12-2024, 08:54 AM
Italophile Italophile is offline Macro in template documents wont run in other documents Windows 11 Macro in template documents wont run in other documents Office 2021
Expert
 
Join Date: Mar 2022
Posts: 334
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Templates are .dotm, documents are .docm.

From your screenshot the code appears to be in your Normal.dotm, which is a template that only you have access to.

Your code uses
Code:
ThisDocument
which refers to the document or template that contains the code. To use the code with the current document use
Code:
ActiveDocument
instead.
Reply With Quote
  #3  
Old 04-15-2024, 01:04 AM
Paulk66 Paulk66 is offline Macro in template documents wont run in other documents Windows 10 Macro in template documents wont run in other documents Office 2021
Novice
Macro in template documents wont run in other documents
 
Join Date: Apr 2024
Posts: 6
Paulk66 is on a distinguished road
Default

Thanks for coming back to me, and apologies for the dotm/docm confusion.

I made the change you suggested, but unfortunately it's still not working. Do you have any other suggestions?
Reply With Quote
  #4  
Old 04-15-2024, 02:49 AM
Italophile Italophile is offline Macro in template documents wont run in other documents Windows 11 Macro in template documents wont run in other documents Office 2021
Expert
 
Join Date: Mar 2022
Posts: 334
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

From the screenshot in your question showing the Customize Quick Access Toolbar dialog you appear to have the same routine in three modules. It should only be in one, ideally the template (.dotm) that the document will be created from if the template is to be used by others.

Debugging code is very difficult when you can't see the context. For example:

How are you creating a new document from the template?
What are your macro security settings?
Is the template in a Trusted Location?
Reply With Quote
  #5  
Old 04-15-2024, 03:20 AM
Paulk66 Paulk66 is offline Macro in template documents wont run in other documents Windows 10 Macro in template documents wont run in other documents Office 2021
Novice
Macro in template documents wont run in other documents
 
Join Date: Apr 2024
Posts: 6
Paulk66 is on a distinguished road
Default

I appreciate debugging can be difficult like this, and I appreciate any help you can give me. For the points you made:

1. Closed everything down and opened Word again, and went back into the QA Toolbar. It only shows the module once now (on each side) and it's the 'Normal.module' one.
2. The template is stored in folder containing other templates (none of them have macros). It's accessed by clicking on 'File' and 'New'
3. Macro settings are 'Enabled'
4. I can't add the location to the Trusted Locations list. Looks like an Organisational setting that prevents it.

Will the lack of Trusted Location be potentially what's stopping it? If so, why does it work in the template, but not in new documents?
Reply With Quote
  #6  
Old 04-15-2024, 03:40 AM
Italophile Italophile is offline Macro in template documents wont run in other documents Windows 11 Macro in template documents wont run in other documents Office 2021
Expert
 
Join Date: Mar 2022
Posts: 334
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

If the code is in the Normal template then it will already be trusted.

What does "when I open a document from the template it doesn't work" mean exactly?

When I run your code:
Code:
Sub ColourTable()


    Dim objTable As Table
    Dim iRowCounter As Integer
    Dim iColumnCounter As Integer

    Dim lngBadColor As Long
    Dim lngGoodColor As Long
    ' Predefine colors
    lngBadColor = RGB(255, 0, 0)
    lngGoodColor = RGB(169, 208, 142)

    ' Loop through all tables in document
    For Each objTable In ActiveDocument.Tables

        For iRowCounter = 1 To objTable.Rows.count

            For iColumnCounter = 1 To objTable.Columns.count

                ' Get Table cell
                With objTable.Cell(iRowCounter, iColumnCounter)

                    If InStr(.Range.Text, "TO BE DONE") > 0 Then .Shading.BackgroundPatternColor = lngBadColor
                    If InStr(.Range.Text, "Yes") > 0 Then .Shading.BackgroundPatternColor = lngGoodColor
                    If InStr(.Range.Text, "N/A") > 0 Then .Shading.BackgroundPatternColor = lngGoodColor
                    If InStr(.Range.Text, "None") > 0 Then .Shading.BackgroundPatternColor = lngGoodColor

                    ' If InStr(.Range.Text, "Very good") > 0 Then .Shading.BackgroundPatternColor = lngVeryGoodColor
                    ' If InStr(.Range.Text, "Minor") > 0 Then .Shading.BackgroundPatternColor = lngGoodColor

                End With

            Next

        Next

    Next

End Sub
I get
Attached Images
File Type: png Screenshot 2024-04-15 113945.png (9.5 KB, 13 views)
Reply With Quote
  #7  
Old 04-15-2024, 04:43 AM
Paulk66 Paulk66 is offline Macro in template documents wont run in other documents Windows 10 Macro in template documents wont run in other documents Office 2021
Novice
Macro in template documents wont run in other documents
 
Join Date: Apr 2024
Posts: 6
Paulk66 is on a distinguished road
Default

Yeah, what you've shown me is how it's supposed to work (Like a visual checklist)

The template document is called 'Investigation Template'. When I open that actual document, the code works. (Opening the file from the actual file location)

When I open a new document based on 'Investigation Template' - which will open as Document 2 (for example), then it doesn't work. (Opening the file using 'File' and 'New' in Word).

Does that help explain?
Reply With Quote
  #8  
Old 04-15-2024, 05:32 AM
Italophile Italophile is offline Macro in template documents wont run in other documents Windows 11 Macro in template documents wont run in other documents Office 2021
Expert
 
Join Date: Mar 2022
Posts: 334
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

If the code that you are running is exactly as shown in my post then it should work.

Without a copy of your template I can't help any further.

I have attached a template that works correctly.
Attached Files
File Type: dotm Investigation Template.dotm (40.9 KB, 2 views)
Reply With Quote
  #9  
Old 04-15-2024, 08:17 AM
Paulk66 Paulk66 is offline Macro in template documents wont run in other documents Windows 10 Macro in template documents wont run in other documents Office 2021
Novice
Macro in template documents wont run in other documents
 
Join Date: Apr 2024
Posts: 6
Paulk66 is on a distinguished road
Default

I've stripped out the confidential bits (which weren't part of the table).

If you're able to have a look it would be appreciated.
Attached Files
File Type: dotm Investigation Template v3.0.dotm (41.0 KB, 1 views)
Reply With Quote
  #10  
Old 04-15-2024, 10:04 AM
Italophile Italophile is offline Macro in template documents wont run in other documents Windows 11 Macro in template documents wont run in other documents Office 2021
Expert
 
Join Date: Mar 2022
Posts: 334
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

You haven't changed the code:
Attached Images
File Type: png Screenshot 2024-04-15 180335.png (3.6 KB, 9 views)
Reply With Quote
  #11  
Old 04-16-2024, 12:37 AM
Paulk66 Paulk66 is offline Macro in template documents wont run in other documents Windows 10 Macro in template documents wont run in other documents Office 2021
Novice
Macro in template documents wont run in other documents
 
Join Date: Apr 2024
Posts: 6
Paulk66 is on a distinguished road
Default

I made the change, but looks like I changed in the wrong place.

Sorted that now and it works. Thanks very much for your help.
Reply With Quote
Reply

Tags
run a macro, template form



Similar Threads
Thread Thread Starter Forum Replies Last Post
referenceing/finding word from one documents into another documents and indicating their location rok123 Word VBA 1 02-07-2016 04:50 PM
Microsoft Word 2013 Documents open as full version documents on two computers sporadically Jaydenc-Fortress Word 2 08-07-2014 12:34 AM
Office 2010 Can't Open Or Save Documents in My Documents Folder trippb Office 1 07-12-2013 07:29 AM
Macro in template documents wont run in other documents update template in all documents vangxbg Word VBA 1 02-25-2013 04:04 AM
Macro in template documents wont run in other documents Attach a template to 1000+ documents ohmzoned Word 1 02-04-2012 12:09 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:14 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft