View Full Version : Start a new paragraph after the word “apple”.
uktous
08-31-2012, 09:26 AM
Could you please write me the macro that can perform the following function?
Start a new paragraph after the word “apple”.
For example:
Tom likes apple John eats apple Mary wants apple
Becomes
Tom likes apple
John eats apple
Mary wants apple
Thanks
Frosty
08-31-2012, 09:52 AM
Why don't you just do a find replace on
"apple"
and replace with
"apple^p"
That functionality is built-in to Word.
uktous
08-31-2012, 10:00 AM
Why don't you just do a find replace on
"apple"
and replace with
"apple^p"
That functionality is built-in to Word.
because i want to learn
I am new to word vba
Frosty
08-31-2012, 10:03 AM
Then record a macro while doing a find replace. That is the best way to learn: recording macros.
uktous
08-31-2012, 10:19 AM
Then record a macro while doing a find replace. That is the best way to learn: recording macros.
I got
Selection.TypeParagraph
However, I hope that the above code will be performed whenever the word "apple" exists.
Frosty
08-31-2012, 10:20 AM
No, that's just typing a paragraph. There is no search of "apple"
Do you know how to use Find/Replace?
uktous
08-31-2012, 10:28 AM
No, that's just typing a paragraph. There is no search of "apple"
Do you know how to use Find/Replace?
Sub Macro1()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "apple"
.Replacement.Text = "apple^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Frosty
08-31-2012, 10:29 AM
Perfect! There's your answer. And that's a good way to learn VBA, as a new programmer. Congratulations, you're on your way!
uktous
09-01-2012, 02:19 AM
Perfect! There's your answer. And that's a good way to learn VBA, as a new programmer. Congratulations, you're on your way!
hi,
I know the effect of ^p.
I want to know the effect of other ^ + some texts such as ^t.
^t will generate a space
Do you have the full list of ^ + some texts?
eg ^a, ^b....
Frosty
09-01-2012, 08:35 AM
I don't know them all. But ^t is a tab character, not a space. Why don't you just explore the find/replace "More" area? It puts the codes in for you.
uktous
09-01-2012, 10:21 AM
I don't know them all. But ^t is a tab character, not a space. Why don't you just explore the find/replace "More" area? It puts the codes in for you.
very helpful
gmaxey
09-01-2012, 02:26 PM
Most, if not all, of these are still applicable:
http://support.microsoft.com/?kbid=197855
gmaxey
09-01-2012, 02:31 PM
Put your sample text in the header or footer and the main text area of the document and you will learn one of the shortcomings of Find.Exexute in VBA. It only processes the current storyrange.
Also, I prefer to use the Range object over the Selection object:
Public Sub FindReplaceAnywhere()
Dim rngStory As Word.Range
Dim pFindTxt As String
Dim pReplaceTxt As String
Dim lngJunk As Long
Dim oShp As Shape
pFindTxt = InputBox("Enter the text that you want to find.", _
"FIND")
If pFindTxt = "" Then
MsgBox "Cancelled by User"
Exit Sub
End If
Tryagain:
pReplaceTxt = InputBox("Enter the replacement.", "REPLACE")
If pReplaceTxt = "" Then
If MsgBox("Do you just want to delete the found text?", vbYesNoCancel) = vbNo Then
GoTo Tryagain
ElseIf vbCancel Then
MsgBox "Cancelled by User."
Exit Sub
End If
End If
'Fix the skipped blank Header/Footer problem
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
ResetFRParameters
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
SrcAndRplInStory rngStory, pFindTxt, pReplaceTxt
On Error Resume Next
Select Case rngStory.StoryType
Case 6, 7, 8, 9, 10, 11
If rngStory.ShapeRange.Count > 0 Then
For Each oShp In rngStory.ShapeRange
If oShp.TextFrame.HasText Then
SrcAndRplInStory oShp.TextFrame.TextRange, _
pFindTxt, pReplaceTxt
End If
Next
End If
Case Else
'Do Nothing
End Select
On Error GoTo 0
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End Sub
Public Sub SrcAndRplInStory(ByVal rngStory As Word.Range, _
ByVal strSearch As String, _
ByVal strReplace As String)
With rngStory.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = strSearch
.Replacement.Text = strReplace
.Execute Replace:=wdReplaceAll
End With
End Sub
Sub ResetFRParameters()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
very helpful
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.