Word VBA Hints

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Word VBA Hints

Post by agibsonsw »

Hello. Word 2003.

It must be a year since I've dabbled with Word VBA and I'm just looking for a couple of pointers in the right direction :smile:

1) How can I retrieve the 'index' of an object within a collection? A paragraph doesn't seem to have an Index property?

Code: Select all

Sub TestP()
    Dim p As Paragraph
    For Each p In ActiveDocument.Range.Paragraphs
        MsgBox p.Something ?
    Next p
End Sub
2) 'Selection' doesn't seem to have an 'applies to' as such so I suppose it always assumes the ActiveDocument. How do I obtain the Selection for a non-active document?
3) I understand that 'ThisDocument' can refer to the document or it's template. How could I reliably refer to the code's document? ActiveDocument may not always be reliable either.

Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78490
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word VBA Hints

Post by HansV »

1. You could use

Code: Select all

For i = 1 To ActiveDocument.Paragraphs.Count
    MsgBox i
Next i
:grin:

Slightly more seriously, if you have a Range object rng and you want to know which paragraph contains its end:

ActiveDocument.Range(Start:=1, End:=rng.End).Paragraphs.Count

2. Selection is (among others) a property of a Window, so you could use Documents(2).Windows(1).Selection to refer to the selection in the (first) window on another document.

3. It's best to set a variable of type Document at a moment when you "know" which one it is, e.g. if you call a macro from within a document:

Dim docCur As Document
Set docCur = ActiveDocument
...
...
' docCur will still refer to the original document even if it is not the active document any more

Or if you want to refer to a document that you create:

Dim docNew As Document
Set docNew = Documents.Add
...
...
' docNew will still refer to the new document even if it is not the active document any longer.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Word VBA Hints

Post by agibsonsw »

@Hans. Hi and thank you.

1) So a paragraph (and a sentence, etc.) doesn't have an 'Index', unlike Excel objects? I ended up using an artificial index variable x, with 'x = x + 1' within my loop.

2) Word Help wasn't very helpful in letting me know that 'Selection' applies to a Window object :smile:, so thank you. I was (eventually) looking for a way to avoid 'Selection' as much as possible: I now understand that 'Range' is the way to go http://msdn.microsoft.com/en-us/library ... 11%29.aspx.

3) I had already considered the two code snippets you posted :smile: (although I used the variable name 'newDoc' he, he). I was then considering examining the 'AttachedTemplate' but this would still carry the same concerns. I was arriving at the solution you indicated: use the triggered event to store a reference to the ActiveDocument.
But if code is called from a toolbar or macro then the issue arises again :scratch:. I suppose in this instance the nearest I could get would be to check the 'AttachedTemplate' and if it's not the correct one then to check this for each open document - then to store a reference to the found document.

The only other approach that (just) occurs to me is to use the New event for the template to set a global variable. I wonder how this is referenced(?) - possibly
Documents(x).MyGlobalVar
One would think there should a simple (or simpler) solution to this.. Regards, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78490
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word VBA Hints

Post by HansV »

Each application has its own idiosyncracies, nice and not so nice.

Access, for example, has the very useful CodeContextObject - the object (form or report) from which the code is run. If you have a procedure in a standard module, and you call it from a form, that form will be the CodeContextObject even though the code is in a standard module, not in the form module.

When you call code from a toolbar, QAT or ribbon button, you may assume that it is to run on the ActiveDocument, so again I'd assign ActiveDocument to a variable at the beginning of the code.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Word VBA Hints

Post by agibsonsw »

When you call code from a toolbar, QAT or ribbon button, you may assume that it is to run on the ActiveDocument, so again I'd assign ActiveDocument to a variable at the beginning of the code.
I suppose we have to assume a minimum level of intelligence from our users at some point :grin:
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78490
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Word VBA Hints

Post by HansV »

No, that would be a false assumption :evilgrin: but on the other hand, it's hard to call a toolbar button from a non-active document...
Best wishes,
Hans