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

How to loop MSWORD macro until end of document?

Status
Not open for further replies.

Chavito1983

Technical User
Aug 12, 2008
10
NL
Hi all,

Sorry for the wrong post. This should be the right place then.

Who can help me find a way to loop something until the end of a Word document? I just can't get it to stop.

Primarily, I am trying to loop (or better, alternate) two macros from the insertion point until the end of the document. If possible and easy to do, I would like to also have the option to select some text and run the macros on the selected text instead of the remainder of the document.

The following works. The problem is that there is no way of telling how many times the macros are going to run before the end, so counters are not an option. Each macro grabs a portion of text based on its contents and processes it. The insertion point then moves past the processed section.

Code:
Sub GetFuzzies()

Do
Application.Run MacroName:="macro1"
Application.Run MacroName:="macro2"
Loop

End Sub

Thanks a lot for any help!

Chavo
 



Do you think that it might be a good idea to post your macro code?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
You need to know where you are in the document in order to be able to tell when you are at the end. Nothing in the little bit of code has that information - how do macro1 and macro2 know where they are? As Skip says, for anybody to help you have to give a little bit more detail.

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
 
If possible and easy to do, I would like to also have the option to select some text and run the macros on the selected text instead of the remainder of the document."

Use: Selection.Text

Other than that, Skip and Tony are right, we need much more information than you are giving.

faq219-2884

Gerry
My paintings and sculpture
 
Hi guys,

Thanks for the input. The problem is that I can't send you the macros' code. They are in a locked project by another engineer. I only know I can access them in this way.

So, there isn't much other info I can give you.

@Fumei: I did find the Selection.Text option, but I do not know how to apply the macros to the selection.

Chavo
 
Before turning to this site, I have been looking at three options, none of which I have been able to get working:

- Finding a coded method of recognizing the end of the document
- Checking whether there is text to the right of the insertion point before calling the first macro
- Adding a final marker at the end of the document and checking whether it still is in the remainder before calling the first macro

Any of these options are fine, but the first two would be best because they don't modify the original document.

Chavo
 






"So, there isn't much other info I can give you."

So there isn't much other help I can offer you.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hi Skip,

I do appreciate your effort to help me. Is there any other information that I might be able to give to get started?

The best working option I got until now was this:

Code:
Sub GetFuzzies()
Dim i As Long
i = 1

Do Until i > ActiveDocument.Sentences.Count
    Application.Run MacroName:="macro1"
    Application.Run MacroName:="macro2"
    i = i + 1
Loop
End Sub

Although the amount of sentences is not equal to the amount of loops required, the difference is small enough for short documents. However, in 80 page documents, the discrepancy increases to such an extent that it is no longer a workable option.

Chavo
 
This is crazy - tell your boss it's crazy. You have to know something about the macros.

What do the macros do? How do they know when they've got to the end of the document? What do they do when they've got to the end of the document?

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
 
Haha.. I could tell my boss it's crazy: in a mirror. I am my own boss and use a commercially available tool for translating. The tool has a Word template that interacts with my translation software.

The first macro selects a chunk of text to be translated, starting at the insertion point and ending at a specific point, based on several rules (more or less a sentence), and then goes on to the translation software to check if there is a similar or identical sentence with its translation available. If so, it is copied into the document.

The second macro confirms the translation without storing it back to the translation software.

As I use various tools, I want to be able to process an entire document with a single command before changing to a different tool. This option is not provided with the commercial software, so I am trying to get it to work myself.

I'm checking out now as it's 10:30 PM in CET.

Regards,

Chavo
 
