Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-26-2021, 09:38 AM
sec4zj sec4zj is offline Restrict editing based on background color of a table Windows 10 Restrict editing based on background color of a table Office 2016
Novice
Restrict editing based on background color of a table
 
Join Date: Jul 2021
Posts: 1
sec4zj is on a distinguished road
Default Restrict editing based on background color of a table

Hello!



I have a table in my document where I am trying to restrict editing based on the background color of the table. Basically, if the background color is white (no shading), that's the only place I want people to be able to edit. How can I accomplish this in Word VBA? I've tried looking this up in a few other places, but I can only find advice for Excel VBA.

Any help would be appreciated!
Reply With Quote
  #2  
Old 07-26-2021, 05:23 PM
Guessed's Avatar
Guessed Guessed is offline Restrict editing based on background color of a table Windows 10 Restrict editing based on background color of a table Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 2,554
Guessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to beholdGuessed is a splendid one to behold
Default

My recommendation would be to put Content Controls anywhere you want to be able to edit the contents and then apply document protection for forms. The real question is how to identify the 'white' cells. A 'white' cell could have no fill or a white fill via indexed color or RGB specification. It could also have color on a background or foreground setting so you might need to fiddle with this to identify target cells correctly.

The following code works on my test tables and shows how you could step through the code to help identify cells and add the CCs to the found cells.
Code:
Sub SelectivelyAddCCs()
  Dim aCell As Cell, aCC As ContentControl, aTable As Table
  For Each aTable In ActiveDocument.Tables
    For Each aCell In aTable.Range.Cells
      aCell.Range.Select
      Debug.Print aCell.Shading.BackgroundPatternColorIndex
      If aCell.Shading.BackgroundPatternColorIndex = 0 Then
        If aCell.Range.ContentControls.Count = 0 Then
          ActiveDocument.ContentControls.Add Type:=wdContentControlText, Range:=aCell.Range
        End If
      End If
    Next aCell
  Next aTable
End Sub
Once you have added the CCs you can turn on the Restrict Editing for forms (via the Developer Tab). You will be able to find code on this forum to do this but you can easily record this step to see sample code.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Restrict editing based on background color of a table Changing background colour in table based on value. amandalee Word Tables 21 09-30-2020 03:13 PM
Set color and background of special bullet in table cell kkkwj Word VBA 2 05-08-2018 08:40 AM
Restrict editing based on background color of a table VBA Table Search All Tables - Find & Replace Text in Table Cell With Specific Background Color jc491 Word VBA 8 09-30-2015 06:10 AM
Restrict editing based on background color of a table How to remove background color in a table that isn't there but is there? pintree3 Word 5 10-27-2014 10:23 AM
Restrict editing based on background color of a table Restrict Editing but Allow New Table Rows to be Added weavie27 Word Tables 3 05-02-2014 03:11 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:18 AM.


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