none
Document.Background - change color (programmatically) - part 2 RRS feed

  • Question

  • Hi, I have the same problem as MH_dhs did in this thread, but I ask a little differently:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/69b43013-5319-4f97-aeda-032b00143e13/documentbackground-change-color-programmatically?forum=vsto

    If I change the background color of a document MANUALLY then the color of the background changes as expected - in ALL views, including print layout.

    If I record a macro of the manual procedure and use it after the document has first been changed once manually, then it works the same way - all views.

    If I close word then create a new document and run the macro (without touching the manual control first!!) the properties change as from the recorded macro, but the color of the background only updates in certain views (like reading and web) but not all of them (print).

    There is clearly something happening when using the manual button that is not recorded into the macro. Just like MH_dhs said. He hinted a part of the xml must be set (see the referenced thread). Does anyone know if this can be set in VBA code, and if so how?

    Or is there another way I can set the background color for all views programmatically?

    The recorded macro looks like this:

        ActiveDocument.Background.Fill.ForeColor.ObjectThemeColor = _
            wdThemeColorText2
        ActiveDocument.Background.Fill.ForeColor.TintAndShade = -0.5
        ActiveDocument.Background.Fill.Visible = msoTrue
        ActiveDocument.Background.Fill.Solid

    BR Jan

    Sunday, July 3, 2016 10:05 AM

Answers

  • Hi allnamesarealreadyinuse,

    you can use the code below to change the background color.

    Sub demo2()
    
        ActiveDocument.Background.Fill.Visible = msoTrue
        ActiveDocument.Background.Fill.ForeColor.RGB = RGB(80, 50, 200)
        ActiveDocument.Background.Fill.Solid
      
     End Sub

    or you can also try to add the line of code in your above mentioned code.

    add the below mentioned line as first line in your code will also work.

    ActiveDocument.ActiveWindow.View.DisplayBackgrounds = True

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 4, 2016 2:40 AM
    Moderator

All replies

  • Hi allnamesarealreadyinuse,

    you can use the code below to change the background color.

    Sub demo2()
    
        ActiveDocument.Background.Fill.Visible = msoTrue
        ActiveDocument.Background.Fill.ForeColor.RGB = RGB(80, 50, 200)
        ActiveDocument.Background.Fill.Solid
      
     End Sub

    or you can also try to add the line of code in your above mentioned code.

    add the below mentioned line as first line in your code will also work.

    ActiveDocument.ActiveWindow.View.DisplayBackgrounds = True

    Regards

    Deepak


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 4, 2016 2:40 AM
    Moderator
  • I am "self trained" in writing macros and most of my macros come from searching the web and trial and error.  This question is for my own edification.

    I had the same code as Jan, but my issue is the color changed going from MSW2010 to MSW2013 so now I get a gray blue rather than the nicer blue that I had (Dark Blue, text 2, lighter 50%).  So, I am trying to rework my macro to get it back to that original color.

    My original macro code from MSword 2010

    ActiveDocument.Background.Fill.ForeColor.ObjectThemeColor = _wdThemeColorText2   

    ActiveDocument.Background.Fill.ForeColor.TintAndShade = 0.5   

    ActiveDocument.Background.Fill.Visible = msoTrue   

    ActiveDocument.Background.Fill.Solid   

    ActiveDocument.ActiveWindow.View.DisplayBackgrounds = True


    Why do we need to change the coding to an RGB type? Is it because RGB is constant while MSWord color codes/styles change? Are there other ways to get the same effect (using the HEX-6 digit color number, HSL or CMYK)?  If so, how do those get written?

    I tried the new code and it worked fine (except for finding the right RGB color that I want).  I am just curious why we need to change it.

    Thanks in advance for clarifying.

    Patricia

    Saturday, November 12, 2016 2:10 PM