Link to home
Start Free TrialLog in
Avatar of JohnRobinAllen
JohnRobinAllenFlag for Canada

asked on

Use VBA with Word to make a shaded character style revert back to the current page background

Using VBA with Word, I am writing a program that displays or hides certain letters and words in a document. To make the document more elegant, I set its background colour to “Parchment.” To highlight certain words by revising their character style to use shading, one of the options in Borders and Shading. I can change the display by changing the character style of the words with code such as this to revise my self-defined  “Mont TC delete” character style:
     With ActiveDocument.Styles("Mont TC delete").Font
                 .Color = 5287936
                 With .Shading
                       .Texture = wdTexture20Percent
                       .ForegroundPatternColor = wdColorAutomatic
                       .BackgroundPatternColor = -687800423
                 End With
                 With .Borders(1)
                       .LineStyle = wdLineStyleSingle
                       .LineWidth = wdLineWidth050pt
                       .Color = wdColorAutomatic
                 End With
                 .Borders.Shadow = True
           End With
     The attached document shows some examples. It shows the word “faire” highlighted with a border and shading added through VBA. It also shows the words “savoir aussi” that at one point were highlighted like “faire” but now should blend in with the other words and be invisible.
     My problem is the background around “savoir aussi”. It should match the parchment colour of the document, but, instead, it displays a clear white background. I can manually get rid of the white background by selecting those words and then by pressing Ctrl + Space. My problem is how to do that by using VBA to revise the properties of their character style.
     The obvious simple solution is to forget about having a parchment background. That would be a pity, since the text is so much easier to read with the appropriate background.
     What can I do?
     —John Robin
EE-query.docx
Avatar of DrTribos
DrTribos
Flag of Australia image

I did not look at the doc, but I think this puts you in the right direction, HTH

Sub ResetFont()

Dim oPara As Paragraph

For iPara = 1 To ActiveDocument.Paragraphs.Count
Set oPara = ActiveDocument.Paragraphs(iPara)
With oPara
.Range.Font.Reset
End With
Next iPara

End Sub
Avatar of JohnRobinAllen

ASKER

I'm sorry for the delay in answering DrTribos's suggestion. Unfortunately it will not work. It resets the entire paragraph that has other character styles. In brief, I have two character styles: characters to delete and characters to insert.
     (a) When I want to show the original document without revisions, I make the "delete" characters visible and the inserted characters hidden.
     (b) When I want to show the revised document, I do the opposite: hide the "deleted" characters and show the "insert" characters.
     (c) When I want to show everything: what is to be deleted and what is to be inserted, I show both styles and highlight them by putting a border around them, put a bar through the "delete" characters, underscore the "insert" characters and use different font colours and shading to further distinguish the two character styles.
     When I want to return to one  of the other modes, I cannot get ride of the shading. I do not want to delete all character styles from the paragraph because I then could not show any of the (a), (b), or (c) views.
     I can get rid of the shading if I make my background colour be white, but that would be such a drastic solution I would prefer, for lack of anything better, not to use shading to distinguish between the styles while retaining a glorious parchment background.
     I hope there is a solution, but I also hope for world peace. Perhaps I will get neither.
     --J.r.
Avatar of irudyk
DrTribos is on the right track.  Instead of applying it to the paragraph object, you would select the specific text (i.e. using the VBA code you used to select and apply the shading and border thereto) and then do:
Selection.Font.Reset
@irudyk, Thanks :-)

So... if I understand correctly you are relying on styles to differentiate between different types of edits...

There seems to be a bug in Word whereby once you specify a colour there is no way to specify 'no colour' or in other words 'undefined'.  This is the problem that I encountered and the only solution that I am aware of was font.reset.

So world peace may now depend on the ability to:

1. determine what the current style is & should be
2. reset
3. reapply the elements of style that should be


OR...
don't use shading


Peace out!
SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi.... yes thats what I thought too. It fails to meet expectations in some cases.  I think, from memory, I had this problem with table cell  shading in Word 2010. It might have been possible to reset shading by applying a table style but it all seemed quite convoluted at the time...
Ahhhh and I just remembered that setting font colour back to automatic gave different results when table shading was applied (i.e. compared to other text that was never changed from its original wdColorAutomatic).  It was very troublesome!

Resetting the font was the only way I found to truly get predictable behavior.

HTH
I see, interesting.  So, I'd think that resetting the font at the Selection level should do the trick.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Brief note: It has been so long since I worked with this particular problem that I have to go back and try to recreate the environment that generated that sample file I uploaded.
     I'm working on that now, Saturday, August 1, and hope to get back to the problem either today or tomorrow. It certainly would be nice to solve it. Almost as nice as having world peace, but if we cannot have that, I'll settle for a solution to my formatting problem.
              j.r.a.
I know that feeling! No worries 😊
I apologize for not having gotten back to this problem, but that should end soon. I have some particularly bad deadlines during the next seven to ten days, but I still hope to return to the problem before a week has passed.
     At least twice in the past I have designated in EE as correct solutions to different problems that seemed fine but which I had not checked thoroughly. I subsequently found the accepted solutions were not correct.
     I similarly suspect that the solution proposed above is good even if it does not do quite what I wanted, but I do not want to repeat my mistakes of the past. Therefore I plead with the powers that be of EE not to close this question until I have a chance to test the proposed solutions. In the meantime I fully accept the status of not being able to ask new questions until this one is resolved.
     Thanks for your patience.
     JRA
Hello JRA,

I hate the thought of you not being able to ask new questions; but on the other hand I applaud EE for this step ;-)  I have also found myself accepting solutions that were NQR...   So, conflicted as I was, I opened your document and selected the bothersome text.  Then, in the immediate window of the VBA IDE I did the following:

?activedocument.Name
EE-query.docx  <-- Result
?selection.Text
savoir aussi  <-- Result
set rng = selection.Range  <-- Assign selection to range
?rng.text  <-- result confirms range is properly assigned
savoir aussi 
rng.font.reset <-reset the font in the range

Open in new window



Rest assured the result was the same as selecting the bothersome text and pressing Ctrl + Space.  

Now, this is brutal but as far as I know the only way to achieve the outcome you're after.  If you have other text level formatting it will also disappear (just like pressing Ctrl + Space).  You can, of course write code to capture every possible element of formatting and reapply....

Good luck with the deadlines :-)
I was unable to reproduce the problem, but I believe that the solution suggested by both Dr Tribos and Irudyk could solve it.  
     After next July I may well return to this problem and then be able to reproduce it.
     Along with Dr Tribos, I too am grateful that EE does not allow new questions when previous questions are unresolved. I have nothing but praise for all the help that EE has given me over the past fifteen years.
:-) Blast from the past.... Guessing you just ticked off a few 'New Years resolutions' 😂