# (VB) Open file by double clicking in the fileListBox



## Anas (Jan 19, 2005)

Hi guys;

I'm working on my final year project and I need your help. I'm trying to open a word document from a VB application. I'm using a fileListBox. I want the file to open in Word when dblClicked. How can it be done?


----------



## Mosaic1 (Aug 17, 2001)

Use Shell to specify which program to use to open the file.


----------



## Gazornenplat (Dec 30, 2004)

What version of VB?

Ian


----------



## Anas (Jan 19, 2005)

Hi Ian;
I'm using VB 6.0


----------



## Gazornenplat (Dec 30, 2004)

Hi,

Shame, you can do good stuff in .net :-(

Shell works, though, as mosaic says.

Shell "c:\windows\notepad.exe c:\xxx.txt"

will fire up notepad with c:\xxx.txt loaded into it

Shell "c:\windows\notepad.exe c:\xxx.txt", vbMaximizedFocus

notepad maximised and with focus

Shell "c:\windows\notepad.exe c:\xxx.txt /c", vbMaximizedFocus

Note the /c which means that your calling program returns immediately and doesn't wait for notepad to be closed (this is almost undocumented and drove me up the wall many years ago!)

or you can get the task handle with:

x = Shell("c:\windows\notepad.exe c:\xxx.txt /c", vbMaximizedFocus)

and use SendKeys to control the program

HTH

Ian


----------



## Mosaic1 (Aug 17, 2001)

And another problem with Shell is the Quotation marks. They can give you fits. 
I take it you don't have anything in the click event. 

If you actually are opening a Word Document and doc is associated with Word, And Word is installed on the System, (if others are going to use it) then I prefer the API call on this one.

ShellExecute

This will open the file in the app it is associated with.


----------



## coderitr (Oct 12, 2003)

Gazornenplat said:


> Note the /c which means that your calling program returns immediately and doesn't wait for notepad to be closed (this is almost undocumented and drove me up the wall many years ago!)


The default behavior of Shell in VB6 is to return to the caller without waiting for the Shell'ed program to close.


----------



## Gazornenplat (Dec 30, 2004)

Ok, so it must have been the other way around. It had been a long day, and it was a long time ago


----------



## Anas (Jan 19, 2005)

Thank you all guys;
The solution that Mosaic1 suggested seems to be what I'm looking for. I'll try it.


----------



## Rollin_Again (Sep 4, 2003)

Shell will only open the document. If you use early binding and set a reference to the Word Library, you can create an Instance of Word to open the document and completely control it. By using early binding, you will be able to use Intellisense to access the documents properties and methods.

Rollin


----------



## Anas (Jan 19, 2005)

Hi Rollin;
Wouldn't setting a reference to the Word Library affect the portability of the application?
This seems to be a long process knowing that I want to open different file types, which means that I'll have to do the same with every file type I want my application to open.

Anas


----------



## Rollin_Again (Sep 4, 2003)

Yes, there may be some portability issues with setting references to specific libraries. You can always use late binding without setting reference to any libraries.


```
Dim objXL As Object
Set objXL = CreateObject("Excel.Application")
objXL.Visible = True
objXL.Workbooks.Open ("C:\YourWorkbookName.xls")
' Add more code
```


```
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open ("C:\YourDocumentName.doc")
'Add more code
```
Rollin


----------

