Welcome to MSDN Blogs Sign in | Join | Help

Create Watermarks using VSTO 2005 (Part 2 C#)

In my blog yesterday I showed How to create a Word Watermark using VSTO 2005. This was relatively easy to do using VB .Net. Today I will show you the same code using VSTO 2005 and C# (for those of you who like abuse). I have tried to make the code exactly the same as the VB code so you should be able to compare line for line with yesterday’s post. OK so converting to C# was not all that hard once you learn the tricks. In the VB code I was able to take the VBA almost as is and tweak it to make it work. In C# there is more fix up work to be done. One thing that we as C# developers know is that we need to supply the optional parameters (See Eric Carters excellent post on this), and boy did the designers of the Word OM go crazy with that feature. In this code thankfully there were only 2; 1 in the AddTextEffect and the 2nd wmShape.Select. The next thing is that there are many properties that require a true/false value (I did not say Boolean). Many of the Office properties take an enumeration type called MsoTriState, kind of like a super-boolean, a yes/no/maybe field. VB lets me get away with passing a Boolean True or False. C# does not. In C# I must pass the correct type,

MsoTriState.msoFalse or MsoTriState.msoTrue. So that was not so bad but there are some properties that are looking for a true/false and are not MsoTriState but are integers; as is the case for the AllowOverlap property. Again, VB lets me get away with passing a Boolean True or False. In C# I must pass -1 for True and 0 for false. So there is one last thing that is just a bug in the VBA code. I am not sure how often it happens but it is something to be aware of when you are converting VBA macro code to C#. Sometimes the property takes an enumeration but the VBA macro recorder spits an integer for the value. WrapFormat.Type does this. In the recorded VBA code WrapFormat.Type = 3. Using this line as is in VB .Net works fine. But, C# requires us to pass the correct type which is an enumeration called WdWrapType. The problem with this is how do I know which enumerated value equals 3? The easy way to figure this out is to fire up Word and press Alt-F11 to open the VBA editor. In the immediate window print out the values by using “? WdWrapType.WdWrapNone” (you will get intellisense help). So after some trial and error I see the WdWrapNone = 3. That’s about it. So the major changes are

  • Pass optional parameters
  • Pass MsoTriState for true/false
  • Cast all of the types(like float and int)
  • Fix any VBA bugs by passing the enumeration

 

So here is the add watermark code in C#. You should be able to apply these basic conversion rules to any VBA code that you want to convert to C#.

 

                        private void AddWatermark(string WatermarkText)

                        {

                                    Word.Selection Selection = ThisApplication.Selection;

                                    Word.Shape wmShape;

                                    //Select the section

                                    this.Sections[1].Range.Select();

                                    ActiveWindow.ActivePane.View.SeekView =

                                                Word.WdSeekView.wdSeekCurrentPageHeader;

                                    //Create the watermar shape

                                    wmShape = Selection.HeaderFooter.Shapes.AddTextEffect(

                                                Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,

                                                WatermarkText, "Times New Roman", 1,

                                                Microsoft.Office.Core.MsoTriState.msoFalse,

                                                Microsoft.Office.Core.MsoTriState.msoFalse,

                                                0, 0, ref missing);

                                    //Set all of the attributes of the watermark

                                    wmShape.Select(ref missing);

                                    wmShape.Name = "PowerPlusWaterMarkObject1";

                                    wmShape.TextEffect.NormalizedHeight = Microsoft.Office.Core.MsoTriState.msoFalse;

                                    wmShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;

                                    wmShape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;

                                    wmShape.Fill.Solid();

                                    wmShape.Fill.ForeColor.RGB = (int)Word.WdColor.wdColorGray25;

                                    wmShape.Fill.Transparency = 0.5f;

                                    wmShape.Rotation = 315.0f;

                                    wmShape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;

                                    wmShape.Height = ThisApplication.InchesToPoints(2.82f);

                                    wmShape.Width = ThisApplication.InchesToPoints(5.64f);

                                    wmShape.WrapFormat.AllowOverlap = -1; //true

                                    wmShape.WrapFormat.Side = Word.WdWrapSideType.wdWrapBoth;

                                    wmShape.WrapFormat.Type = Word.WdWrapType.wdWrapNone;  //3

                                    wmShape.RelativeHorizontalPosition =

                                                Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;

                                    wmShape.RelativeVerticalPosition =

                                                Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;

                                    wmShape.Left = (float)Word.WdShapePosition.wdShapeCenter;

                                    wmShape.Top = (float)Word.WdShapePosition.wdShapeCenter;

 

                                    //set focus back to document

                                    ActiveWindow.ActivePane.View.SeekView =

                                                Word.WdSeekView.wdSeekMainDocument;

                        }

 

                        private void DeleteWatermark()

                        {

                                    Word.Selection Selection = ThisApplication.Selection;

                                    //Select the section

                                    this.Sections[1].Range.Select();

                                    ActiveWindow.ActivePane.View.SeekView =

                                                Word.WdSeekView.wdSeekCurrentPageHeader;

                                    object shapeName = "PowerPlusWaterMarkObject1";

                                    Selection.HeaderFooter.Shapes.get_Item(ref shapeName).Delete();

                                    //set focus back to document

                                    ActiveWindow.ActivePane.View.SeekView =

                                                Word.WdSeekView.wdSeekMainDocument;

                        }

 

                        private void AddWM_Click(object sender, EventArgs e)

                        {

                                    try

                                    {

                                                AddWatermark("Hello World");

                                    }

                                    catch(Exception ex)

                                    {

                                                MessageBox.Show(ex.Message);

                                    }

                        }

 

                        private void DeleteWm_Click(object sender, EventArgs e)

                        {

                                    try

                                    {

                                                DeleteWatermark();

                                    }

                                    catch (Exception ex)

                                    {

                                                MessageBox.Show(ex.Message);

                                    }

                        }

 

Published Tuesday, March 08, 2005 9:53 AM by pstubbs
Filed under: ,

Comments

# re: Create Watermarks using VSTO 2005 (Part 2 C#)

Hi, very interesting article. I've tried out this code and run into a couple of problems. The first issue is that I got an exception when trying to do the following:

doc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;

The exception said that this was only allowed in the Print Layout View. So, I added the line:

doc.Application.ActiveWindow.View.Type = Word.WdViewType.wdPrintView;

to select the Print LayoutView. Then, i got another exception when trying to execute:

wmShape.WrapFormat.AllowOverlap = -1;

this excpetion indicated that the command was not available for the current selection?????
Monday, March 20, 2006 1:17 PM by S Shakespeare

# re: Create Watermarks using VSTO 2005 (Part 2 C#)

VB.Net with Option Strict On requires the same work as mentioned above for C#.

And I would suggest that instead of printing all values in the VBA immediate window it's easier to switch to the object catalog (F2) and look up the enum in the class list. An even quicker way to get there is Shift-F2 on the enum value in question in the code pane.
Monday, April 24, 2006 6:53 AM by Tom

# re: Create Watermarks using VSTO 2005 (Part 2 C#)

Image Resizer Pro 2006 is a powerful and easy-to-use utility,  to rapidly resize, convert, and rotate a large batch of images at a time, or even add custom watermarks on them.

http://www.yaodownload.com/video-design/imageeditors/image-resizer_imageeditors.htm
Wednesday, April 26, 2006 9:32 PM by mike

# Combining Information Rights Management with Watermarking via an Outlook 2007 add-in

VSTO 3.0 with Visual Studio 2008 makes it incredibly easy to extend the office ribbon, and write managed

Tuesday, April 29, 2008 6:34 AM by Ronan Geraghty's Blog

# Combining Information Rights Management with Watermarking via an Outlook 2007 add-in

VSTO 3.0 with Visual Studio 2008 makes it incredibly easy to extend the office ribbon, and write managed

Tuesday, April 29, 2008 6:34 AM by Microsoft Ireland Blog
Anonymous comments are disabled
 
Page view tracker