Remove Text

E

EB

I'm using the following code to remove some text form fields plus a logo i
have at the top of my template. ub RemoveHeader()
ActiveDocument.FormFields("Text12").Delete
ActiveDocument.FormFields("Text13").Delete
ActiveDocument.FormFields("Text14").Delete
ActiveDocument.FormFields("Text15").Delete
ActiveDocument.FormFields("Text16").Delete
ActiveDocument.FormFields("Text17").Delete
ActiveDocument.FormFields("Text18").Delete
ActiveDocument.Shapes(1).Delete

I would like to modify the code so that I can delete two words located at
the top of the document. The word "Telephone" and the word "Facsimile." I
have tried using the following, however, it does not work:
Selection.Find.Text = ("Telephone")
Selection.Delete

Can someone tell me what I'm doing wrong?

thanks,
 
W

Wraithchilde

You need to execute the find before you delete, like so:
Selection.Find.Text = ("Telephone")
Selection.Find.Execute
Selection.Delete
 
E

Edward Thrashcort

You have to execute a find

Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = ("Telephone")
.Execute
If .Found Then Selection.Delete
End With

Eddie
 
E

EB

The code below does not work. All the Text form fields are removed except
for the word Telephone. I added your code as follows:

Sub RemoveHeader()
ActiveDocument.FormFields("Text12").Delete
ActiveDocument.FormFields("Text13").Delete
ActiveDocument.FormFields("Text14").Delete
ActiveDocument.FormFields("Text15").Delete
ActiveDocument.FormFields("Text16").Delete
ActiveDocument.FormFields("Text17").Delete
ActiveDocument.FormFields("Text18").Delete
ActiveDocument.Shapes(2).Delete
Selection.Find.Text = ("telephone")
Selection.Find.Execute
Selection.Delete
End Sub

I'm new to macros, so I dont understand what I'm doing wrong.
 
E

EB

Edward,

I'm trying to figure out what is the best way for me to proceed with this.
instead of deleting all of the text form fields, the logo, and the words
"Telephone and Facimile" is there a way I can set all that information so
that it does not print or so that it's hidden? my code now looks like this:

Sub RemoveHeader()

Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = ("Telephone")
.Execute
If .Found Then Selection.Delete
End With

With Selection.Find
.Text = ("Facsimile")
.Execute
If .Found Then Selection.Delete
End With

ActiveDocument.FormFields("Text12").Delete
ActiveDocument.FormFields("Text13").Delete
ActiveDocument.FormFields("Text14").Delete
ActiveDocument.FormFields("Text15").Delete
ActiveDocument.FormFields("Text16").Delete
ActiveDocument.FormFields("Text17").Delete
ActiveDocument.FormFields("Text18").Delete
ActiveDocument.Shapes(2).Delete

End Sub

Sorry for all the questions, but I'm learning as I go and I'm trying to
figure out what is the best way to do this.

Thanks,
 
E

Edward Thrashcort

Couldn't you just set the default value of all fields to nothing?

Dim ThisField As FormField

With ActiveDocument.Range

For i = 1 To .FormFields.Count
Application.StatusBar = "Initialising Formfield " & Str(i)
Set ThisDocField = .FormFields(i)
Select Case ThisDocField.Type
Case wdFieldFormCheckBox: ThisDocField.CheckBox.Default = False
Case wdFieldFormTextInput: ThisDocField.TextInput.Default = ""
End Select
Next

End With

Use the online help to understand the syntax

Eddie
 
E

EB

Once again thanks Eddie,

One more question. is there a way to anchor the cursor when running a
macro? After all the formfields, logo, and the two words are deleted the
cursor jumps to where the word "Telephone" use to be. Is there away to keep
anchor below the Continuous Section break?
 
E

Edward Thrashcort

switch screenupdating off then reposition the cursor when the macro ends

Using the Selection object BY DEFINITION moves the cursor.

Maybe you should define telephone anf fax as bookmarks and delete them
without selecting them directly

Look it up on the online Help. That's the bset way to learn about macros

Eddie
 
L

Lene Fredborg

In addition to the built-in VBA help (which is really helpful), you will find
a lot of useful macro/VBA information on the Word MVP Site:
http://word.mvps.org/FAQs/MacrosVBA/index.htm

Most often, the same problem can be solved in many different ways using VBA.
Once you get more familiar with VBA, you will find out how you can optimize
your code.

A tip related to your current code:
Instead of repeating the same code line for each FormField you are deleting,
you could use this code:

Dim n As Long
For n = 12 To 18
ActiveDocument.FormFields("Text" & n).Delete
Next n

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

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