Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2006

Tip: Looking for answers? Try searching our database.

Repeat code on each page

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Josha - 11 Sep 2006 04:45 GMT
Hi,

I am new to VB and still getting a handle on things. How do I repeat the
below code for each new odd numbered page. If this requires the use of line
numbers instead of paragraph numbers to set the range, see the remark in code.

Josha

Code:
Sub Test3()

Dim pChar As Variant
Set myDoc = ActiveDocument

'The following example creates a Range object beginning at the start
'of the second paragraph and ending after the third paragraph.
'The corresponding line numbers that need to be used are ln10 to ln15.
'The code must be repeated for each odd numbered page.

Set myRange = myDoc.Range(Start:=myDoc.Paragraphs(2).Range.Start  , _
   End:=myDoc.Paragraphs(5).Range.End)
   
 
   For Each pChar In myRange.Characters
       If pChar Like "[A-Z]" Then
       pChar.Select
       Selection.Range.Case = wdTitleWord
       End If
   Next

End Sub

--------------------------------------------------------------------------------
Helmut Weber - 11 Sep 2006 15:31 GMT
Hi Josha,

>How do I repeat the below code for each new odd numbered page.

For each *new* odd numbered page? That would very difficult.

>'The following example creates a Range object beginning at the start
>'of the second paragraph and ending after the third paragraph.

No, after the fifth!

>Set myRange = myDoc.Range(Start:=myDoc.Paragraphs(2).Range.Start  , _
>    End:=myDoc.Paragraphs(5).Range.End)

To process a range including the paragraphs 2 and 3
on each odd numbered page, you may use something like this:

Sub Makro2()
Dim lPgs As Long   ' number of pages
Dim lCnt As Long   ' just a counter
Dim rtmp As Range  ' a temporary range
With ActiveDocument
  lPgs = .BuiltInDocumentProperties("Number of pages")
  .Range(0, 0).Select ' goto start of doc
End With
With selection
  .ExtendMode = False ' just in case
  Set rtmp = .Range
  For lCnt = 1 To lPgs Step 2
     .GoTo what:=wdGoToPage, _
     which:=wdGoToAbsolute, _
     Count:=lCnt
     Set rtmp = .Bookmarks("\page").Range
     rtmp.End = rtmp.Paragraphs(3).Range.End
     rtmp.start = rtmp.Paragraphs(2).Range.start
     rtmp.Select ' for testing using [F8]
     With rtmp.Find
     ' your code, which is the real difficult part
     ' you may have to redefine the range after find.execute
     ' depending on what you do to it.
     End With
  Next
End With
End Sub

Though there may be lots of complications,
e.g. with paragraphs spanning from one page to another
or even over several pages.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Josha - 12 Sep 2006 06:01 GMT
Sorry the fith paragraph.

I dont want each "NEW" odd page but each odd page. sorry, i reaslise the
confusion.

will give it a go. thanks

> Hi Josha,
>
[quoted text clipped - 44 lines]
> e.g. with paragraphs spanning from one page to another
> or even over several pages.
Josha - 12 Sep 2006 06:30 GMT
Ok...

The code works to a point. The proble is that it references paragraph
numbers based on paragraph breaks (established by pressing enter). Paragraphs
are specified across the whole document. I need to specify the range based on
the line number which is a page specific reference. The line numbers can bee
seen represent along the bottom screen bar when in print view mode, as i am
sure you already know.

Josh

> Hi Josha,
>
[quoted text clipped - 44 lines]
> e.g. with paragraphs spanning from one page to another
> or even over several pages.
Helmut Weber - 12 Sep 2006 11:50 GMT
Hi Josha

how about this one?

Sub Makro2()
Dim lPgs As Long   ' number of pages
Dim lCnt As Long   ' just a counter

With ActiveDocument
  lPgs = .BuiltInDocumentProperties("Number of pages")
  .Range(0, 0).Select
End With
With selection
  For lCnt = 1 To lPgs Step 2
    .ExtendMode = False
    .Collapse Direction:=wdCollapseEnd
     .GoTo what:=wdGoToPage, _
     which:=wdGoToAbsolute, _
     Count:=lCnt
     .GoTo what:=wdGoToLine, _
     which:=wdGoToNext, _
     Count:=4 ' goto line 4
     .ExtendMode = True
     .GoTo what:=wdGoToLine, _
     which:=wdGoToNext, _
     Count:=4 ' move 4 lines further down
     Stop ' for testing
     With .Range
     ' your code
     End With
  Next
End With
End Sub

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Josha - 13 Sep 2006 05:08 GMT
Helmut,

It worked this time but i had to remove my range referencing the paragraph
numbers. This is not a problem, it definately works. Is it possible to use
the 'Paragraphs' reference with the counter to do the same job?

This is how the code looked as in a working form.

Sub ChangeAddressCase()
Dim lPgs As Long   ' number of pages
Dim lCnt As Long   ' just a counter

With ActiveDocument
  lPgs = .BuiltInDocumentProperties("Number of pages")
  .Range(0, 0).Select
End With
With Selection
  For lCnt = 1 To lPgs Step 2
    .ExtendMode = False
    .Collapse Direction:=wdCollapseEnd
     .GoTo what:=wdGoToPage, _
     which:=wdGoToAbsolute, _
     Count:=lCnt
     .GoTo what:=wdGoToLine, _
     which:=wdGoToNext, _
     Count:=9 ' goto line 10
     .ExtendMode = True
     .GoTo what:=wdGoToLine, _
     which:=wdGoToNext, _
     Count:=6 ' move 6 lines further down
     'Stop ' for testing
     With .Range

     
 

              Selection.Range.Case = wdTitleWord

     End With
  Next
End With
End Sub

Josha

> Hi Josha
>
[quoted text clipped - 29 lines]
> End With
> End Sub
Helmut Weber - 13 Sep 2006 15:10 GMT
Hi Josha,

I don't quite understand whether
you talk about lines or paragaphs
or on line paragraphs.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.