PDA

View Full Version : VBA macro code to change font text color



sdoscher
09-06-2010, 06:49 PM
Hi I am looking for code to change the font text color in an MS Word document.

My purpose is to verify that the user has macros enabled. My thought is that the "default" font text color of the document would be all white (EXCEPT one line that says, in black font color, "This document requires macros enabled. Please reopen the document and choose to enable macros"). This would make it appear as if the only thing in the document was just that one sentence. All the other text would be virtually hidden. However, if user initally choose to enable macros when opening the document, all the document text would be changed to black and the sentence about "needing to enable macros" would be changed to white, making it invisible.



Scott

gmaxey
09-06-2010, 10:07 PM
Well something like this:

Option Explicit
Sub Document_Open()
ActiveDocument.Range.Font.Color = wdColorAutomatic
ActiveDocument.Bookmarks("Notification").Range.Font.Color = wdColorWhite
End Sub
Sub Document_Close()
ActiveDocument.Range.Font.Color = wdColorWhite
ActiveDocument.Bookmarks("Notification").Range.Font.Color = wdColorAutomatic
End Sub


But what if the user isn't using a white background?

sdoscher
09-06-2010, 11:11 PM
Thank you Greg.....I will now have to reveal how little I know about VBA....

:)

I believe you know exactly what I want to do....and thank you so much. So, in terms of how I actually do this...here is what I did with the code.

1. I opened my document
2. using alt-f11, I added that code under Project (myfile.doc)->Microsoft Word Object->ThisDocument

When I open the Word doc and click "disable macro", it behaves as I expected...that is....all the text is hidden because it is "white on white"....however, the message..."This document requires macros to be enabled. Please reopen the document and choose 'Enable Macros'." does not appear....how/where do I define that?

Probably related is this:

Now when I open the document and click ENABLE macros this error comes up: "The requested member of the collection does not exist". When I click debug, it brings me to this line: "ActiveDocument.Bookmarks("Notification").Range.Font.Color = wdColorWhite"

When I close the VB editor and then exit the MS word doc, it throws a similar error and when clicking debug, I am brought to this line, "ActiveDocument.Bookmarks("Notification").Range.Font.Color = wdColorAutomatic"


What am I doing wrong? Do I have to define what "Notification" is? Is "Notification" the message that I want to display ("This document requires macros to be enabled. Please reopen the document and choose 'Enable Macros".)

Regarding the white background...I see your point and it is a good one. I will have to accept that as a potential gap... but I think the risk is fairly low since I am creating the document that people will be using in this case, so I have some control over that.

Scott

gmaxey
09-07-2010, 04:52 AM
Scott,

Sorry for the over your head first reply and the preceding is not intended to disparage.

The "notification" line would be the line in the text that you want if macro's are disabled. You need to indentify that line with a bookmark.

Select the line of text and then insert a bookmark at the selection named "Notification."

Now assuming that you have macros enabled, when you close the document all of the text is formatted white. Then the code will reformat the text within the bookmark to wdColorAutomatic. The process is reversed when the user opens the document with macros enabled.

sdoscher
09-09-2010, 06:33 AM
Thank you Greg. No Problem, I just appreciate you replying. It works perfectly now.

Scott