Programmatically creating watermarks in Word can be a challenge. Here I have created the code to add and remove watermarks in Word using VSTO 2005. The way I created this code is the way the many VBA programmers work. They record a macro doing the action that they want to automate. Then they tweak the recorded VBA to suit their requirements. In this case creating the watermark was the same process, except that I needed to translate some of the VBA goo into the Objects that VSTO expects. One of the challenges was that VBA isn’t strongly typed so I needed to find the strongly typed object that VBA was using. Once you do this a couple of times it you get the hang of it and it becomes easy. So here is the VB .Net code to create a watermark using Word and VSTO. You can call this from anywhere in your code, for this example I added a couple of winform buttons on the document to fire the add and delete methods.

See Also Create Watermarks using VSTO 2005 (Part 2 C#)

    Private Sub AddWatermark(ByVal WatermarkText As String)

        Dim Selection As Word.Selection = ThisApplication.Selection

        Dim wmShape As Word.Shape

        'Select the section

        Me.Sections(1).Range.Select()

        ActiveWindow.ActivePane.View.SeekView = _

            Word.WdSeekView.wdSeekCurrentPageHeader

        'Create the watermark shape

        wmShape = Selection.HeaderFooter.Shapes.AddTextEffect( _

        Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, _

            WatermarkText, "Times New Roman", 1, False, False, 0, 0)

        'Set all of the attributes of the watermark

        With wmShape

            .Select()

            .Name = "PowerPlusWaterMarkObject1"

            .TextEffect.NormalizedHeight = False

            .Line.Visible = False

            .Fill.Visible = True

            .Fill.Solid()

            .Fill.ForeColor.RGB = Word.WdColor.wdColorGray25

            .Fill.Transparency = 0.5

            .Rotation = 315

            .LockAspectRatio = True

            .Height = ThisApplication.InchesToPoints(2.82)

            .Width = ThisApplication.InchesToPoints(5.64)

            .WrapFormat.AllowOverlap = True

            .WrapFormat.Side = Word.WdWrapSideType.wdWrapBoth

            .WrapFormat.Type = 3

            .RelativeHorizontalPosition = _

                Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin

            .RelativeVerticalPosition = _

                Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin

            .Left = Word.WdShapePosition.wdShapeCenter

            .Top = Word.WdShapePosition.wdShapeCenter

        End With

        'set focus back to document

        ActiveWindow.ActivePane.View.SeekView = _

            Word.WdSeekView.wdSeekMainDocument

    End Sub

 

    Private Sub DeleteWatermark()

        Dim Selection As Word.Selection = ThisApplication.Selection

        'Select the section

        Me.Sections(1).Range.Select()

        ActiveWindow.ActivePane.View.SeekView = _

            Word.WdSeekView.wdSeekCurrentPageHeader

        Selection.HeaderFooter.Shapes("PowerPlusWaterMarkObject1").Delete()

        'set focus back to document

        ActiveWindow.ActivePane.View.SeekView = _

            Word.WdSeekView.wdSeekMainDocument

    End Sub

 

 

    Private Sub AddWM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddWM.Click

        Try

            AddWatermark("Hello World")

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

 

    Private Sub DeleteWM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteWM.Click

        Try

            DeleteWatermark()

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub