Word 2003 Selection object error (VBA)

C

Crirus

Hello!

I have a macro that use selection find to do some automate replace on a
document, called First.doc
Anyway, if I have another document opened (Second.doc) and selection on that
is somewhere in the document...
If I use the macro to replace something in First.doc document, the selection
is reset to the beginning of the document is second.doc wich is
inactive.!!!!


Anyone encountered this error on vba macros when using Selection?
 
M

Malcolm Smith

Are you just using the Selection & Replace as copied from the macro
recorder or have you altered it to work with ranges and tidied it up
yourself?

If you haven't done the latter then I would advise you doing so as the
macro recorder really is only good enough for you to get started and I
wouldn't advise using its output for production code.

- Malc
 
C

Crirus

I work with macros since Word 95 introduced WordBasic..so I'm sure I know
what I'm doing
Just try to open 2 documents and then execute step by step a simple replace
all like this:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "a"
.Replacement.Text = "a"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
 
M

Malcolm Smith

This looks like code coming right from the macro recorder and shoved right
into your code without modification.

I would suggest that you look at this code and make it more bullet-proof;
bear in mind that the macro recorder gives a very exact answer to the
problem and not one which one comes across in real-life.

I always recommend that one gets an IDEA from the macro recorder and then
take that idea and then code it into proper and secure code.

For a start I would look at the Selection object; why not use a Range on
the document in which you are interested in for a start?

- Malc
www.dragondrop.com
 
C

Crirus

Simply because this is the way I coded in XP or 97 and woked just fine..
seems 2003 have lots of annoing bugs not only this.. I'm really disapointed
of it....
 
C

Crirus

It shouldnt mess documents selections... I dont think is ok to force me to
save the selection on a inactive document so this is definetly a bug
I use bookmark as a work around but thre are many other problems with this
version.. not mentioning omnipresent bug since 97 that fail to find
something with Selection.Find sometimes, and the workaround is to search
anithing with find dialog and then selection.Find work just fine
 
M

Malcolm Smith

So, you don't understand the concepts of defensive programming. I just
think that you were very lucky the first time around.

As I said before, never rely on the macro recorder to produce production
code. It will always need more work on the resulting code which worked
for that one single instance only.

For example, does your code check to see that there is actually a document
present?

But, go ahead, shout about bugs in Word. The rest of us will just go on
writing strong and secure code.

- Malc
www.dragondrop.com
 
C

Crirus

Ohh, man!
I tell you one thing... basically I dont record macros.. I write them .. I
live doing that.. is not about defensive code, I know how to work around
that...
Do you think is ok to have 2 documents opened and using selection object on
one of them should reset selection in another?
You will be so calm either if using repace on a doc will replace stuffs in
another? :)
 
J

JGM

Hi Crirus,

All Malcom is trying to do is to point out that the Selection property
should be avoided because it often leads to unexpected results, like what
you are experiencing. Generally, people who post in the NG and who use the
Slection property do so because they use the recorder, or have learned to
write VBA code with the recorder and haven't "graduated" yet.

The selection property cannot be linked directly to a particular document,
as in "Documents(MyDoc).Selection...", unless you use the Window object to
make that connection, which can be messy.
The Find Property can be used with both the Selection and Range properties.
Since Range is very easily linked to a particular document, as in
"Documents(MyDoc).Range...", it seems logical that if you want reliability,
you should try to use the Find property with Range.

I never use the Selction object, unless I really have to, like if I want to
grab an inline shape that was just inserted... even then, it is often
possible to use Range instead.

I do not have Office 2003, but from what I have heard, a lot of "lazy"
programming that we use to get away with under Word 2002 and previous is not
"tolerated" anymore, or instead of making guesses, the code is applied
"literally".

Finally, something puzzled me about your first post, you stated:

"I have a macro that use selection find to do some automate replace on a
document, called First.doc
Anyway, if I have another document opened (Second.doc) and selection on that
is somewhere in the document...
If I use the macro to replace something in First.doc document, the selection
is reset to the beginning of the document is second.doc wich is
inactive.!!!!"

"(Second.doc) and selection on that is somewhere in the document" So, at the
beginning of your code, how do you switch the selection from the second to
the first document (it seems that the cursor is located somewhere n the
second doc, but you want to do a "Replace all" in the first doc, so you have
to change the "Selection." from one window to another...)

Just curious.
Cheers!
 
C

Crirus

Well, in all previous versions, Word linked the selection object to the
active document...
In 2003, seems that Selection object is differently implemented so it cant
be used like old one anymore.
The main problem is that I have to rewrite lots of macros because of this,
and to me, seems logical to have selection related to active doc only
 
C

Cindy M -WordMVP-

