Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word using the same instance problem 1

Status
Not open for further replies.

daimaou

Programmer
Apr 4, 2001
154
PT
I'm having this kinda annoying problem.
I've an OLE Word object on my form, and then I also have the ability to open some documents using the ShellExecute command.
The problem is everytime I press the button to open a word document using ShellExecute the same instance of Word that's being used by the OLE object will be used. This means if I then close MS Word the OLE object will lose it's reference and I'll get an error "The remote server machine does not exist or is unavailable"!!

Now I found out this is a bug:

that has no apparent solution if you launch a document directly from windows explorer(wich seems to be exactly what ShellExecute does).

Anyone has/had this problem and have any ideas at all on how to solve it?

I really need to use ShellExecute because not all the documents are in .doc format.

Thanks.
 
I agree, that is a PITA.

For your case, if the file extension is DOC, do not use ShellExecute to open the document.

If sMyFileExtension = "DOC" Then
Dim oTemp as Object
Set oTemp = CreateObject("Word.Application")
oTemp.Documents.Open(sMyFilePath)
oTemp.Visible = True
Set oTemp = Nothing
Else
'ShellExecute it
End If

However, this does not remedy the problem when the user launches a word document thru Windows Explorer,Desktop shortcut, etc.. while you are automating Word. Jon Hawkins
 
Thanks for the reply Jon.
yeah that solves half of the problem. I think there is no way to avoid the user from opening a word document through Windows Explorer and crash the program though, but this way is better than nothing.
Although on PCs that have files like .rtf, .txt or others associated to MS Word this wouldn't work, that's why I wanted to use only ShellExecute.
Lets wait microsoft develop some fix for this(quickly if possible...).

btw you(or anyone else) know if there is a way of checking to wich program is a file type associated from VB code?
 
You can use the FindExecutable API function:

Public Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long

Dim nRetCode As Integer
Dim sFile As String, sExe As String
Dim nVar As Integer, nVar2 As Integer

sExe = Space(200)
sFile = "C:\my documents\obg.txt"
nRetCode = FindExecutable(sFile, "", sExe)
If nRetCode > 32 Then
nVar = InStr(1, sExe, Chr(0))
nVar2 = InStrRev(sExe, "\", nVar) + 1
sExe = Mid(sExe, nVar2, nVar - nVar2)
MsgBox "Associated Application is " & sExe
Else
MsgBox "Not Assocatiated or File Not Found"
End If Jon Hawkins
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top