Use variable with Count of GoTo Method?

G

Greg Strong

Why can't I use a variable for the count in the GoTo method to delete
merged text?

Basically the thought was to use a DoWhile Loop on a merged document to
go to the beginning of the second page of each section, go back 2 spaces
to get back to the end of the page 1 of that section and delete the text
on that line, then move on to the next section. The macro runs the
correct number of times, but the GoTo method is not moving past section
number 1. What I end up with is more deleted on the first page of the
first section rather the same codes on the first page of each section.

It appears the thought of using the "numCtr" on the "Count" of the GoTo
method is not working.

See the code below in the quote box with the question mark under the
questioned code. Any ideals? TIA!


,----- [ Macro ]
| Sub TestDeleteDocNo()
| '
| ' TestDeleteDocNo Macro
| ' Macro recorded 1/12/2004 by Greg Strong
| '
| Selection.EndKey Unit:=wdStory
| numRepeats = Selection.Information(wdActiveEndSectionNumber)
| Selection.HomeKey Unit:=wdStory
| numCtr = 1
|
| Do While numCtr < numRepeats
| Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=numCtr, Name:=""
| ?
|
| Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst, Count:=2, Name:=""
| Selection.Find.ClearFormatting
| Selection.MoveLeft Unit:=wdCharacter, Count:=2
| Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
| Selection.Delete Unit:=wdCharacter, Count:=1
| Selection.HomeKey Unit:=wdStory
| numCtr = numCtr + 1
| Loop
|
| End Sub
`-----



Regards,


Greg Strong
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Greg,

Try the following:

Dim i As Long, myrange As Range
For i = 1 To ActiveDocument.Sections.Count
Set myrange = ActiveDocument.Sections(i).Range
myrange.End = myrange.Start
Set myrange = myrange.Bookmarks("\page").Range
myrange.Start = myrange.End
Set myrange = myrange.Bookmarks("\line").Range
myrange.Delete
Next i

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
W

Word Heretic

G'day Greg Strong <[email protected]>,

the 'goto page' is not subordinate to the 'goto section' my friend, it
will always go to page 1 and delete madly.

Steve Hudson
Word Heretic Sydney Australia
Tricky stuff with Word or words

Email: Steve at wordheretic.com


Greg Strong was spinning this yarn:
Why can't I use a variable for the count in the GoTo method to delete
merged text?

Basically the thought was to use a DoWhile Loop on a merged document to
go to the beginning of the second page of each section, go back 2 spaces
to get back to the end of the page 1 of that section and delete the text
on that line, then move on to the next section. The macro runs the
correct number of times, but the GoTo method is not moving past section
number 1. What I end up with is more deleted on the first page of the
first section rather the same codes on the first page of each section.

It appears the thought of using the "numCtr" on the "Count" of the GoTo
method is not working.

See the code below in the quote box with the question mark under the
questioned code. Any ideals? TIA!


,----- [ Macro ]
| Sub TestDeleteDocNo()
| '
| ' TestDeleteDocNo Macro
| ' Macro recorded 1/12/2004 by Greg Strong
| '
| Selection.EndKey Unit:=wdStory
| numRepeats = Selection.Information(wdActiveEndSectionNumber)
| Selection.HomeKey Unit:=wdStory
| numCtr = 1
|
| Do While numCtr < numRepeats
| Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=numCtr, Name:=""
| ?
|
| Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst, Count:=2, Name:=""
| Selection.Find.ClearFormatting
| Selection.MoveLeft Unit:=wdCharacter, Count:=2
| Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
| Selection.Delete Unit:=wdCharacter, Count:=1
| Selection.HomeKey Unit:=wdStory
| numCtr = numCtr + 1
| Loop
|
| End Sub
`-----



Regards,


Greg Strong

Steve Hudson

Word Heretic, Sydney, Australia

wordheretic.com

If answers r 2 terse, ask again or hassle an MVP,
at least they get recognition for it then.
Lengthy replies offlist require payment.
 
J

JGM

Hi Greg,

