VB appl crashes on opening word doc

V

vonclausowitz

Hi All,

I have a VB application which opens a word file to search for some text
strings.
At home there is no problem but when I try this code at work on a
win2000 machine with office
2000 installed it crashed somewhere at this line:

Do Until sFile = ""
sFileoud = sFile
Tekst1.Text = sFileoud
Tekst3.Text = ""
Me.Refresh
lCounter = lCounter + 1
Set olWordDocs = olApplication.Documents.Open(FileName:=oDisk + sFile,
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:=
_
wdOpenFormatAuto)

If olApplication.ActiveDocument.ProtectionType <>
wdNoProtection Then
olApplication.ActiveDocument.Unprotect
End If

olApplication.Selection.Find.ClearFormatting
With olApplication.Selection.Find
.Text = "MSGID"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
olApplication.Selection.Find.Execute
olApplication.Selection.MoveRight Unit:=wdCharacter,
Count:=3
olApplication.Selection.EndKey Unit:=wdLine,
Extend:=wdExtend
sRefnr = ""
sRefnr = olApplication.Selection



Does anyone know what is wrong here?

Regards
Marco
 
K

Kodeworks

Marco

Are you running a more recent version of Word on the home computer? If
you are, then it's likely your application is failing on

Set olWordDocs = olApplication.Documents.Open .............

To fix this you need to use late binding if you want to continue to
develop on your home computer. If you develop your code on the work
computer, then it will probably run fine on your home computer. You'll
find an explanation of the how's and why's here
http://support.microsoft.com/default.aspx?scid=kb;en-us;245115

Sunil Jadwani
Kodeworks - Business Automation Solutions
www.kodeworks.com
 
J

Jezebel

If you switch to late binding, you'll also need to deal with all those Word
constants (wdCharacter, wdFindContinue, etc) which will no longer be
defined.
 
V

vonclausowitz

You mean defining variables as objects?
Like this:

Dim olApplication As Object
Dim olWordDocs As Object

Marco

Kodeworks schreef:
 
J

Jezebel

Late binding means you remove the reference to Word from the project
references, and instead use

Dim olApplication as Object
Set olApplication = CreateObject("Word.Application")

The problem you might be having at the moment is that your app is compiled
to run with one version of Word (Word 10 or 11, whatever it shows as the
library reference) and fails on a computer that has a different library.
 
V

vonclausowitz

How can that be solved?
Exept of course doing the programming on the other machine.

Marco
 
E

Ed

Pardon me for jumping in - but once you do pick up the object and bind to
Word, don't the constants get their definition? Maybe I've just been lucky
so far, but I've not had any problems writing in early binding to use the
IntlliSense and then switching to late binding to run. Such as (code
snipped to relevant bits):
Dim WD As Object
On Error Resume Next
Set WD = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WD = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

WD.Documents.Open doc
WD.Visible = True
WD.Documents(doc).Windows(1).View.Type = wdPrintView

Ed
 
J

Jezebel

By using late binding, as described. Late binding uses whatever version of
the library is actually available on the machine. You have to write your
code using the earliest version that you want to support; and to be thorough
you should also check the version when you first instantiate the object, and
exit if it's a version the app doesn't support.
 

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