Hi Crirus,
Well, in all previous versions, Word linked the selection object to the
active document...
In 2003, seems that Selection object is differently implemented so it cant
be used like old one anymore.
The main problem is that I have to rewrite lots of macros because of this,
and to me, seems logical to have selection related to active doc only
I agree it seems strange. Ever since Word 2000 came out, with the pseudo-SDI
interface, quite a number of things having to do with "which document is
active" have been quirky. Some people run Find/Replace (in the UI) and the
document display disappears. Probably related to that, I've seen Word put an
additional W-icon in the Windows taskbar (and dipslay a blank window). And
there cases where documents started up from Explorer are visible, but don't
have the focus (you can't start typing), requiring a click on the taskbar
icon to make them "live".

I suspect the problem you're seeing may not be inherent to Word 2003, but a
combination of your configuration plus whatever your entire code is doing.
FWIW, I took your sample code and tested it, and the selection remained in
the "ActiveDocument".

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

In 2002, the following macro does what it is intended to do with the lines
of code commented out as indicated:

In 2003, only the first Find Replace is executed on the active document as
the focus moves to another open document. In fact, clicking on the scroll
bar in the "active document" activates the other document which has assumed
the focus.

If the lines that are commented out are uncommented, so that the active
document is "activated" before each Find Replace, the macro does what it is
intended to do in Word 2003.

Alternatively, if all of the With Selection.Find are replaced by With
ActiveDocument.Content.Find, the full code is executed on the active
document:

' Dim mydoc As Document
' Set mydoc = ActiveDocument
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Execute FindText:="(OH )([0-9)]{5})^13(SSN:
[0-9]{3}-[0-9]{2}-[0-9]{4}^13)", MatchWildcards:=True,
ReplaceWith:="\1\2^p^p", Wrap:=wdFindContinue, Forward:=True,
Replace:=wdReplaceAll
End With
' mydoc.Activate
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Execute FindText:="[0-9]{3}-[0-9]{3}-[0-9]{4}^13",
MatchWildcards:=True, ReplaceWith:="^p", Wrap:=wdFindContinue,
Forward:=True, Replace:=wdReplaceAll
End With
' mydoc.Activate
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Execute FindText:="^p^p", MatchWildcards:=False, ReplaceWith:="##",
Wrap:=wdFindContinue, Forward:=True, Replace:=wdReplaceAll
End With
' mydoc.Activate
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Execute FindText:="(OH )([0-9)]{5})^13", MatchWildcards:=True,
ReplaceWith:="\1\2##", Wrap:=wdFindContinue, Forward:=True,
Replace:=wdReplaceAll
End With
' mydoc.Activate
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Execute FindText:="^p", MatchWildcards:=False, ReplaceWith:="^t",
Wrap:=wdFindContinue, Forward:=True, Replace:=wdReplaceAll
End With
' mydoc.Activate
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Execute FindText:="##", MatchWildcards:=False, ReplaceWith:="^p",
Wrap:=wdFindContinue, Forward:=True, Replace:=wdReplaceAll
End With
' mydoc.Activate
' mydoc.Range.ConvertToTable Separator:=wdSeparateByTabs, NumColumns:=5

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
 
C

Cindy M -WordMVP-

Hi Doug,

Except that I'm not seeing the problem on my installation. I set up a test
document that includes a couple of the "Find" things in your code and it all
runs in the one document. No focus in the second document, nothing
found/replaced in the second document.

this may be a silly question, but: Do both you and Crirus have the full
release version installed, and not a beta release?

Is Word being used as email editor?
In 2002, the following macro does what it is intended to do with the lines
of code commented out as indicated:

In 2003, only the first Find Replace is executed on the active document as
the focus moves to another open document. In fact, clicking on the scroll
bar in the "active document" activates the other document which has assumed
the focus.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Cindy,

I am using 11.5604.5703 and Word is not the email editor.

The macro was developed in response to a post in the vba.beginners with the
subject Find and Insert not Replace where the person wanted to convert the
following into a mailmerge datasource for just the addresses (No SSN's or
phone numbers). Nothing was replaced in the second document as the find
string was not there. I discovered and overcame the problem (by inserting
the mydoc.Activate) before I had developed it as far as converting the text
to a table. On coming across this post, I swapped harddrives so that I
could test it in XP and was unable to reproduce the problem there.

Teresa Renee Fuller
2100 F Dornbin Drive
Reynoldsburg, OH 43068
SSN: xxx-xx-1432
Asst US Trustee
Office of the US Trustee
170 North High Street
Suite 200
Columbus, OH 43215
614-469-7411
Anthony D Phillips
1202 Cattail Road
Chillicothe, OH 45601
SSN: xxx-xx-3026
Karen Phillips
1202 Cattail Road
Chillicothe, OH 45601
SSN: xxx-xx-1612
Asst US Trustee
Office of the US Trustee
170 North High Street
Suite 200
Columbus, OH 43215
614-469-7411
Michelle Santucci Scott
1308 Hideaway Woods
Westerville, OH 43081
SSN: xxx-xx-0904
Asst US Trustee
Office of the US Trustee
170 North High Street
Suite 200
Columbus, OH 43215
614-469-7411
Theodore M Ahrens
7858 Rock Hollow Ln
Malta, OH 43758
Eleanor D Ahrens
7858 Rock Hollow Ln
Malta, OH 43758
Asst US Trustee
Office of the US Trustee
170 North High Street
Suite 200
Columbus, OH 43215
614-469-7411

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
 
C

Crirus

The selection remained there but the selection in the other document has
changed the position so I need a bookmark on inactive document to restore
the selection
 

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