I notice two small problems with your code. One of them leads to the what
you have experienced, and the other would have cause a small annoyance which
I am sure you would have fixed in no time. See the small changes I have made
in your code below.

First, your problem is due to the fact that you use this code

Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst, Count:=2, Name:=""

after going to each section. This code always brings you to the 2 page in
the document, not the second page from the cursor. To go to the second page
from the cursor, use
"wdGoToRelative" instead of "wdGoToFirst."

Second, the line

Do While numCtr < numRepeats

means that you would never run the code in the last section. if that is what
you intended, then fine. In that case, change it back to what it was in the
code below.

'_______________________________________
Sub TestDeleteDocNo()
'
' TestDeleteDocNo Macro
' Macro recorded 1/12/2004 by Greg Strong
'

Dim numCtr, numRepeats

Selection.EndKey Unit:=wdStory
numRepeats = Selection.Information(wdActiveEndSectionNumber)
Selection.HomeKey Unit:=wdStory
numCtr = 1


Do While numCtr <= numRepeats
Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst,
Count:=numCtr, Name:=""
Selection.GoTo What:=wdGoToPage, Which:=wdGoToRelative, Count:=1,
Name:=""

Selection.Find.ClearFormatting
Selection.MoveLeft Unit:=wdCharacter, Count:=2
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdStory
numCtr = numCtr + 1
Loop

End Sub
'_______________________________________

HTH
Cheers!
--
_______________________________________
Jean-Guy Marcil
(e-mail address removed)

Greg Strong said:
Why can't I use a variable for the count in the GoTo method to delete
merged text?

Basically the thought was to use a DoWhile Loop on a merged document to
go to the beginning of the second page of each section, go back 2 spaces
to get back to the end of the page 1 of that section and delete the text
on that line, then move on to the next section. The macro runs the
correct number of times, but the GoTo method is not moving past section
number 1. What I end up with is more deleted on the first page of the
first section rather the same codes on the first page of each section.

It appears the thought of using the "numCtr" on the "Count" of the GoTo
method is not working.

See the code below in the quote box with the question mark under the
questioned code. Any ideals? TIA!


,----- [ Macro ]
| Sub TestDeleteDocNo()
| '
| ' TestDeleteDocNo Macro
| ' Macro recorded 1/12/2004 by Greg Strong
| '
| Selection.EndKey Unit:=wdStory
| numRepeats = Selection.Information(wdActiveEndSectionNumber)
| Selection.HomeKey Unit:=wdStory
| numCtr = 1
|
| Do While numCtr < numRepeats
| Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=numCtr, Name:=""
| ?
|
| Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst, Count:=2, Name:=""
| Selection.Find.ClearFormatting
| Selection.MoveLeft Unit:=wdCharacter, Count:=2
| Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
| Selection.Delete Unit:=wdCharacter, Count:=1
| Selection.HomeKey Unit:=wdStory
| numCtr = numCtr + 1
| Loop
|
| End Sub
`-----



Regards,


Greg Strong
 
G

Greg Strong

This code always brings you to the 2 page in
the document, not the second page from the cursor. To go to the second page
from the cursor, use
"wdGoToRelative" instead of "wdGoToFirst."

Thanks! Your correction corrects my problem.
Second, the line

Do While numCtr < numRepeats

means that you would never run the code in the last section. if that is what
you intended, then fine.

Yes this is what I intended because after the merge the document's last
section really has nothing except just the paragraph symbol.

Thanks again!

Regards,


Greg Strong
 
G

Greg Strong

Dim i As Long, myrange As Range
For i = 1 To ActiveDocument.Sections.Count
Set myrange = ActiveDocument.Sections(i).Range
myrange.End = myrange.Start
Set myrange = myrange.Bookmarks("\page").Range
myrange.Start = myrange.End
Set myrange = myrange.Bookmarks("\line").Range
myrange.Delete
Next i

Other suggested use of ranges as well. It looks like I'll have to do
some reading.

Regards,


Greg Strong
 

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