PDA

View Full Version : Change Background Color on opening Word File



NickS1965
04-05-2012, 10:59 AM
I'm hoping that someone can help me... I'm trying to develop a Word Document to assist a colleague with Dyslexia. She finds it easier to read an on-screen document if the background is pale pink and the text is dark blue. I have created a Word Doc in Word 2003 (the version at work) which has the following code:

Private Sub clickMe_Click()

Dim MyA, MyB, MyC, MyD, MyE, MyF As Integer
Dim dlgOpen As FileDialog
'Open the File Dialog Box to allow a file to be selected

Set dlgOpen = Application.FileDialog(FileDialogType:=msoFileDialogOpen)

With dlgOpen


.AllowMultiSelect = False
.Show
.Execute

End With

'Collect colour values from the master version
'MyA to MyC are the background colours
MyA = txtA.Value
MyB = txtB.Value
MyC = txtC.Value

'MyD to MyF are the Font colours
MyD = txtD.Value
MyE = txtE.Value
MyF = txtF.Value

'Select the whole text from the document
Selection.WholeStory
'Set the font color to the master version
Selection.Font.Color = RGB(MyD, MyE, MyF)
Selection.Font.Size = 14
Selection.Font.Name = "Arial"
Selection.HomeKey Unit:=wdStory

'Set the Active Document Background Color to the master version
ActiveDocument.Background.Fill.ForeColor.RGB = RGB(MyA, MyB, MyC)
ActiveDocument.Background.Fill.Visible = msoTrue
ActiveDocument.Background.Fill.Solid
End Sub

This allows the user to click on a button, select the file she wants to open from her drives, and theoretically it opens the document with the altered standard colored text and background color.

Except it doesn't always do it... Sometimes the text changes and the background stays the same.

In those cases (the background always changes in Web Layout, but we want it in Print Layout) to get it to work, you have to alter the background color manually and then press Ctrl+Z to "undo" and then the chosen salmon pink color appears as if by magic.

Can anybody help me please. She is already finding the assist of great help, but it could be better if it worked as required every time.

Regards,
Nick

fumei
04-05-2012, 01:57 PM
I am having difficulty understanding, as you are not properly declaring things.
MyA = txtA.Value
MyB = txtB.Value
MyC = txtC.Value
What on earth is txtB.Value (or txtA.Value, or txtC.Value)? Your code shows nothing.

NickS1965
04-05-2012, 02:22 PM
Hi,
Apologies for not explaining better.

On the original word document there are 6 text boxes. 3 for the RGB background color (Red, Green, Blue values between 0 and 255) and three for the font color.

'txtA' is the text box where the user enters the Red value, hence 'txtA.Value' is an integer which is passed to 'MyA' for use in the RGB(N,N,N) statement.

'txtB' is the Green value, and 'txtC' is the Blue value.

When the code is run, the VBA picks up the values from the three text boxes and concatenates these into RGB(MyA, MyB, MyC)

Hope this explains things.

Nick

fumei
04-05-2012, 04:32 PM
1. textboxes have strings, not Integers
2. VBA does not use Integers anymore, all integers are converted to Long

Question. You have textboxes the USER enters a Red value? What if they make a mistake?? What if they type in..."red"?

If you have a user with specific background/font color requirements, why have ANY user input? Why not just make her documents the colors she needs?

Having users picks RGB values individually seems very very strange...at best.

NickS1965
04-06-2012, 12:38 PM
OK,

Research shows that approximately 35-40% of Dyslexics are assisted in readability by use of colour. Each dyslexic sufferer is different and will need different colour choices to make a document readable to them. So, as I work in an organisation with 45,000 computer users, it made sense to create a tool that may assist more than just one user - therefore allowing them to select their own colour variations.

Secondly, although the user for who this tool was developed has a work supplied laptop, she sometimes has to use an alternative workstation. Unfortunately, we do not have colour corrected monitors and therefore the user finds that on one screen the tool works fine, but on another the words "march across the screen like ants" - that's what happens to dyslexia sufferers, and what I am trying to help fix. The difference can be as little as 3 or 4 in the Green value, but the effect is major.

Those are the reasons for having the users pick RGB values individually - they actually need the full 16,500,000 colours available to choose from.

You're right - there is no error checking in this piece of code, but there is elsewhere. It doesn't allow them to enter text other than a number between 0 and 255. If they type in "red" they get an error message. The document contains text that explains what they need to do.

So, do you have any answers to the original question please? Why is it working in Web layout view, but only occasionally working in Print layout view? Why if you manually change the colour and then do Ctrl+Z does it then show the selected colour in Print layout view?

I have tried changing the Dim statement to read Long rather than Integer but it has not changed the way the code responds

Nick

fumei
04-06-2012, 03:23 PM
I am still not sure what is happening. What is "master version"?

The FileDialog will open a file. THAT file is now ActiveDocument. So where are these textboxes? Some other document? The original document? In which case, how can txtA.Value work?

As for the not always showing correctly in PrintView, I do not know. What I can suggest though is a programmatic scroll to the bottom, and then to the top of the document. This will cause a GUI refresh. OR, do a programmatic refresh.Application.ScreenRefresh

This is essentially what happens with your manual change Ctrl-Z.

NickS1965
04-07-2012, 01:06 AM
You'll be pleased to know that I have managed to get it working.

The Application.ScreenRefresh didn't help, even if nested between an Application.ScreenUpdating = False / Application.ScreenUpdating = True.

However, what I did find buried deep in the Word VBA Help files was a statement: ActiveDocument.ActiveWindow.View.DisplayBackgrounds = True

This seems to have fixed it.

Regards and thanks for your interest

fumei
04-07-2012, 07:40 AM
Ok. Even better that you rooted through Help and found what worked yourself.