Home All Groups Group Topic Archive Search About

Word can't open a file in VB6

Author
22 Sep 2005 12:09 AM
inquirer
I have a small VB6 application which shells to a program that will
decrypt an encrypted file and then open the decrypted file in Word 2003.
The program was working without a problem until a few days ago when it
started giving runtime error 5121 when it tried to open the file. The
lines of code involved are :

' decrypt temporary file file with filecode
   Shell "d:\filecode\filecode.exe /p " & S$ & " /f ""temp1.doc"" /s
temp1.doc /d"
'Create an instance of MS Word
    Set oApp = CreateObject(Class:="Word.Application")
    oApp.Visible = True
'Open the Document
    oApp.Documents.Open FileName:="d:\filecode\testdata\temp1.doc"

The runtime error occurs at the oApp line.

If I exit the program, I can then open temp1.doc in word without any
problems.

As far as I know, I have done nothing over the last few weeks that would
cause the application to stop working.

Could it be that the shell command still has temp1.doc "open" and so
word cannot open it? If so, how can I "close" it?

Searching the web suggest that antivirus programs could cause this
error. So i tried running the program with my AV program (F-Prot)
disabled but the problem did not go away.

Any suggestions on how to solve this or bypass the problem much appreciated
Thanks
Chris

Author
22 Sep 2005 12:23 AM
Bob Butler
Show quote Hide quote
"inquirer" <nore***@noreply.com> wrote in message
news:4331f652$0$1909$afc38c87@news.optusnet.com.au
> I have a small VB6 application which shells to a program that will
> decrypt an encrypted file and then open the decrypted file in Word
> 2003. The program was working without a problem until a few days ago
> when it started giving runtime error 5121 when it tried to open the
> file. The lines of code involved are :
>
> ' decrypt temporary file file with filecode
>    Shell "d:\filecode\filecode.exe /p " & S$ & " /f ""temp1.doc"" /s
> temp1.doc /d"
> 'Create an instance of MS Word
>     Set oApp = CreateObject(Class:="Word.Application")
>     oApp.Visible = True
> 'Open the Document
>     oApp.Documents.Open FileName:="d:\filecode\testdata\temp1.doc"
>
> The runtime error occurs at the oApp line.
>
> If I exit the program, I can then open temp1.doc in word without any
> problems.
>
> As far as I know, I have done nothing over the last few weeks that
> would cause the application to stop working.
>
> Could it be that the shell command still has temp1.doc "open" and so
> word cannot open it? If so, how can I "close" it?

Shell is asynchronous so it's very possible that you are opening word before
the decoding finishes.  search groups.google.com for 'vb shell wait' and
you'll find examples on how to wait.  You can also potentially delay briefly
and attempt to open the file exclusively from VB.  Loop doing that until it
succeeds. and then start Word.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
22 Sep 2005 1:49 AM
MikeD
Show quote Hide quote
"inquirer" <nore***@noreply.com> wrote in message
news:4331f652$0$1909$afc38c87@news.optusnet.com.au...
> I have a small VB6 application which shells to a program that will
> decrypt an encrypted file and then open the decrypted file in Word 2003.
> The program was working without a problem until a few days ago when it
> started giving runtime error 5121 when it tried to open the file. The
> lines of code involved are :
>
> ' decrypt temporary file file with filecode
>    Shell "d:\filecode\filecode.exe /p " & S$ & " /f ""temp1.doc"" /s
> temp1.doc /d"
> 'Create an instance of MS Word
>     Set oApp = CreateObject(Class:="Word.Application")
>     oApp.Visible = True
> 'Open the Document
>     oApp.Documents.Open FileName:="d:\filecode\testdata\temp1.doc"
>
> The runtime error occurs at the oApp line.


In addition to what Bob stated about Shell being asynchronous, I see 3 lines
containing 'oApp'.  On which of these lines does the error occur?  Also, you
should always provide the error description IN ADDITION TO the error number.
VB reports error number 5121 as an application-defined or object-defined
error. That doesn't help us at all in knowing what the exact error you're
getting really is.

--
Mike
Microsoft MVP Visual Basic