View Single Post
 
Old 08-01-2016, 05:36 AM
ninaiva ninaiva is offline Windows 7 64bit Office 2016
Novice
 
Join Date: Jul 2016
Posts: 5
ninaiva is on a distinguished road
Default

Sorry, I guess I forgot to save the macro in the docx.
After using the code below in the attached file (password: cc), I get msg 'runtime error '5941'. I've read many forum posts on this, I tried different codes but I still can't get it to work. This is just an example that I want to apply later in the document I'm actually working on - it has 24 dropdown menus with 4 options. I want a certain option to color the cells' background. Thanks!

Code:
Option Explicit
Dim i As Long

Private Sub Document_Open()
ActiveDocument.Bookmarks("DropDown").Range.Select
Call Index
End Sub

Sub Index()
  i = Replace(Selection.FormFields(1).Range.Bookmarks(1).Name, "DropDown", "")
End Sub

Sub ColorDropDown()
Dim sText As String, oFld As FormField
With ActiveDocument
  Set oFld = .FormFields("Dropdown" & i)
  sText = oFld.Result
  If .ProtectionType <> wdNoProtection Then .Unprotect Password:=""
  With oFld.Range
    Select Case sText
      Case Is = "G" 'green
        .Font.Color = wdColorBrightGreen
        .Shading.BackgroundPatternColor = wdColorBrightGreen
      Case Is = "Y" 'yellow
        .Font.Color = wdColorYellow
        .Shading.BackgroundPatternColor = wdColorYellow
      Case Is = "R"  'red
        .Font.Color = wdColorRed
        .Shading.BackgroundPatternColor = wdColorRed
    End Select
  End With
  .Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End With
End Sub
Attached Files
File Type: docx DropDown.docx (12.3 KB, 8 views)
Reply With Quote