Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace Macro in a Footer 3

Status
Not open for further replies.

noHandlebars

Technical User
Jun 25, 2008
46
US
Hi I'm trying to create a macro that will ask the user for input, then take that input and put it into the existing footer of a document, replacing what was previously in the footer.

This is what I have so far:

Sub FasterFindAndReplaceAllStoriesHopefully()

Dim myStoryRange As Range

Dim facilityName As String
Dim siteDate As String

'input boxes get facility and site date information
'facilityName = InputBox("Enter the correct facility name - location, st", "Facility Name Information", "Facility Name - Location, ST")
'siteDate = InputBox("Enter the correct site visit date (All Caps, First 3 Letters of Month)", "Site Visit Date Information", "JAN 1, 1111")


'First search the main document using the Selection
With Selection.Find
.Text = "FACILITY NAME - LOCATION, ST"
.Replacement.Text = "asdf"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With

'Now search all other stories using Ranges
For Each myStoryRange In ActiveDocument.StoryRanges
If myStoryRange.StoryType <> wdMainTextStory Then
With myStoryRange.Find
.Text = "FACILITY NAME - LOCATION, ST"
.Replacement.Text = "asdf"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = "FACILITY NAME - LOCATION, ST"
.Replacement.Text = "asdf"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Loop
End If
Next myStoryRange

End Sub


Right now I have the input boxes disabled and just some dummy text for the replacement text. But when i run the macro nothing happens. Any suggestions on to why its not working?
 
When you say nothing is working, does it run and create an error or do you see nothing happening at all.
Do you actually have some text in the footer saying "FACILITY NAME - LOCATION, ST"?

I have just created a document with this in the footer and it works perfectly.

Matt
[rockband]
 
I see nothing happen and yes "facility name - location, st" is the text in the footer being replaced. I also forgot to mention that the footers are not linked to each other.
 
Use the following:
.MatchCase = False

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
If you want or are able to post the file I will have a look at it for you.

Matt
[rockband]
 
OK this is strange but I know why it is not making the changes.

It took a while but I eventually decided to go through the string "FACILITY NAME - LOCATION, ST" a character at a time and comparing them to the same string I had manually typed in myself. Everything matched except for the dash between "name" and "location". I compared the strings again this time converting them to the ascii value and your string returned an ascii value of 150 for your dash where the string I had typed in a value of 45 which is what I would have expected.

If you are able to go through and make the changes to the hyphens in your file then run the code again you will see it works perfectly and makes the necessary changes.

Hope this makes sense. Any probs post back.

Matt
[rockband]
 
if you want to run this code, it will find all character with an asc value of 150 and change them to asc value of 45.

Code:
Sub change150()
For Each mchar In ActiveDocument.Characters
If Asc(mchar) = 150 Then
mchar = Chr(45)
End If

Next

End Sub

Matt
[rockband]
 
You may replace this:
.Text = "FACILITY NAME - LOCATION, ST"
with this:
.Text = "FACILITY NAME " & Chr(150) & " LOCATION, ST"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
thanks that was a good catch chandlm. the macro is working great now.
 
I may be missing something, but if the real issue is, in fact:

Hi I'm trying to create a macro that will ask the user for input, then take that input and put it into the existing footer of a document, replacing what was previously in the footer.
then...

Code:
ActiveDocument.Sections(1).Footers(1).Range.Text = _
    InputBox("first text?") & "  " & InputBox("second text?")

does precisely that.

It replaces whatever is in the footer with the contents of the two inputboxes. I added the " " spaces between the two strings from the inputboxes.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top