Help - Search - Members - Calendar
Full Version: Deleting text in a footer in a Macro
UtterAccess Discussion Forums > Microsoft® Office > Microsoft Word
timn
Hi,

I wish to delete any text that exists in a footer in preparation for adding new text.

I've recorded the macro to add the new text but the deletion of the existing text i causing me
problems. Obviously the length of the existing text is not known.

I have used the code below: but it just gets rid of 300 characters, which is fine but not comprehensive enough
for me. What if a footer holds 301 characters ??

CODE
Selection.MoveRight Unit:=wdCharacter, Count:=300, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1


Any ideas on how to delete text in a footer without knowing befoe hand the length?

Thanks
Tim
SparrowCathy
Can the footer text to be deleted be named as a bookmark so the code will delete the bookmark instead of specified text characters? Is it the only text in the footer or are you deleting only a portion of the text and leaving other footer text?

If all footer, how about something like
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
timn
Cathy I am including the code:

The footer text is ALL deleted in preparation for new more appropritate text.

Putting the process in context, we have many documents (100's) that have been used by many people over an organisation and they have added their own footers. Now we are centralising and placing one copy of each document on an Intranet. So we are gathering these documents and giving them one consistant footer.

So, all existing footer text has to go, and new text used. This is what the macro is trying to do...and it works.

I am not happy because 300 characters seems kind of arbitary and not good coding form...

If possible I wish to select and delete ALL text in a footer before adding new text

CODE

    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
    If Selection.HeaderFooter.IsHeader = True Then
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    Else
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    End If
    
[color="red"]  Selection.MoveRight Unit:=wdCharacter, Count:=300, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.LanguageID = wdEnglishAUS [/color]
    
    NormalTemplate.AutoTextEntries("Filename and path").Insert Where:= _
        Selection.Range, RichText:=True
    Selection.TypeParagraph
    NormalTemplate.AutoTextEntries("Page X of Y").Insert Where:=Selection. _
        Range, RichText:=True
    Selection.TypeText Text:=vbTab & "Issue Date: "
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldDate
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
SparrowCathy
Try replacing your red portion with:

Selection.WholeStory
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.LanguageID = wdEnglishAUS

Your idea is excellent to solve a common problem with multiple documents, users, etc., and adding footer consistency. I am very new to learning any code at all. This replacement worked in my tests, however, will any of your documents have more than one section with the headers/footers unlinked from previous? If so, the footer replacement as is doesn't search for the next instance of a footer till it finds the last one and replaces it. I don't know enough to add that type of handling, and you may not need it, or may have ideas, or someone else here. I do know, that at least in our documents, we might have 10+sections, many with the 'same as previous' link removed, so there would need to be a way to continue the find/replace till all were replaced. A loop of some kind going to the next footer till all replaced, below might get you started.

ActiveWindow.ActivePane.View.NextHeaderFooter

hth, Cathy
timn
Cathy,

That code you provided worked in my macro. Thank You.
It was faster as well.

Very few if any of our documents have sections, so I do not see that as a problem.

I always get some good ideas from here:

http://word.mvps.org/FAQs/index.htm
Click the macros/VBA tab.

For somebody who is new at coding you have been quite a help :-)

Thanks

Tim
SparrowCathy
Glad to help, Tim, and thanks for the opportunity to learn something new, cause I didn't know the answer going in, and one of the ways I'm learning vb is to record a macro, do the function, and then see what the code is in the macro and how to adjust it to what's needed. The help menu gave me a bulky solution that worked, but as a last resort I creating a macro to see what it said, and it was more efficient than what the help menu provided, so I posted that solution.

I was very intrigued by your situation, cause in the near future we may have a similar need, and it got me thinking there were better ways to handle things and that I really needed to learn more about how to write macros and use them to help me in my work.

I also appreciate the link very much, cause I'm just now learning how to do things in Word, and have been stumbling lots! I've already added it to my favorites list and plan to use it this week to learn how to create a dialog box form.
Thank you so much, and best wishes with your footer conversion project. Cathy
frown.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.