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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening Excel Documents

Status
Not open for further replies.

mmor10

MIS
Jan 6, 2004
5
US
Does anyone know of a way to open Excel documents through Visual Basic when the path contains more than one space? When I try opening a document where there is more than one space in the pathname I receive the following error, File could not be found. Check the spelling of the of the file name... But, the file is really in that location. When I rename the file and subfolders to contain no spaces or replace the spaces with underscores the file opens just fine. If anyone knows a way around this, I'd greatly appreciate any help. Thanks. I am currently using the following function to open the file.

Dim ExcelApp As Object
Set ExcelApp = CreateObject("Excel.Application")ExcelApp.Visible = True
ExcelApp.Workbooks.Open txtFilename.Text
 
hmmm bizarre!

it should work!

are you positive the path is correct?

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
To get the best response to a question, please check out FAQ222-2244 first
A General Guide To Excel in VB FAQ222-3383
 

There is always the API, although I agree with ADoozer in that it should work but via this function it should remove all spaces ...

[tt]
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long

Dim RetVal As Long, StringLength As Long
Dim S As String, OrigString As String

OrigString = String(255, 0)
OrigString = "D:\Program Files\Microsoft Visual Studio\this is a test.txt"

S = String(255, 0)
StringLength = 255

RetVal = GetShortPathName(OrigString, S, StringLength)

Debug.Print Left(S, InStr(1, S, Chr(0)) - 1)

[/tt]

Good Luck

 
Yes I agree, it's the strangest thing. I actually use a drive list, directory list, and file list that I use together to browse to the file. Plus, when I copy the file name from the text box in visual basic and past it in the 'Run' window under the start menu, it opens the file just fine. It just doesn't make sense to me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top