Repeat code on each page

J

Josha

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


--------------------------------------------------------------------------------
 
H

Helmut Weber

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.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Josha

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

Helmut Weber said:
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.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Josha

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

Helmut Weber said:
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.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Helmut Weber

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

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Josha

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
 
H

Helmut Weber

Hi Josha,

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

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top