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

  • Question

  • Hi folks,

    now it is that formatting,

    that should be done programmatically.
    Have been trying around a lot with Document.Background.Fill.Forecolor.RGB /.Forecolor.ObjectThemeColor / .Forecolor.TintAndShade but nothing happens.

    Document.Background.Fill.Forecolor.RGB (on its own) works if I set Document.ActiveWindow.View.Type to WebView but after changing back to PrintView its gone.

    If I debug my code before and after a manual change, I find those changes within the Document.Background.Forecolor object:

    Unluckily I cannot acess the Type field. Of course I want an arbitrary color, not a predefined scheme color.

    Cindy, I hope, it is not as trivial as the last one.

    Cheers,
    MH_dhs

    • Edited by MH_dhs Thursday, February 9, 2012 1:09 PM
    Tuesday, February 7, 2012 2:30 PM

Answers

  • Hi MH_dhs

    <<Please let me know if you could re-enact the whole behaviour (with closing/reopening word). >>

    Yes, I can close Word, start it again, then run the macro. Note, please, that I'm not using a recorded macro (that was removed when I quit Word the other day as I didn't save changes to Normal.dotm), but a test macro

    Sub NoColor()
        ActiveDocument.Background.Fill.ForeColor.RGB = RGB(100, 0, 0)
        ActiveDocument.Background.Fill.Visible = msoTrue
    End Sub

    What's not certain is whether a background shape may be in my Normal.dotm. I don't think so, as I never use Backgrounds and rarely open Normal.dotm.

    Is your default template a Normal.dot or a Normal.dotm? Was it brought forward from an older version of Word? You might try renaming all normal templates on your machine, start Word, then see if the code can work. It's possible there's a problem with the template from which the documents are generated.


    Cindy Meister, VSTO/Word MVP

    Friday, February 10, 2012 8:32 AM
    Moderator

All replies

  • Hi MH

    I never use this, so I resorted to the macro recorder, which delivered:

    Sub ChangePageColor()
        ActiveDocument.Background.Fill.ForeColor.ObjectThemeColor = _
            wdThemeColorText2
        ActiveDocument.Background.Fill.ForeColor.TintAndShade = 0.4
        ActiveDocument.Background.Fill.Visible = msoTrue
        ActiveDocument.Background.Fill.Solid
    End Sub
    Have you tried the Visible property?

    Cindy Meister, VSTO/Word MVP

    Tuesday, February 7, 2012 6:27 PM
    Moderator
  • Hi Cindy,

    ok ok, I should have tried the macro recorder too. It didnt record anything in the last case with the shapes, thats why I missed it this time.

    I recorded the manual change to an arbitrary color on my machine which resulted in even shorter code:

    ActiveDocument.Background.Fill.Visible = msoTrue
    ActiveDocument.Background.Fill.ForeColor.RGB = RGB(138, 202, 94)
    ActiveDocument.Background.Fill.Solid

    I knew the Visible-Property before but the Solid-Method was new for me.

    If I execute the macro after closing and re-opening Word, the macro does not work (and neither works the code in my AddIn).
    If I change the color manually first (to a different color), then the macro works (and so does my AddIn). There might be one initial setting, that we missed so far (and that is not recorded).

    BTW: I use Office 2007 and VS 2010 (which doesnt matter for the macro). Same effect in Office 2010.
    I just found an Office 2003 and there the macro works spontaneously.

    Cheers,
    MH_dhs

    • Edited by MH_dhs Thursday, February 9, 2012 1:10 PM
    Wednesday, February 8, 2012 9:19 AM
  • Hi MH_dhs

    It works fine for me:

    ActiveDocument.Background.Fill.ForeColor.RGB = rgb(100, 0,0)
    ActiveDocument.Background.Fill.Visible = msoTrue


    Cindy Meister, VSTO/Word MVP

    Thursday, February 9, 2012 4:51 PM
    Moderator
  • Hi Cindy,

    I checked the symptoms on 4 different machines now, both Office 2010 an 2007, German and English version.

    They all show the same behaviour:
    -record the macro
    -close Word and reopen (Word shows a blank document)
    -execute the macro
    -nothing happens
    -perform a manual change of the page color
    -execute the macro
    -the macro works

    I observed, that if I load a coloured document, the macro works spontaneously as well. If the loaded document is blank, the bahavoiur is as described above. So there must be another setting to the Document-object, that causes the macro to skip working.

    Please let me know if you could re-enact the whole behaviour (with closing/reopening word).

    Cheers,
    MH_dhs

    PS: I had a look into the DOCX-file of the coloured document. Besides some tags (concerning the same content as the macro) in the document.xml I found this

    in the settings.xml.
    I am sure, I could acess that via DocumentFormat.OpenXml.Wordprocessing but currently I have the document as a Microsoft.Office.Interop.Word-Object. Can you find out a way to set this property as Word does and as it can be done manually?

    • Edited by MH_dhs Friday, February 10, 2012 8:16 AM
    Friday, February 10, 2012 8:14 AM
  • Hi MH_dhs

    <<Please let me know if you could re-enact the whole behaviour (with closing/reopening word). >>

    Yes, I can close Word, start it again, then run the macro. Note, please, that I'm not using a recorded macro (that was removed when I quit Word the other day as I didn't save changes to Normal.dotm), but a test macro

    Sub NoColor()
        ActiveDocument.Background.Fill.ForeColor.RGB = RGB(100, 0, 0)
        ActiveDocument.Background.Fill.Visible = msoTrue
    End Sub

    What's not certain is whether a background shape may be in my Normal.dotm. I don't think so, as I never use Backgrounds and rarely open Normal.dotm.

    Is your default template a Normal.dot or a Normal.dotm? Was it brought forward from an older version of Word? You might try renaming all normal templates on your machine, start Word, then see if the code can work. It's possible there's a problem with the template from which the documents are generated.


    Cindy Meister, VSTO/Word MVP

    Friday, February 10, 2012 8:32 AM
    Moderator