PDA

View Full Version : Word 2007 crashes on obj.selection.find from Access...



muzicman0
12-14-2006, 09:51 PM
Hi everyone,
Not sure if I should post this in the Access forum, or here, but since it seems to be a word issue, I'll start here...

The below code is actually from an access DB that was developed with the 2003 version. I recently upgraded to 2007, and now the code causes Access to crash. Wordobj is a module level word object. Basically, the design is to load a word document, scan for specific text (IE: 'Name:'", then I use extend to expand the selection to the full line (which would be something like "Name: Steve"), then get rid of the extra, so I am only left with 'Steve'. The function below actually is the code that scans the doc. (hope that made sense...):



Function txtParse(txtToFind As String) As String
Dim nAdd As Integer
nAdd = 1
If txtToFind = "Name:" Or txtToFind = "Address:" Then nAdd = 2
WordObj.Selection.ClearFormatting
With WordObj.Selection.Find
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
.Execute FindText:=txtToFind
End With
WordObj.Selection.Extend
WordObj.Selection.Extend
txtParse = Right(WordObj.Selection, Len(WordObj.Selection) - Len(txtToFind) - nAdd)
End Function


What I have tried so far is to narrow down the crash to this line '.MatchWholeWord = True', however, just for a test, I commented out that line, and it crashed on the next line.

I have also created a macro in Word that does basically what I am looking to do, to see what code was generated, and made changes to my code. This resulted in Access locking up.

Anybody have any ideas of either a workaround or a fix? Or do you see something that I could change in my code?

Thanks for any help!
mm0

fumei
12-19-2006, 09:04 AM
Could you describe "crash" in more detail? I used the same code (pretty muc, I am not using a Word instance, but Word itself, so I removed Wordobj). Withthe following as sample text:

Adjaldalda
Lajdalda;d

adjaldad

Name: Steve

Fredlkfsfs;fs

Address: Bob

The following code works.Sub Test()
MsgBox txtParse("Address")
End Sub

Function txtParse(txtToFind As String) As String
Dim nAdd As Integer
nAdd = 1
If txtToFind = "Name:" Or txtToFind = "Address:" Then nAdd = 2
Selection.ClearFormatting
With Selection.Find
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
.Execute FindText:=txtToFind
End With
Selection.Extend
Selection.Extend
txtParse = Right(Selection, Len(Selection) - Len(txtToFind) - nAdd)
End Function
Putting in "Addres" returns "Bob"; putting in "Name" returns "Steve".

I am not sure I would use Selection.Extend. Selection.Expand may be better.

I am also assuming that these items are in separate paragrpahs.

muzicman0
12-19-2006, 09:08 AM
Thanks for checking for me...just as a side note, I can get it to work from word also, it is only if I do it from access that it crashes...

by crash I mean that I get an error box pop up that there was a serious error, and the program terminates.

I have also tried some alternate code (I made a macro in word, and just copied the code that was created, which was slightly different)...this resulted in a complete lock-up of access, which I had to cntrl-alt-del out of.

Norie
12-19-2006, 09:44 AM
If you are running this from Access where are you setting/creating
WordObj?

muzicman0
12-19-2006, 09:50 AM
It is created in the same module, and is set as a public object (declared in the general section)...if you would like, I can post the entire module, there is just a lot of code there, and I have narrowed the crash down to the code above...if you would like me to post, just let me know...
mm0

muzicman0
12-19-2006, 03:04 PM
As a work around for now, I have installed word 2003 also, just to use the word 11 object, and it seems to work, while still allowing me to open the new version of word for everything else, but I would sure like to know what is causing this problem...hmmm.....