MS Word Macros

Padawan

Active Member
Joined
Apr 9, 2002
Messages
395
Greetings, All.

I'm working on some text files I need to edit and want to write some VBA for this. But I need a little bit of help with some of the macros.

Specifically, what I'm trying to do is to search for a specific string (>> PAGE), count the occurances, then set a loop to count up to the number of occurances to number the pages.

I tried the recorder, but it didn't record what I needed. I used the find, replace all with the Find What text (^&). The message box returns replaced XX number of times. I need to capture this message box number to set my variable.

As an alternate, I can search for the >>PAGE and then overwrite the number. The problem I had was it would insert but not overwrite the number.

As a third alternative, I really don't care how I accomplish this if someone has a solution with a better technique to complete what I'm trying to do. I'm not set in stone to use the techniques I'm trying if you have a better way.

Can anyone give me a little assistance, or direct me to a website that can?

THANK YOU!!!!!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi,
Try starting with follows:
Rich (BB code):
Sub CountOfOccurrences()
  Dim i As Long         ' <-- count variable
  With ActiveDocument.Content.Find
    .Text = "PAGE"      ' <-- text to find
    .ClearFormatting
    .Forward = True
    .Wrap = wdFindStop  ' <-- to stop the finding loop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    While .Execute
      i = i + 1
    Wend
    MsgBox """" & .Text & """ happens " & i & " times"
  End With
End Sub


Sub ReplaceTextToOrderedNum()
  Dim i As Long
  i = 1  ' <-- "PAGE" will be replaced by: 1, 2, etc
  With ActiveDocument.Content.Find
    .Text = "PAGE"          ' <-- text to find
    .Replacement.Text = i   ' <-- replacement text
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindContinue  ' <-- to process all occurrences
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    While .Execute(Replace:=wdReplaceOne)
      i = i + 1
    Wend
    MsgBox "Replacements = " & i - 1
  End With
End Sub

Regards,
Vladimir
 
Last edited:
Upvote 0
ZVI

The counting code works fine, but I can't get the numbering to work. It is replacing the all occurances with the number "1".



Thoughts?
 
Upvote 0
Sorry, it's my misprint.
In ReplaceTextToOrderedNum() code just add below this line:
i = i + 1
that one:
.Replacement.Text = i
 
Upvote 0
That did it. I was trying to figure it out, but didn't know about adding the line.

Now I just have to tweak a little bit and I think I have it.

THANK YOU
 
Upvote 0
John,
I'm glad it has helped!
Cheers,
Vladimir
 
Upvote 0

Forum statistics

Threads
1,225,400
Messages
6,184,762
Members
453,255
Latest member
excelbit

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top