Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-14-2023, 08:42 AM
darkmaster006 darkmaster006 is offline Change "fill" text colour to no colour Windows 10 Change "fill" text colour to no colour Office 2021
Novice
Change "fill" text colour to no colour
 
Join Date: Aug 2023
Posts: 10
darkmaster006 is on a distinguished road
Default Change "fill" text colour to no colour


Hello! So I've been given various Word files which have, in all the document, the fill "shading" paragraph colour, in white. What I want to do is place a VBA Macro in AutoOpen so that, when I open a document, the fill is changed to no colour. So far, I've come up with a method that goes about it by selecting all text and then changing it, but I'm sure there must be a better way of doing this for all the document. What I have is:
Code:
    Selection.WholeStory
    Selection.Shading.Texture = wdTextureNone
    Selection.Shading.ForegroundPatternColor = wdColorAutomatic
    Selection.Shading.BackgroundPatternColor = wdColorAutomatic

Maybe something similar to this (change font colour to black) could be done?
Code:
' Change all text font to black colour
    ActiveDocument.Content.Font.Color = wdColorBlack
Thank you for any help.
Reply With Quote
  #2  
Old 08-15-2023, 12:45 AM
vivka vivka is offline Change "fill" text colour to no colour Windows 7 64bit Change "fill" text colour to no colour Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hello, darkmaster006! My limited knowledge of VBA says that your code is doing exactly what is expected:
selection.WholeStory
selection.Shading.BackgroundPatternColor = wdColorAutomatic

Maybe, this forum's gurus will have different opinions.
Reply With Quote
  #3  
Old 08-15-2023, 04:37 AM
Italophile Italophile is offline Change "fill" text colour to no colour Windows 11 Change "fill" text colour to no colour Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

You can avoid selecting the text by using the following:

Code:
    With ActiveDocument.Content.ParagraphFormat
        .Shading.Texture = wdTextureNone
        .Shading.ForegroundPatternColor = wdColorAutomatic
        .Shading.BackgroundPatternColor = wdColorAutomatic
    End With
Reply With Quote
  #4  
Old 08-16-2023, 07:14 AM
darkmaster006 darkmaster006 is offline Change "fill" text colour to no colour Windows 10 Change "fill" text colour to no colour Office 2021
Novice
Change "fill" text colour to no colour
 
Join Date: Aug 2023
Posts: 10
darkmaster006 is on a distinguished road
Default

Thank you both!

Quote:
Originally Posted by Italophile View Post
You can avoid selecting the text by using the following:

Code:
    With ActiveDocument.Content.ParagraphFormat
        .Shading.Texture = wdTextureNone
        .Shading.ForegroundPatternColor = wdColorAutomatic
        .Shading.BackgroundPatternColor = wdColorAutomatic
    End With
This seems to be the best solution. It, indeed, avoids selecting the text, which is what I wished to happen.
For texts that are all formatted as "one paragraph", this works perfectly. Example:


This gets "cleaned" well, like this:


Yet, when I get a text that has "multiple paragraphs", like this:


Nothing gets changed. Do you have any idea how to do it to texts like these too? The only way I've found, so far, is to paste them to another document, without formatting. It's not ideal, but it's what I've found worked so far. There must be an easier way to do it in-document and with macros, like the code above.
Reply With Quote
  #5  
Old 08-16-2023, 09:11 AM
vivka vivka is offline Change "fill" text colour to no colour Windows 7 64bit Change "fill" text colour to no colour Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi! Back to my previous answer.
The both will work:

Code:
 
Sub Test() 
 
   selection.WholeStory
   selection.Shading.Texture = wdTextureNone
   selection.Shading.ForegroundPatternColor = wdColorAutomatic
   selection.Shading.BackgroundPatternColor = wdColorAutomatic
   selection.Collapse 1 'this deselects the text
 End Sub
Or:


Code:
Sub Test()

     With ActiveDocument.range
        .Shading.Texture = wdTextureNone
        .Shading.ForegroundPatternColor = wdColorAutomatic
        .Shading.BackgroundPatternColor = wdColorAutomatic
     End With
End Sub
Reply With Quote
  #6  
Old 08-16-2023, 02:40 PM
darkmaster006 darkmaster006 is offline Change "fill" text colour to no colour Windows 10 Change "fill" text colour to no colour Office 2021
Novice
Change "fill" text colour to no colour
 
Join Date: Aug 2023
Posts: 10
darkmaster006 is on a distinguished road
Default

Quote:
Originally Posted by vivka View Post
Hi! Back to my previous answer.
The both will work:

Code:
 
