Create Watermarks using VSTO 2005


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

Comments (4)

  1. William Bich says:

    Was trying to remove a watermark using late binding (createobject) in vb.net.  This code actually worked out.  Even though it was not in the header. It pointed me in the right direction.

  2. janakav says:

    Hi Paul,

    If I type a name (eg. my name) word doesn’t recognize it as a correct word since my name is not included in the word dictionary. But still I could right click on it and select ‘Add to dictionary’. My question is how can I automate this?

    Thanks in advance,

    Janaka

  3. Darshak Sheth says:

    Hi,

    You can use this to add water mark to excel

    Microsoft.Office.Interop.Excel.Shape sp = ws.Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, "WatermarkText", "Times New Roman", 1, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0);

    sp.Select("A:A");

    sp.Name = "WaterMarkObject1";

    sp.TextEffect.NormalizedHeight = MsoTriState.msoFalse;

    sp.Line.Visible = MsoTriState.msoTrue;

    sp.Line.DashStyle = MsoLineDashStyle.msoLineRoundDot;

    sp.Fill.Visible = MsoTriState.msoTrue;

    sp.Fill.OneColorGradient(MsoGradientStyle.msoGradientHorizontal, 3, 0.6f);

    sp.Fill.TwoColorGradient(MsoGradientStyle.msoGradientHorizontal, 1);

    sp.Fill.Transparency = 0.9f;

    sp.Rotation = 315;

    sp.LockAspectRatio = MsoTriState.msoTrue;

    sp.Height = (float)objApp.InchesToPoints(2.82);

    sp.Width = (float)objApp.InchesToPoints(5.64f);

    sp.ZOrder(MsoZOrderCmd.msoSendToBack);

    You can also use this (not 100% sure)

    ws.Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, "Water Mark Text", "Arial", 15.5f, MsoTriState.msoTrue, MsoTriState.msoTrue, 300.5f, 100.0f);

  4. Darshak Sheth says:

    Hi,

    You can use this to add water mark to excel

    where "ws" is WorkSheet object

    Microsoft.Office.Interop.Excel.Shape sp = ws.Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, "WatermarkText", "Times New Roman", 1, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0);

    sp.Select("A:A");

    sp.Name = "WaterMarkObject1";

    sp.TextEffect.NormalizedHeight = MsoTriState.msoFalse;

    sp.Line.Visible = MsoTriState.msoTrue;

    sp.Line.DashStyle = MsoLineDashStyle.msoLineRoundDot;

    sp.Fill.Visible = MsoTriState.msoTrue;

    sp.Fill.OneColorGradient(MsoGradientStyle.msoGradientHorizontal, 3, 0.6f);

    sp.Fill.TwoColorGradient(MsoGradientStyle.msoGradientHorizontal, 1);

    sp.Fill.Transparency = 0.9f;

    sp.Rotation = 315;

    sp.LockAspectRatio = MsoTriState.msoTrue;

    sp.Height = (float)objApp.InchesToPoints(2.82);

    sp.Width = (float)objApp.InchesToPoints(5.64f);

    sp.ZOrder(MsoZOrderCmd.msoSendToBack);

    You can also use this (not 100% sure)

    ws.Shapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect1, "Water Mark Text", "Arial", 15.5f, MsoTriState.msoTrue, MsoTriState.msoTrue, 300.5f, 100.0f);