Hi Chavo,
The first macro selects a chunk of text to be translated, starting at the insertion point and ending at a specific point, based on several rules (more or less a sentence
How does the first macro 'select' its chunk of text? Do you make the selection, or is the selection process automated in some way? What are these 'several rules' you mention?

Allowing for the possibility that your macros change the sentence count (and in the absence of anything more useful to work with), perhaps you could use something like:
Code:
Sub GetFuzzies()
Dim i As Long
For i = ActiveDocument.Sentences.Count to 1 Step -1
    Application.Run MacroName:="macro1"
    Application.Run MacroName:="macro2"
Next
End Sub
Cheers

[MS MVP - Word]
 
Hi Macropod,

After a good night sleep and a new golden idea I finally got it to work, at least, in a basic way.

The following code extends the selection to see if it's possible. If it can be extended by 3 characters, the Loop knows it's not at the end of the file. If not, the file end has been reached and the loop exits. This doesn't allow for selecting a chunk of text before running, but it will do for now.

Code:
Sub GetFuzzies()

Selection.MoveEnd Unit:=wdCharacter, Count:=3

Do Until Selection.Characters.Count < 3
    Application.Run MacroName:="macro1"
    Application.Run MacroName:="macr02"
    Selection.MoveEnd Unit:=wdCharacter, Count:=3
Loop
End Sub

As for your question, the first macro makes its own selection. I just put te insertion point anywhere in the file and it will proceed from there. It has a mechanism (unknown to me) to determine whether there is anything to the right of the insertion point. If there is, it runs. If not, it doesn't. The rules are based on the encountered characters. The selection is extended to (more or less) the first [^t^p:.?!] to the right of the insertion point.

Hopefully this makes it all a bit clearer. I find it hard to explain what happens because I see it happening 100s of times each day. The macros do indeed increase the sentence count: the source chunk is hidden and the target chunk is pasted into the file, both surrounded with markers to identify them.

Chavo
 
If your macros work with the insertion point and you don't change that as you're progressing, it follows that they change it. Assuming all the work happens in the same window, you can check the insertion point (Selection.End).

The implication of you not knowing is that the macros don't pass any indication of success or failure so, how you will know when you've reached the end is for you to experiment a little. Perhaps the insertion point is left unchanged - in which case Selection.End before the call and after the call wil be the same. Or perhaps it leaves it at - or near - the end of the document: ActiveDocument.Range.End - perhaps ActiveDocument.Range.End-1 - as I said, it's going to need some trial and error.

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
 
As for your question, the first macro makes its own selection."

This is impossible. Literally. Macros do not make their "own" selection. Macros do, and ONLY do, what they are told to do...by a programmer.

There [/b]must[/b] be rules of logic in there. If you do not know what they are - and apparently you do not - then it is difficult to help.

The original question was regarding continuing until the end of the document. This is a normal and very common requirement. I do it all the time. The procedure is fairly easy to accomplish....and if we had an idea of what is trying to be accomplished, it would be easy to say so. You state:

"The following code extends the selection to see if it's possible. If it can be extended by 3 characters, "

with no explanation whatsoever. WHY 3 characters????? Why not, 6??? Or 7? Or 2? Or 1? No explanatuon of the logic.

You have these three "requirements":

- Finding a coded method of recognizing the end of the document
- Checking whether there is text to the right of the insertion point before calling the first macro
- Adding a final marker at the end of the document and checking whether it still is in the remainder before calling the first macro

The end of the document...is the end of the document. One "coded" infoirmation is ActiveDocument.Range.End. This is simply a number. The end of the document. However, with good coding, this is rarely needed. You use a Do While statement actioning on the Range of the document. It will processing until the end, and stop.

As for checking if there is text to the right of the insertion, that is simple. Check the insertion (Selection) .End number, and if it is less than the document Range.End...yes, there is text to the right.

Ihave to say that using Selection (the insertion point) is NOT a good way to process documents. Range is far better.

faq219-2884

Gerry
My paintings and sculpture
 
Hi Chavo,
I was looking for similar process of running macro to the end of document and I combined yours with some other property. This worked out for me.
Code:
Dim i As Long
i = 1
Do Until i > ActiveDocument.BuiltInDocumentProperties("Number of Pages")

`Your Stuff

    i = i + 1
Loop
 
shinbi,

That is as arbitrary as Chavo's check on sentences. It may or may not work depending on the relationship between the process and a page of the document.

Chavo,

Do your macros change the text at the insertion point, or just at some point forward from it? Or do they leave the insertion point just after some identifiable text. I'm wondering if you can check for the markers you say are added to tell whether or not they have run, assuming that having run means, effectively, not finished yet.

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
 
Hi Chavo,

there are two possiblities I can see so far.

1) If you are working with selection, you could make use of the "Range":
Code:
Do
...
Loop until Selection.Range.End=ActiveDocument.Range.End

2) I know quite a few CAT tools and usually they all have a "pretranslate" option which seems to be what you are trying to re-program. Can't you just make use of the built-in functions of that CAT tool?

Cheers,
MakeItSo

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
P.S: I am a localisation engineer. If you told me what exactly you are trying to perform and with which CAT tool I might be able to provide more detailed help.
;-)

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Hi MakeItSo,

The trouble with checking selection against end of document is it assumes that the invoked macros put the selection there when they have finished, and nothing Chavo has said so far suggests tha that is the case.

I do agree it could help to know what the package is - quite often they have trial downloads.

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
 
hi Tony,

Chavo said, it's a translation tool. These tools usually work with the clipboard, i.e. the copy the currently selected segment to the clipboard, query the clipboard contents against the translation memory (database) and the paste the translated contents back from the TM if a translation was found.
The reason for this seemingly unnnecessarily longwinded procedure is simple: they don't want to lose formatting information, as this is also stored in the translation database.

Hence the selection **should** be were the current cursor position/selection is, only extended until the next end-of-segment marker, which is e.g. "." ";" ":" or end of paragraph.

Now that I talk of it, this might be closer to a functioning check in this case:
Code:
Loop Until Selection.Range.Sentences(1).End[b]+1[/b]=ActiveDocument.Range.End
'extend by trailing paragrpah mark which is a sentence of its own

Still not all too reliable for my taste. Need more info on what Chavo actually wants to achieve.

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top