Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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, 17 views)
File Type: png Organiser.PNG (12.9 KB, 17 views)
File Type: png Toolbar.PNG (32.3 KB, 19 views)
Reply With Quote
 

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 10:17 PM.


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