Macro Question for Word 2003

M

Marilyn

I have been trying to record a macro that will delete a Frame and the Text
Form field inside of it. I only want just that data which is at the top of
the Page to be deleted for people who already have pre-printed letterheads.
However, the Macro I record only works if my cursor is next to that
information.

Can someone please point me to a macro that can delete just that information
at the top of the document regardless of the position of my cursor?

Thanks,
 
M

Marilyn

Ok, this is the macro i manage to record and it delets the frame but it
leaves behind the text.
Sub Clean()
For Each aFrame In ActiveDocument.Frames
aFrame.Delete
Next aFrame
End Sub

I would like to Delete the top frame and the text inside that frame.

Thanks,
 
L

Lisa

I think this will do what you want, assuming it's the first frame in the
document. (Otherwise, change the index value -- i.e., the number inside the
parens -- to the appropriate number... "Frames(2)" if it's the second frame.)

Sub DeleteFirstFrame
ActiveDocument.Frames(1).Select
Selection.Delete
End Sub

The code you provided in your second post will delete all frames in the
active document. (And I don't know why it leaves the contents of the frame
behind if you don't select the frame, but that just seems to be the way it
works ;-)
 
M

Marilyn

Hi Lisa!

That was great and it worked like a charm, however, I have two frames that I
want to remove from the template. How do I tell the macro to remove the
second one as well.

Thanks,
 
J

Jean-Guy Marcil

Marilyn was telling us:
Marilyn nous racontait que :
Hi Lisa!

That was great and it worked like a charm, however, I have two frames
that I want to remove from the template. How do I tell the macro to
remove the second one as well.

Try:

Dim i As Long

With ActiveDocument
If .Frames.Count > 0 Then
For i = 1 To .Frames.Count
.Frames(1).Delete
Next
End If
End With

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
L

Lisa

Hi Marilyn,
Please keep in mind that I'm VERY new to VBA -- and coding in general, so
these probably aren't the most elegant solutions. (For example, I suspect
there's a way to use a "range" to delete the first two frames, but I haven't
gotten around to working with ranges yet...)

Below are two macros that seem to work for me -- the first macro deletes the
first two frames (and their content); the second deletes all frames (and
their content). (And I still don't know why you need to select the frame
before deleting it to makes its content go away too ;-)

Sub DeleteFirstTwoFrames()
Dim MyFCount As Integer
MyFCount = ActiveDocument.Frames.Count
If MyFCount >= 1 Then 'if theres one or more frames in the doc, do the
following
'otherwise display a msg that there are no frames
If MyFCount = 1 Then 'if there's only one frame, delete it
ActiveDocument.Frames(1).Select
Selection.Delete
Else 'if there's more than one frame, delete the first two
ActiveDocument.Frames(2).Select 'delete the second frame first,
otherwise
'frame 2 becomes frame 1 as soon as you delete the first frame,
'so you would end up deleting frames 1 & 3
Selection.Delete
ActiveDocument.Frames(1).Select 'delete the first frame
Selection.Delete
End If
Else: MsgBox "There are no frames in this document."
End If
End Sub
Sub DeleteAllFrames()
Dim MyFCount As Integer
Dim myFrame As Frame
MyFCount = ActiveDocument.Frames.Count
If MyFCount >= 1 Then
For Each myFrame In ActiveDocument.Frames
myFrame.Select
Selection.Delete
Next myFrame
Else: MsgBox "There are no frames in this document."
End If
End Sub
 
L

Lisa

The example to delete all frames from Jean-Guy is looks better to me
(streamlined/efficient) than what I had posted. (Not surprising since he
knows what he's doing and I don't ;-)... His response wasn't showing when I
responded.)

The only "missing piece" is selecting the frames. (For some reason, if you
just delete a frame, it leaves behind the content of the frame -- converting
it to inline text (and inlineshapes, etc.). However, if you select the frame
then delete the selection, it removes the content of the frame as well.)

So, with that addition, I think his code would be as shown below. (The
"Else: MsgBox" stuff is optional; you can remove that whole line if you don't
want to be told there that there are no frames if you try to run the macro on
a doc that doesn't include frames. Also, if you have Option Explicit at the
top of the module, you'd need to dim "i" as integer)
Sub DeleteAllFrames()
With ActiveDocument
If .Frames.Count > 0 Then
For i = 1 To .Frames.Count
.Frames(1).Select
Selection.Delete
Next
Else: MsgBox "There are no frames in this document."
End If
End With
End Sub
 
J

Jean-Guy Marcil

Lisa was telling us:
Lisa nous racontait que :
The example to delete all frames from Jean-Guy is looks better to me
(streamlined/efficient) than what I had posted. (Not surprising since
he knows what he's doing and I don't ;-)... His response wasn't
showing when I responded.)

The only "missing piece" is selecting the frames. (For some reason,
if you just delete a frame, it leaves behind the content of the frame
-- converting it to inline text (and inlineshapes, etc.). However, if
you select the frame then delete the selection, it removes the
content of the frame as well.)

So, with that addition, I think his code would be as shown below. (The
"Else: MsgBox" stuff is optional; you can remove that whole line if
you don't want to be told there that there are no frames if you try
to run the macro on a doc that doesn't include frames. Also, if you
have Option Explicit at the top of the module, you'd need to dim "i"
as integer)
Sub DeleteAllFrames()
With ActiveDocument
If .Frames.Count > 0 Then
For i = 1 To .Frames.Count
.Frames(1).Select
Selection.Delete
Next
Else: MsgBox "There are no frames in this document."
End If
End With
End Sub

I had not realized that the content of the frame had to go as well.
In that case, try this instead :

With ActiveDocument
Do While .Frames.Count > 0
.Frames(1).Range.Delete
Loop
End With

We need a loop because the first .Range.Delete deletes the content, but
leaves an empty frame on the page... So we need to delete each frame twice,
or use the "Select" tactic.

By the way, just as a side note, it is always better to avoid using the
Selection object whenever possible because it changes the user selection, it
is slower and unreliable. Also, while it is true that if you do not use
Option Explicit you do not have to declare all variables, I do not think it
is a good programming habit.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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