Log in

View Full Version : [SOLVED:] Setting the colour, of the text within a 'set' of bookmarks, to white



PMHBgo
03-19-2020, 09:40 AM
Set bmClassification = ActiveDocument.Bookmarks("Classification").Range
bmClassification.Text = "CONFIDENTIAL"
bmClassification.Font.Color = RGB(237, 28, 36)
ActiveDocument.Bookmarks.Add "Classification", bmClassification

from a post of 31 October 2017: http://www.vbaexpress.com/forum/showthread.php?61190-Changing-font-color-of-a-bookmark-in-VBA

I started a new thread, though I quote from an old one, because I felt that my query, though drawing on the previous, would have been an inappropriate hijacking of the original post.

Dear Paul

I am quite experienced in Excel VBA (High Intermediate C+/B-) but am just beginning my Word VBA journey.

The above code would appear to suit my requirement but I just had a few clarification requests, please.

1. Where would one place this code - in a Subroutine or a Function and where in the code storage places - is it in a Microsoft Word Object, against the document file, or in a module stored within the Word equivalent of an XLAM, whatever file type that may be (I don't know what that file type would be, please tell me)?
2. In respect of the
.Range part of the
ActiveDocument statement - what is this? I recognise this from Excel VBA where one might write, as a range,
Range("A1:B99"), for example, but how does one select, or refer to, a Range in Word - is there a list of key words or what?
3. How does one invoke this code, is it F5? Can I set up a 'situation' where the macro is run every time I open the file, if so, how do I do that, please? If I do set up a file opening invocation, may I still run the macro while editing the file, again would that be using F5 or how, please?
4. My hopes for this adoption / adaptation of your code is that I can, by using it, set the colour, of some blocks of text (specifically, for the text of multiple document areas, which are each marked by a bookmark), to white.
5. To be very specific, I have a lot of similarly named bookmarks, e.g. Set_Family_Number_1, Set_Family_Number_2 ... Set_Family_Number_93, can I, where you have
Bookmarks("Classification") have instead
Bookmarks("Bookmark_Name"), where Bookmark_Name is a text field that is setup within a normal FOR NEXT loop using a sequence of statements such as -




DIM Bookmark_Name as String

DIM I as Long

DIM jbFamily_Grp as Bookmark

FOR I = 1 to 93

Bookmark_Name = "Set_Family_Number_" & I

Set jbFamily_Grp = ActiveDocument.Bookmarks(Bookmark_Name).Range
jbFamily_Grp.Font.Color = RGB(255, 255, 255)
ActiveDocument.Bookmarks.Add Bookmark_Name, jbFamily_Grp

NEXT I



I always like to use Option Explicit when I do my Excel VBA so that is why I have set out the above using all the DIM statements, which I think will be required.

With thanks in anticipation. : pray2: :dunno :banghead: :confused:

macropod
03-19-2020, 02:51 PM
​1. As written, the code could be used in either a Subroutine or a Function in Word; some minor modifications would be required if you wanted to run it against a Word document from Excel.
2. As used in the code you posted, .Range refers to an entire bookmarked range. As in Excel, you can use Range(Start, End), but Start & End refer to character positions in Word. I'm not sure what you mean by ''keywords'; a look under 'Range Object' in the Word VBA help file will take you to a list of Range object's members.
3. You don't - it's just a code snippet from a larger project (e.g. a Subroutine or a Function) which would be 'invoked'.
4 & 5. Try something along the lines of:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument
For i = 1 To .Bookmarks.Count
With .Bookmarks(i)
If .Name Like "Set_Family_Number_#*" Then
.Range.Font.ColorIndex = wdWhite
End If
End With
Next
End With
Application.ScreenUpdating = True
End Sub