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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Macro to put page break before #$%

Status
Not open for further replies.

1234347584375

Technical User
Jul 6, 2011
3
US
I am given a file every day with 50+ blocks of data that each begin with "#$%" and I need to serate each block into a new page. I tried recording a macro but no luck....this is what i have so far, but i have no idea if it is remotely correct. THANKS IN ADVANCE :)

Selection.Find.ClearFormatting
With Selection.Find
.Text = "#$%"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.InsertBreak Type:=wdPageBreak
 


Code:
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "#$%"[b]
        .Replacement.Text = "^m"[/b]
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks!
Another quick question. Im trying to get each data block to save as a new file (as an .rtf), while keeping the original document intact. The code below is almost accurate, but i get an infinite loop. Any ideas?

Sub patient dictation()

Num = 0
Do While ActiveDocument.Range.End > 1
Selection.HomeKey wdStory
Selection.Bookmarks("\page").Range.Copy
With Documents.Add
.Range.Paste
Num = Num + 1
.SaveAs "patientdictation" & Num, FileFormat:=wdFormatRTF
.Close
End With
Loop


End Sub
 
Thanks!
Another quick question. Im trying to get each data block to save as a new file (as an .rtf), while keeping the original document intact. The code below is almost accurate, but i get an infinite loop. Any ideas?

Sub dictation()

Num = 0
Do While ActiveDocument.Range.End > 1
Selection.HomeKey wdStory
Selection.Bookmarks("\page").Range.Copy
With Documents.Add
.Range.Paste
Num = Num + 1
.SaveAs "patientdictation" & Num, FileFormat:=wdFormatRTF
.Close
End With
Loop


End Sub
 
As you don't change the ActiveDocument, you won't change its Range.End. If your condition is ever true it will always be true.

Also, you always - every time round the loop - go to the start of the document and grab the page there (page 1).

Try something like this instead ..

Code:
[blue]With Selection
    For Num = 1 To .Information(wdNumberOfPagesInDocument)
        .GoTo wdGoToPage, wdGoToAbsolute, Num
        .Bookmarks("\Page").Range.Copy
        With Documents.Add
            .Range.Paste
            .SaveAs "patientdictation" & Num, FileFormat:=wdFormatRTF
            .Close
        End With
    Next
End With[/blue]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top