Sub Test() 
 
   selection.WholeStory
   selection.Shading.Texture = wdTextureNone
   selection.Shading.ForegroundPatternColor = wdColorAutomatic
   selection.Shading.BackgroundPatternColor = wdColorAutomatic
   selection.Collapse 1 'this deselects the text
 End Sub
Or:


Code:
Sub Test()

     With ActiveDocument.range
        .Shading.Texture = wdTextureNone
        .Shading.ForegroundPatternColor = wdColorAutomatic
        .Shading.BackgroundPatternColor = wdColorAutomatic
     End With
End Sub
Hello, thank you!
Sadly, it seems that this leaves it with a complete white background (can't figure out why):
Reply With Quote
  #7  
Old 08-17-2023, 12:09 AM
Italophile Italophile is offline Change "fill" text colour to no colour Windows 11 Change "fill" text colour to no colour Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Quote:
Originally Posted by darkmaster006 View Post
Nothing gets changed. Do you have any idea how to do it to texts like these too?
Without seeing the document, it is only possible to guess.
Reply With Quote
  #8  
Old 08-17-2023, 02:37 AM
vivka vivka is offline Change "fill" text colour to no colour Windows 7 64bit Change "fill" text colour to no colour Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi again! Unfortunaltely, from the very beginning it was not clear that the document had a page filling color. After the misunderstanding has been corrected, here's the code that seems to work:

Code:
Sub Deshading()

Dim oPara As Paragraph
'If the selection includes tables, error 4605 will be returned.
'Thus, an error handler is needed:
On Error Resume Next

Application.ScreenUpdating = False
    For Each oPara In ActiveDocument.range.Paragraphs
        oPara.range.Select
        If Not selection.Shading.BackgroundPatternColor = wdColorAutomatic Then
            selection.Shading.BackgroundPatternColor = wdColorAutomatic
        End If
    Next oPara
Application.ScreenUpdating = True
End Sub

Last edited by vivka; 08-17-2023 at 09:53 PM.
Reply With Quote
  #9  
Old 08-18-2023, 08:44 PM
darkmaster006 darkmaster006 is offline Change "fill" text colour to no colour Windows 10 Change "fill" text colour to no colour Office 2021
Novice
Change "fill" text colour to no colour
 
Join Date: Aug 2023
Posts: 10
darkmaster006 is on a distinguished road
Default

Quote:
Originally Posted by Italophile View Post
Without seeing the document, it is only possible to guess.
Oh, alright! I've uploaded an example file with the text that I talked about, if that helps.

Quote:
Originally Posted by vivka View Post
Hi again! Unfortunaltely, from the very beginning it was not clear that the document had a page filling color. After the misunderstanding has been corrected, here's the code that seems to work:

Code:
Sub Deshading()

Dim oPara As Paragraph
'If the selection includes tables, error 4605 will be returned.
'Thus, an error handler is needed:
On Error Resume Next

Application.ScreenUpdating = False
    For Each oPara In ActiveDocument.range.Paragraphs
        oPara.range.Select
        If Not selection.Shading.BackgroundPatternColor = wdColorAutomatic Then
            selection.Shading.BackgroundPatternColor = wdColorAutomatic
        End If
    Next oPara
Application.ScreenUpdating = True
End Sub
Thank you for the help! This does not seems to work, sadly. Furthermore, it seems to select the last paragraph and make the cursor go to the end of the document.
Attached Files
File Type: docx example.docx (12.9 KB, 3 views)
Reply With Quote
  #10  
Old 08-19-2023, 05:29 AM
vivka vivka is offline Change "fill" text colour to no colour Windows 7 64bit Change "fill" text colour to no colour Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi! Is your example is what you want to get or what you don't want to get but get?
Reply With Quote
  #11  
Old 08-19-2023, 06:22 AM
vivka vivka is offline Change "fill" text colour to no colour Windows 7 64bit Change "fill" text colour to no colour Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

This code replaces automatic shading with the page color set in your example document:
Code:
Sub Another_Go()
'
Application.ScreenUpdating = False
'If the selection includes tables, error 4605 will be returned.
'Thus an error handler is needed:
On Error Resume Next
    For Each oPara In ActiveDocument.range.Paragraphs
        oPara.range.Select
        selection.End = selection.End - 1
        selection.Shading.BackgroundPatternColor = -654246042
    Next oPara
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #12  
Old 08-19-2023, 11:39 AM
Italophile Italophile is offline Change "fill" text colour to no colour Windows 11 Change "fill" text colour to no colour Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Quote:
Originally Posted by vivka View Post
This code replaces automatic shading with the page color set in your example document:
I’m pretty sure you’ll find that the page color has only been set to make the text shading visible. Kinda hard to see white shading against a white background.
Reply With Quote
  #13  
Old 08-19-2023, 02:01 PM
vivka vivka is offline Change "fill" text colour to no colour Windows 7 64bit Change "fill" text colour to no colour Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Italophile, thank you for clearing up the misunderstanding. My n-th try:
Code:
Sub Test_Nth()

Application.ScreenUpdating = False
'If the selection includes tables, error 4605 will be returned.
'Thus an error handler is needed:
On Error Resume Next
    For Each oPara In ActiveDocument.range.Paragraphs
        oPara.range.Select
        selection.End = selection.End - 1
        selection.Shading.BackgroundPatternColor = wdColorAutomatic
    Next oPara
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #14  
Old 08-21-2023, 07:07 AM
darkmaster006 darkmaster006 is offline Change "fill" text colour to no colour Windows 10 Change "fill" text colour to no colour Office 2021
Novice
Change "fill" text colour to no colour
 
Join Date: Aug 2023
Posts: 10
darkmaster006 is on a distinguished road
Post

Quote:
Originally Posted by Italophile View Post
I’m pretty sure you’ll find that the page color has only been set to make the text shading visible. Kinda hard to see white shading against a white background.
Indeed. Although, for a fact, I am using that page colour. It's easier on the eyes than the white. Thank you for all the help!



Quote:
Originally Posted by vivka View Post
Italophile, thank you for clearing up the misunderstanding. My n-th try:
Code:
Sub Test_Nth()

Application.ScreenUpdating = False
'If the selection includes tables, error 4605 will be returned.
'Thus an error handler is needed:
On Error Resume Next
    For Each oPara In ActiveDocument.range.Paragraphs
        oPara.range.Select
        selection.End = selection.End - 1
        selection.Shading.BackgroundPatternColor = wdColorAutomatic
    Next oPara
Application.ScreenUpdating = True
End Sub
Wow, this works amazingly! Thank you very much for all the help, too!
Just in case, I added:
Selection.HomeKey Unit:=wdStory
(that is, Ctrl+Home)
so that the cursor ends up in the beginning of the document. With that added, this works flawlessly.
Full code, in case anyone wants it:
Code:
Sub AutoOpen()
'
' AutoOpen Macro
'
' This macro opens itself (AutoOpen) automatically with every Word document that's opened normally
' Choose background colour (light green 2)
    ActiveDocument.Background.Fill.ForeColor.ObjectThemeColor = wdThemeColorAccent6
    ActiveDocument.Background.Fill.ForeColor.TintAndShade = 0.6
    ActiveDocument.Background.Fill.Visible = msoTrue
    ActiveDocument.Background.Fill.Solid
' Turn option 'Show background colors and images in Display view' on
    ActiveWindow.View.DisplayBackgrounds = True
' Change all text font to black colour
    ActiveDocument.Content.Font.Color = wdColorBlack
' Change all text and change "fill shading" background to no colour https://www.msofficeforums.com/word-vba/51216-change-fill-text-colour-colour.html
    With ActiveDocument.Content.ParagraphFormat
        .Shading.Texture = wdTextureNone
        .Shading.ForegroundPatternColor = wdColorAutomatic
        .Shading.BackgroundPatternColor = wdColorAutomatic
    End With
' Change all paragraphs with fill to no fill (from https://www.msofficeforums.com/word-vba/51216-change-fill-text-colour-colour.html)
Application.ScreenUpdating = False
'If the selection includes tables, error 4605 will be returned.
'Thus an error handler is needed:
On Error Resume Next
    For Each oPara In ActiveDocument.Range.Paragraphs
        oPara.Range.Select
        Selection.End = Selection.End - 1
        Selection.Shading.BackgroundPatternColor = wdColorAutomatic
    Next oPara
Application.ScreenUpdating = True
    Selection.HomeKey Unit:=wdStory
End Sub
Reply With Quote
  #15  
Old 08-21-2023, 11:08 AM
vivka vivka is offline Change "fill" text colour to no colour Windows 7 64bit Change "fill" text colour to no colour Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Darkmaster006, I'm glad that you are glad! As for the page color that is easy for eyes, I prefer sky blue. And of course, thank you for your sharing the full code!
Reply With Quote
Reply

Tags
fill, shading



Similar Threads
Thread Thread Starter Forum Replies Last Post
Change Text Colour/Box Shade if CheckBox is marked cavals07 Word VBA 7 01-30-2023 11:05 AM
Change "fill" text colour to no colour New table line has some cells fill colour change trevorc Excel 2 06-30-2021 12:27 PM
Text box fill colour changes when pasted to new file ParishPete Publisher 0 06-21-2019 12:47 AM
Change text colour for content control labels? Toe Word 1 01-17-2019 08:45 AM
How to globally change default "pattern fill" color for all shapes in PP 2013 tbach PowerPoint 1 01-31-2014 10:59 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:43 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft