Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2005

Tip: Looking for answers? Try searching our database.

Macro to remove a watermark

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jcericola - 02 Mar 2005 17:29 GMT
I wrote a simple macro to add a watermark to my document.  Now I'm trying to
write one to delete the watermark.  The code works on the first Watermark I
insert and delete, but subsequent ones give me an error "The item with the
specified name was not found". I beleive the problem is that word wants to
reference the object by name, but if the name of the item is different
everytime you reinsert a new watermark.  Please help me find a way to delete
my watermark.  Here is my code.  It is the second macro that fails.

Sub InsertWatermark_Draft()

   If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
       ActiveWindow.Panes(2).Close
   End If
   If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
       ActivePane.View.Type = wdOutlineView Then
       ActiveWindow.ActivePane.View.Type = wdPrintView
   End If
   ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
   Application.DisplayAutoCompleteTips = True
   NormalTemplate.AutoTextEntries("Watermark_Draft").Insert
Where:=Selection. _
       Range, RichText:=True
   ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

End Sub

Sub RemoveWatermark_Draft()

   If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
       ActiveWindow.Panes(2).Close
   End If
   If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
       ActivePane.View.Type = wdOutlineView Then
       ActiveWindow.ActivePane.View.Type = wdPrintView
   End If
   ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
   Selection.HeaderFooter.Shapes("Text Box 2").Select
   Selection.ShapeRange.Delete
   Selection.Delete Unit:=wdCharacter, Count:=1

End Sub
Greg - 02 Mar 2005 20:50 GMT
I am just taking a stab at this.  I think you could do this if you put
your watermark in an AutoText field.  Then when you want to delete you
could then simply delete the AutoText fields found in the defined
range.

Sub InsertWatermark_Draft()

If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
 ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
 ActivePane.View.Type = wdOutlineView Then
 ActiveWindow.ActivePane.View.Type = wdPrintView
End If
 ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
 Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldAutoText,
Text:= _
 "Water_Mark", PreserveFormatting:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

End Sub
Sub RemoveWatermark_Draft()

Dim oFld As Field
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
  ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
  ActivePane.View.Type = wdOutlineView Then
  ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
For Each oFld In Selection.Range.Fields
 If oFld.Type = wdFieldAutoText Then
   oFld.Delete
 End If
Next
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

If anyone can offer a better way to set the range to the wholestory
range I would appreciate feedback.
Jcericola - 03 Mar 2005 00:11 GMT
this code is not working for me.  as soon as I copy it into word the line
"Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldAutoText,
Text:= _
 "Water_Mark", PreserveFormatting:=True" goes red and the debug says I have
a syntax error.  Please help.

> I am just taking a stab at this.  I think you could do this if you put
> your watermark in an AutoText field.  Then when you want to delete you
[quoted text clipped - 39 lines]
> If anyone can offer a better way to set the range to the wholestory
> range I would appreciate feedback.
Jezebel - 02 Mar 2005 22:21 GMT
You'll make your life much easier if you don't do all that mucking around
with windows, panes, and views -- just work directly with the objects
themselves. To insert the watermark:

Sub InsertWatermark_Draft()
   NormalTemplate.AutoTextEntries("Watermark_Draft").Insert _
           Where:=ActiveDocument.StoryRanges(wdPrimaryHeaderStory), _
           RichText:=True
End Sub

To delete it, assuming you don't have other shapes in your header --

Sub RemoveWatermark_Draft()

   With ActiveDocument.StoryRanges(wdPrimaryHeaderStory)
       Do Until .Shapes.Count = 0
           .Shapes(1).Delete
       Loop
   End with

End sub

>I wrote a simple macro to add a watermark to my document.  Now I'm trying
>to
[quoted text clipped - 40 lines]
>
> End Sub
Jcericola - 03 Mar 2005 00:05 GMT
What if I do have other objects in my header?

> You'll make your life much easier if you don't do all that mucking around
> with windows, panes, and views -- just work directly with the objects
[quoted text clipped - 62 lines]
> >
> > End Sub
Jezebel - 03 Mar 2005 00:22 GMT
Then you'll need to examine the shapes individually to determine which one
you want to delete. Insert the watermark and check the properties -- there
will be some that are sufficiently distinctive, such as type or position.
But you can't use the Name (which is what your original code was using)
because that's not reliable: it depends on the sequence of events in
creating the document.

maybe something like

Dim pShape as Word.Shape

   For each pShape in
ActiveDocument.StoryRanges(wdPrimaryHeaderStory).Shapes
       if pShape.top = 123 and pShape.Left = 456 then
           pShape.Delete
           Exit for
       end if
   Next

> What if I do have other objects in my header?
>
[quoted text clipped - 70 lines]
>> >
>> > End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.