Help with deleting objects in VBA

K

Kerri

Hi,

I created a macro that will insert the words "Draft Copy"
into a document using WordArt. I named the object Draft1
so I could create a Delete macro that would remove
the "Draft Copy". This works great until the document is
saved, closed and then reopened. Then it can't find
the "Draft1" object in order to delete it. So how do i
make it remember the object name even after it has been
saved and closed?

I provided the code below if anyone has a suggestion.
Thank you, Kerri

Dim Draft1 As Shape
'----------------------
Sub DRAFTWaterMark()

ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader

Set Draft1 =
Selection.HeaderFooter.Shapes.AddTextEffect
(msoTextEffect8, _
"Draft Copy", "Times New Roman", 36#, msoFalse,
msoFalse, 226.3, 157.7)

Draft1.Select
With Selection.ShapeRange
..Fill.Visible = msoTrue
..Fill.Solid
.Fill.ForeColor.RGB = RGB(192, 192, 192)
.Fill.Transparency = 0#
.Line.Weight = 0.75
.Line.DashStyle = msoLineSolid
.Line.Style = msoLineSingle
.Line.Transparency = 0#
.LockAspectRatio = msoFalse
.Height = 81.35
..Width = 360#
.Rotation = 0#
.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = _
wdRelativeVerticalPositionPage
.Left = wdShapeCenter
.Top = wdShapeCenter
.LockAnchor = True
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapBoth
.WrapFormat.DistanceTop = InchesToPoints(0)
.WrapFormat.DistanceBottom = InchesToPoints(0)
.WrapFormat.DistanceLeft = InchesToPoints(0.13)
.WrapFormat.DistanceRight = InchesToPoints(0.13)
.WrapFormat.Type = 3
.ZOrder 5
.Shadow.Visible = msoFalse
End With

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

'---------------------------------------------------------
Sub DeleteDraft()
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 _
Draft1.Select
Selection.ShapeRange.Delete

ActiveWindow.ActivePane.View.SeekView =
wdSeekMainDocument
End Sub
 
J

Jay Freedman

Hi, Kerri,

The object Draft1 is defined *only* as long as the module remains in memory.
When you close the document, the object is discarded. Think of the object as
just a fancy kind of variable.

The fix has several steps. First, move the declaration of Draft1 inside the
DRAFTWaterMark routine, as it will only need to be local to that routine
after you make other changes.

Next, in the list of properties of Draft1 that you're setting, add

.Name = "Draft1"

This name will be permanently stored in the document with the other
properties, unlike the object "variable".

You could revise the DeleteDraft routine to simply delete the shape named
"Draft1", but that would trigger an error message if you ran the macro when
there was no such shape in the header. Instead, run a loop through all the
shapes (in any) in the header, and delete the one named "Draft1" if you find
it:

Sub DeleteDraft()
Dim MyShape As Shape

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

For Each MyShape In Selection.HeaderFooter.Shapes
If MyShape.Name = "Draft1" Then
MyShape.Select
Selection.ShapeRange.Delete
Exit For
End If
Next MyShape

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
 
K

Kerri

Hi Jay,

Let me start by thanking you for taking the time to help
me, I truly appreciate your time. And let me continue by
saying.

I feel like a fool for asking, but with my limited
experience, I don't know where to put the .Name
= "Draft1". =) I have tired several places but
nothing worked. I thought I knew right where it would go.
but apparently not!

Could I bother you for a little more information on
the .Name = "Draft1"??? This is something I see myself
using a lot in my documents. I might name a textbox as
well.

Thank you again,
Forgive me
Kerri
 
J

Jay Freedman

Hi, Kerri,

In your original code you have these lines:

Draft1.Select
With Selection.ShapeRange
.Fill.Visible = msoTrue
.Fill.Solid

Put the line

.Name = "Draft1"

between the With Selection.ShapeRange line and the .Fill.Visible = msoTrue
line. (Actually it could go anywhere between the With... line and the End
With line, but I like to see it near the beginning.)

No need to apologize. Every one of us started out dazed and confused...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top