Hello guys,
I am trying to insert watermark as text in word 2007 using c# but getting error can u pls help me out.
here is my code...

//THE LOGO IS ASSIGNED TO A SHAPE OBJECT SO THAT WE CAN USE ALL THE
//SHAPE FORMATTING OPTIONS PRESENT FOR THE SHAPE OBJECT
Word.Shape logoWatermark = null;

//INCLUDING THE TEXT WATER MARK TO THE DOCUMENT
logoWatermark = oWord.Selection.HeaderFooter.Shapes.AddTextEffect(
    Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,
    "Enter The Text Here", "Arial", (float)60,
    Microsoft.Office.Core.MsoTriState.msoTrue,
    Microsoft.Office.Core.MsoTriState.msoFalse,
    0, 0, ref oMissing); 
logoWatermark.Select(ref oMissing);
logoWatermark.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
logoWatermark.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
logoWatermark.Fill.Solid();
logoWatermark.Fill.ForeColor.RGB = (Int32)Word.WdColor.wdColorGray30;
logoWatermark.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
logoWatermark.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
logoWatermark.Left = (float)Word.WdShapePosition.wdShapeCenter;
logoWatermark.Top = (float)Word.WdShapePosition.wdShapeCenter;
logoWatermark.Height = oWord.InchesToPoints(2.4f);
logoWatermark.Width = oWord.InchesToPoints(6f);

and error i am getting is ....

nullreferenceexception was unhandled.
Object reference not set to an instance of an object.

logoWatermark = oWord.Selection.HeaderFooter.Shapes.AddTextEffect(
                    Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,
                    "Enter The Text Here", "Arial", (float)60,
                    Microsoft.Office.Core.MsoTriState.msoTrue,
                    Microsoft.Office.Core.MsoTriState.msoFalse,
                    0, 0, ref oMissing);

In this line i am getting error.
Thanks in advance

where do you assign a value to oWord?

At the namespace
like

using Word = Microsoft.Office.Interop.Word;
using System.Reflection;


And at button click()

                object oMissing = System.Reflection.Missing.Value;
                object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

                //Start Word and create a new document.
                Word._Application oWord;
                Word._Document oDoc;
                oWord = new Word.Application();
                oWord.Visible = true;
                oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing);

Can anybody help me. Here is my whole code...

using Word = Microsoft.Office.Interop.Word;
using System.Reflection;

      private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                object oMissing = System.Reflection.Missing.Value;
                object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

                //Start Word and create a new document.
                Word._Application oWord;
                Word._Document oDoc;
                oWord = new Word.Application();
                oWord.Visible = true;
                oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing);

                //Insert a paragraph at the beginning of the document.
                Word.Paragraph oPara1;
                oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
                //Add Text
                oPara1.Range.Text = "Hello, Bhagawat";
                //Add Font styles and other features           
                oPara1.Range.Font.Name = "Times New Roman";
                oPara1.Range.Font.Size = 20;
                oPara1.Range.Font.Bold = 1;

                //THE LOGO IS ASSIGNED TO A SHAPE OBJECT SO THAT WE CAN USE ALL THE
                //SHAPE FORMATTING OPTIONS PRESENT FOR THE SHAPE OBJECT
                Word.Shape logoWatermark = null;

                //INCLUDING THE TEXT WATER MARK TO THE DOCUMENT
                logoWatermark = oWord.Selection.HeaderFooter.Shapes.AddTextEffect(
                    Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,
                    "DEMO", "Arial", (float)60, Microsoft.Office.Core.MsoTriState.msoTrue
                   , Microsoft.Office.Core.MsoTriState.msoFalse,0, 0, ref oMissing);
                logoWatermark.Select(ref oMissing);
                logoWatermark.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                logoWatermark.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
                logoWatermark.Fill.Solid();
                logoWatermark.Fill.ForeColor.RGB = (Int32)Word.WdColor.wdColorGray30;
                logoWatermark.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
                logoWatermark.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
                logoWatermark.Left = (float)Word.WdShapePosition.wdShapeCenter;
                logoWatermark.Top = (float)Word.WdShapePosition.wdShapeCenter;
                logoWatermark.Height = oWord.InchesToPoints(2.4f);
                logoWatermark.Width = oWord.InchesToPoints(6f);

                //################ Ending Part of the page Goes here ###############
                //24 pt spacing after paragraph.
                // oPara1.Format.SpaceAfter = 24;   
                oPara1.Range.InsertParagraphAfter();
                //##################################################################
                //Close this form.
                //this.Close();
            }
            catch (Exception ex)
           {
                MessageBox.Show(ex.Message);
           }
        }

If you are getting a null reference exception then one of the following must be null. Put a breakpoint on the line giving the exception and type these into the watch window and see which is null

oWord
oWord.Selection
oWord.Selection.HeaderFooter
oWord.Selection.HeaderFooter.Shapes

Hi Momerath,
Here oWord.Selection.HeaderFooter, i am getting null value..

Hi geric823, actually i am used the word API not the third party tool as you suggest via Spire.Doc

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.