Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I am trying to change the background color of a page in word document. But i am getting error. This is my code.

VB
Try
            oWord = GetObject(, "Word.Application")
            oDoc = oWord.ActiveDocument
            Dim ttl As String = oDoc.Name
            Label1.Text = ttl
            Dim clr As Color = Color.FromArgb(128, 128, 0)
            Try
                oDoc.Background.Fill.BackColor.RGB = ColorTranslator.ToOle(clr)
                oDoc.Background.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoCTrue
                oDoc.Background.Fill.Solid()

            Catch ex As Exception
                MsgBox("Second TryCatch - " & ex.Message)
            End Try
        Catch ex As Exception
            Label1.Text = "Object Error"
        End Try


And the message says that "The specified value is out of range"
Posted

1 solution

I have no idea about ColorTranslator.
On the other side msoCTrue is not supported. Please see: MsoTriState enumeration (Microsoft.Office.Core)[^]

This should works:
VB.NET
Dim oDoc As Document = Nothing
'later
oDoc = ActiveDocument

    With oDoc
        .Background.Fill.ForeColor.RGB = RGB(128, 128, 0)
        .Background.Fill.Visible = msoTrue
        .Background.Fill.Solid
    End With
    
oDoc = Nothing
 
Share this answer
 
Comments
Vinod Kc 18-Jan-16 1:21am    
Thank you @Maciej Los :)
Maciej Los 18-Jan-16 2:53am    
You're very welcome.
Cheers,
Maciej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900