how to delete certain autoentries in a document

E

Edward

Hi everyone,
I'm looking for a code to delete certain autoentries ina document. There a
few in each document that all have the same name. I want to write a macro to
search through a document and delete any autoentriey that has that certain
name.
 
J

Jay Freedman

Hi everyone,
I'm looking for a code to delete certain autoentries ina document. There a
few in each document that all have the same name. I want to write a macro to
search through a document and delete any autoentriey that has that certain
name.

Word has two kinds of "autoentries" -- AutoText and AutoCorrect -- as
well as various kinds of fields.

Once an AutoText or AutoCorrect entry has been inserted in a document,
it's just plain text; there is no indication of what its name was, or
even that it was the result of an automatic change instead of simple
typing. If this is the kind of entry you're talking about, all you
need to do is run a Find with the _text_ (not the name) of the entry.

If you're talking about fields, you need to know what kind of field.
Right-click the field and choose Toggle Field Codes. The first word in
the field code is a keyword that says what kind of field it is. Then
you can write a macro to examine each field, determine its type, and
delete it if necessary.
 
E

Edward

Thanks for your response. I'm using this code to insert a logo into in our
word documents ( in several locations)
ActiveDocument.AttachedTemplate.AutoTextEntries("Logo1").Insert _
where:=FPRange, RichText:=True
and logo1 as you see is a autotextentry , so my question is there any way to
run a code and delete all of inserted logos at once?
 
J

Jay Freedman

No, not without either some assumptions about the document or some
preparation at the time the logo is inserted.

If you can safely assume that (a) the logos are all InlineShape objects (or,
alternatively, all floating Shape objects) and (b) the logos are the _only_
InlineShape (or Shape) objects in the document and (c) the logos are in the
body of the document rather than in the header or footer or some other
story, then run the code

Dim nILS As Long
With ActiveDocument.InlineShapes
For nILS = .Count To 1 Step -1
.Item(nILS).Delete
Next
End With

(substitute .Shapes for .InlineShapes if appropriate). If the logos are
inserted in the headers of multiple sections instead of the body of the
document, and are the only graphics in the header, something like this would
be appropriate, with tweaking as necessary:

Dim nILS As Long
With ActiveDocument.Sections(1).Headers( _
wdHeaderFooterPrimary).Shapes
For nILS = .Count To 1 Step -1
.Item(nILS).Delete
Next
End With

If there are, or might be, other graphics in the document, then you need
more planning ahead. When you insert any logo, surround it with a bookmark
with a specific recognizable string in its name. For example, assuming the
logo is inserted as an InlineShape, you could do this:

Dim FPRange As Range
Dim ATnm As String
ATnm = "Logo1" ' each logo assumed to be named "LogoNN"
Set FPRange = Selection.Range ' or set some other range

With ActiveDocument
.AttachedTemplate.AutoTextEntries(ATnm).Insert _
Where:=FPRange, RichText:=True
.Bookmarks.Add Name:=ATnm, Range:=FPRange
End With

The bookmark would thus surround the logo. To delete the logos, use code
like this:

Dim BKnum As Long
With ActiveDocument.Bookmarks
For BKnum = .Count To 1 Step -1
If InStr(.Item(BKnum).Name, "Logo") Then
.Item(BKnum).Range.Delete
End If
Next
End With

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
E

Edward

Thanks Jay . It was very helpful. you gave me a great hint to do this.
I also have logo fields(they are fields not Autotext entries) in header
area, what is the best way to delete them with a macro in our document.
Note: All logo fields are the same ( have the same logo)
 
J

Jay Freedman

Word doesn't have any field type called "logo". Are they
IncludePicture fields, or something else? As I said before, toggle the
field code and look at the keyword that's the first thing in the code.
 
E

Edward

Sorry Jay, I think I wasn't clear . they are Ref field that contain a name
and a logo.
 
J

Jay Freedman

Then it depends on whether you can identify the particular fields that
should be deleted. If they're the only Ref fields in the header, you can use
a modification of one of the code bits I showed before:

Sub demo()
Dim nFld As Long
With ActiveDocument.Sections(1).Headers( _
wdHeaderFooterPrimary).Range.Fields
For nFld = .Count To 1 Step -1
If .Item(nFld).Type = wdFieldRef Then
.Item(nFld).Delete
End If
Next
End With
End Sub

If there are several Ref fields but you know the name of the bookmark to
which the logo-containing Ref field refers, you can examine the code of the
field to see whether it should be deleted. For example, if the field says
{Ref bk1} or just {bk1}, then you could use

Sub demo2()
Dim nFld As Long
With ActiveDocument.Sections(1).Headers( _
wdHeaderFooterPrimary).Range.Fields
For nFld = .Count To 1 Step -1
If (.Item(nFld).Type = wdFieldRef) And _
(InStr(LCase(.Item(nFld).Code.Text), "bk1")) Then
.Item(nFld).Delete
End If
Next
End With
End Sub
 

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