2010-06-17 88 views
2

我在Delphi 7中打開EXCEL 2007時遇到困難它適用於Office 2003及以下版本,但微軟的精彩人物已發送更新或其他內容,並且delphi應用程序剛剛崩潰月。Delphi 7和Excel 2007打開文件錯誤

oE := GetActiveOleObject('Excel.Application'); 
    oE.Workbooks.Open(Filename:=sFilename, UpdateLinks:=false, ReadOnly:=true); //Error 

我得到以下錯誤:

'c:\Temp\Book1.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct.'#$A#$A'If you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted'

然而,如果我在VBA運行相同的命令是沒有問題的。

+0

什麼是您用來打開該文件的VBA腳本? – 2010-06-17 08:44:29

+0

在Excel宏中: Application.Workbooks.Open「C:\ Temp \ Book1.xls」,false,true – Traci 2010-06-18 00:45:11

回答

2

我知道這聽起來很愚蠢,但是您是否手動確認文件存在於該位置?

sFileName的內容到底是什麼,它是完整路徑還是隻有文件名?當它只是文件名時,也許Excel無法找到它,因爲它的當前工作目錄是別的。如果您只傳遞文件名,請改爲使用完整路徑。

+0

是的,我將該值複製到equivilent VBA函數中,並按預期方式打開文件完整路徑和文件名 – Traci 2010-06-17 07:26:44

+0

@Traci :當您將Books1.xls放入C:\,並將您的調用更改爲'oE.Workbooks.Open(Filename:='c:\ Books1.xls',UpdateLinks:= false,ReadOnly:= true)時會發生什麼情況; ? – 2010-06-17 08:04:54

+0

不知何故,文件名有一個空間,我修剪它,一切OK – Traci 2010-06-22 00:34:07

1

的完整代碼的旁觀者:

uses ComObj; .. 

procdure startExcel; 
var 
    oE:Variant; 
begin 
    try 
    oE := GetActiveOleObject('Excel.Application'); 
    except 
    oE := CreateOleObject('Excel.Application'); 
    end; 
    oE.Workbooks.Open(filename, false, false); 
    oE.Visible := True; 
end; 

source