Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 08-08-2012, 01:24 AM
expatchic expatchic is offline Windows 7 64bit Office 2007
Novice
 
Join Date: Aug 2012
Posts: 3
expatchic is on a distinguished road
Default Add Page Break to Document

Hello. I was able to "write" some VBA code to change the font (highlighting) of certain words. I say that lightly...I stole it from a recorded macro, and then enhanced it based on some examples I found on the web.



Now what I need to do is when I find the text string, back up 2 lines, and insert a page break. Would also like to check for the existence of a page break before inserting a new one.

A novice at VBA, but not programming in general. Any help would be greatly appreciated.

Thank you,

ExpatChic
Reply With Quote
  #2  
Old 08-08-2012, 01:51 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,352
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Hi ExpatChic,

It would be helpful ig you could post the code you're using, plus some clarification of what you mean by "back up 2 lines". Are these 2 lines part of the same paragraph, or separate paragraphs?

PS: I split your post off into a new thread, as it seems unrelated to the one you attached it to.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 08-08-2012, 04:06 AM
expatchic expatchic is offline Windows 7 64bit Office 2007
Novice
 
Join Date: Aug 2012
Posts: 3
expatchic is on a distinguished road
Default

Here you go:

Code:
Dim vFindText, vReplText As String
Dim vColor As String
Dim x As Integer
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
For x = 1 To 3
  Select Case x
    Case Is = 1
      vFindText = "Cat I"
      vColor = wdRed
    Case Is = 2
      vFindText = "Cat II"
      vColor = wdYellow
    Case Is = 3
      vFindText = "Cat III"
      vColor = wdPink
  End Select
  vReplText = vFindText
  With Selection.Find
    .Forward = True
    .Wrap = wdFindContinue
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Format = True
    .MatchCase = False
    .Text = vFindText
    .Replacement.Text = vReplText
    .Replacement.Highlight = True
    .Execute Replace:=wdReplaceAll
    .Font.Color = vColor
  End With
Next x
Now the text is something like this:

blah blah blah blah
blah blah blah blah
one or more blank lines
Heading
Cat I (or II or III)
blah
blah
blah

This scenario repeats throughout a 600+ page document. I would like to insert a page break before each Heading...I.e. go back 2 lines. But, I would also like to check for the existence of a page break before adding another one.

I tried various forms of Do...Loop, but I got myself into an endless loop. :-)

Thank you for your help.

Last edited by macropod; 08-08-2012 at 04:45 AM. Reason: Added code tags & formatting
Reply With Quote
  #4  
Old 08-08-2012, 04:50 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,352
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Hi ExpatChic,

If you want a page break before all Heading1 paragraphs, simply change the Heading1 Style to include the 'page break before' attribute. No code required.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #5  
Old 08-08-2012, 06:55 AM
expatchic expatchic is offline Windows 7 64bit Office 2007
Novice
 
Join Date: Aug 2012
Posts: 3
expatchic is on a distinguished road
Default

ok, but it's not really a HEADING in the Word sense of the word (no pun); it's simply words that denote the start of a new Cat I/II/III "section". (Not a word section either; just more words).

blah blah blah
blah blah blah

New Category
Cat I/II/III
blah blah blah
blah blah blah

New Category
Cat I/II/III
blah blah blah
blah blah blah

But regardless of that, how would you set up code to do this? I'm trying to learn VBA, too!

Thanks again.
Reply With Quote
  #6  
Old 08-08-2012, 04:16 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,352
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Hi ExpatChic,

Try the following. I had to make some changes to your code, as it wasn't working.
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim vFindText As String, vColor As String, x As Long
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchWildcards = True
  'Insert Page breaks before the paragraphs preceding the 'Cat #' paragraphs
  .Text = "[!^13]{1,}^13Cat I"
  .Replacement.Text = "^m^&"
  .Execute Replace:=wdReplaceAll
  'Delete any duplicate page breaks
  .Text = "[^m]{2,}"
  .Replacement.Text = "^m"
  .Execute Replace:=wdReplaceAll
  'Highlight the 'Cat #' paragraphs
  .Replacement.Text = "^&"
  .Replacement.Highlight = True
  For x = 1 To 3
    Select Case x
      Case Is = 1
        vFindText = "Cat I^13"
        vColor = wdRed
      Case Is = 2
        vFindText = "Cat II^13"
        vColor = wdYellow
      Case Is = 3
        vFindText = "Cat III^13"
        vColor = wdPink
    End Select
    Options.DefaultHighlightColorIndex = vColor
    .Text = vFindText
    .Execute Replace:=wdReplaceAll
  Next x
  Options.DefaultHighlightColorIndex = wdNoHighlight
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply
Please reply to this thread with any new information or opinions.

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Page break formatting changes Tom0822 Excel 1 03-19-2012 08:50 PM
Force a page break Emaleth9999 Mail Merge 1 02-09-2012 02:36 AM
Delete a page after Section Break Next Page Aston Word 5 07-14-2011 05:43 PM
paragaph hard break, soft break and ...strange break czomberzdaniela Word 2 12-03-2010 06:58 PM
Page Break Manolo Word 0 04-29-2009 11:04 PM


All times are GMT -7. The time now is 03:01 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft