Visual Studio Development Bookmark and Share   
 index > Visual Studio Tools for Office > Add a textbox to header of a word document using vsto
 

Add a textbox to header of a word document using vsto

Hi,

I have a vsto word 2007 addin project. In this I have added a Ribbon control.

I need to add a watermark on click of a button (which is there on the ribbon control).

To achieve this I have written the following code which throws error (This code is written in the code behind of the ribbon control).

Private Sub InsertWatermark_Click(ByVal sender As Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles InsertWatermark.Click
AddWatermark()
End Sub

Private Sub AddWatermark()

Dim Selection As Word.Selection = _currentApplication.Selection
Dim wmShape As Word.Shape

'Select the section
_currentDocument.Sections(1).Range.Select()
_currentDocument.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView

_currentDocument.ActiveWindow.ActivePane.View.SeekView = _
WdSeekView.wdSeekCurrentPageHeader
'Create the watermark shape

wmShape = Selection.HeaderFooter.Shapes.AddTextEffect( _
Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, _
"Water mark text", "Times New Roman", 1, MsoTriState.msoFalse, MsoTriState.msoFalse, 0, 0)

'Set all of the attributes of the watermark

With wmShape

.Select()
.Name = "PowerPlusWaterMarkObject1"
.TextEffect.NormalizedHeight = MsoTriState.msoFalse
.Line.Visible = MsoTriState.msoFalse
.Fill.Visible = MsoTriState.msoCTrue
.Fill.Solid()
.Fill.ForeColor.RGB = Word.WdColor.wdColorGray25
.Fill.Transparency = 0.5
.Rotation = 315
.LockAspectRatio = MsoTriState.msoCTrue
.Height = InchesToPoints(2.82)
.Width = InchesToPoints(5.64)
.WrapFormat.AllowOverlap = -1
.WrapFormat.Side = Word.WdWrapSideType.wdWrapBoth
'.WrapFormat.Type = 3
.RelativeHorizontalPosition = _
WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin

.RelativeVerticalPosition = _
WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin

.Left = Word.WdShapePosition.wdShapeCenter
.Top = Word.WdShapePosition.wdShapeCenter

End With

'set focus back to document
_currentDocument.ActiveWindow.ActivePane.View.SeekView = _
WdSeekView.wdSeekMainDocument

End Sub



This code throws error on wmShape.select(). The error code states "This member cannot be accessed in this view."

The same code is working in the vba.

Please help!!
Thanks in advance
Aniruddha Shastry  Wednesday, April 01, 2009 9:39 AM
Generally, it's a bad idea to use the Selection object at all, and working with SeekView to actually activate the header/footer of a document during automation is notoriously unreliable. Instead, it's better to work directly with the objects provided by the Word object model. (This is an area where the macro recorder is of only limited assistance to the developer.)

I suggest you try something more along these lines (untested):

Dim wmShape As Word.Shape

'Select the section
Dim sec as Word.Section = _currentDocument.Sections(1)
Dim hf as Word.HeaderFooter = sec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary)

wmShape = hf.Shapes.AddTextEffect( _
Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, _
"Water mark text", "Times New Roman", 1, MsoTriState.msoFalse, MsoTriState.msoFalse, 0, 0)

'Set all of the attributes of the watermark

With wmShape
.Name = "PowerPlusWaterMarkObject1"
.TextEffect.NormalizedHeight = MsoTriState.msoFalse
.Line.Visible = MsoTriState.msoFalse
.Fill.Visible = MsoTriState.msoCTrue
.Fill.Solid()
.Fill.ForeColor.RGB = Word.WdColor.wdColorGray25
.Fill.Transparency = 0.5
.Rotation = 315
.LockAspectRatio = MsoTriState.msoCTrue
.Height = InchesToPoints(2.82)
.Width = InchesToPoints(5.64)
.WrapFormat.AllowOverlap = -1
.WrapFormat.Side = Word.WdWrapSideType.wdWrapBoth
'.WrapFormat.Type = 3
.RelativeHorizontalPosition = _
WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin

.RelativeVerticalPosition = _
WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin

.Left = Word.WdShapePosition.wdShapeCenter
.Top = Word.WdShapePosition.wdShapeCenter

End With
Cindy Meister, VSTO/Word MVP
Cindy Meister  Thursday, April 02, 2009 3:15 PM
Generally, it's a bad idea to use the Selection object at all, and working with SeekView to actually activate the header/footer of a document during automation is notoriously unreliable. Instead, it's better to work directly with the objects provided by the Word object model. (This is an area where the macro recorder is of only limited assistance to the developer.)

I suggest you try something more along these lines (untested):

Dim wmShape As Word.Shape

'Select the section
Dim sec as Word.Section = _currentDocument.Sections(1)
Dim hf as Word.HeaderFooter = sec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary)

wmShape = hf.Shapes.AddTextEffect( _
Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, _
"Water mark text", "Times New Roman", 1, MsoTriState.msoFalse, MsoTriState.msoFalse, 0, 0)

'Set all of the attributes of the watermark

With wmShape
.Name = "PowerPlusWaterMarkObject1"
.TextEffect.NormalizedHeight = MsoTriState.msoFalse
.Line.Visible = MsoTriState.msoFalse
.Fill.Visible = MsoTriState.msoCTrue
.Fill.Solid()
.Fill.ForeColor.RGB = Word.WdColor.wdColorGray25
.Fill.Transparency = 0.5
.Rotation = 315
.LockAspectRatio = MsoTriState.msoCTrue
.Height = InchesToPoints(2.82)
.Width = InchesToPoints(5.64)
.WrapFormat.AllowOverlap = -1
.WrapFormat.Side = Word.WdWrapSideType.wdWrapBoth
'.WrapFormat.Type = 3
.RelativeHorizontalPosition = _
WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin

.RelativeVerticalPosition = _
WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin

.Left = Word.WdShapePosition.wdShapeCenter
.Top = Word.WdShapePosition.wdShapeCenter

End With
Cindy Meister, VSTO/Word MVP
Cindy Meister  Thursday, April 02, 2009 3:15 PM
Hi Cindy,

When I try to do this I get an error stating RunTime Error 424 - Object Required. What I am trying to do is open each document in a directory and iterate through each adding a Watermark and saving. Any thoughts?

Regards,

Alan
Alsabar  Friday, October 16, 2009 4:30 PM
Hi Alan

No idea without seeing the code and which line is generating the error.

From the sound of it, you have an object variable that hasn't been assigned to an object, but impossible to know what that might be without the code.
Cindy Meister, VSTO/Word MVP
Cindy Meister  Friday, October 16, 2009 4:55 PM

You can use google to search for other answers

Custom Search

More Threads

• IS there any efficient method to search in a large excel sheet
• VSTO v3 CTP and MS Access Ribbon UI Customization
• How to integrate infopath form with asp.net application
• Deplyoing Word Addin made using VSTO on the Server
• Office 2007 Add-in permission problem
• VSTO SE Walkthrough 2/2 Problem - missing elements in the prerequisites list
• Outlook 2003?
• Can't expand named range
• Outlook Addin changes Connection mode
• Trouble With AddOleControl