Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 使用Delphi的OleObject在Office 365和Office 2013中的工作方式不同_Excel_Delphi_Delphi 2010 - Fatal编程技术网

Excel 使用Delphi的OleObject在Office 365和Office 2013中的工作方式不同

Excel 使用Delphi的OleObject在Office 365和Office 2013中的工作方式不同,excel,delphi,delphi-2010,Excel,Delphi,Delphi 2010,我正在开发一个使用Delphi Pascal打开XLSX文件的小工具,并在上面写一个单元格。它在使用Office 2013和Office 365的计算机上的行为不同 代码如下: var ExcelApp: OleVariant; anExcelFileName: String; begin try ExcelApp := CreateOleObject('Excel.Application'); anExcelFileName := 'D:\sample.xlsx';

我正在开发一个使用Delphi Pascal打开XLSX文件的小工具,并在上面写一个单元格。它在使用Office 2013和Office 365的计算机上的行为不同

代码如下:

var
  ExcelApp: OleVariant;
  anExcelFileName: String;
begin
  try
    ExcelApp := CreateOleObject('Excel.Application');
    anExcelFileName := 'D:\sample.xlsx';

    ExcelApp.Workbooks.Open(anExcelFileName);
    ExcelApp.Visible := True;

    ExcelApp.Workbooks[1].Sheets[1].Range['A1'].Value := 'HELLO';
  except
    on E: Exception do
      showMessage('Error on something: ' + E.Message);
  end;
end;
在Office 2013中,代码将访问驱动器D中的文件sample.xlsx,打开它,并在单元格A1中写入HELLO

在Office 365中,代码将打开两个文件。首先,它将打开sample.xlsx并打开一个新的空白工作簿,然后在新的空白工作簿中编写HELLO


如何在office 365中获取旧行为?

您的代码失败,因为它假设您打开的工作簿将是工作簿集合中的第一个工作簿,而该假设并不总是成立的

工作簿。打开
返回新打开的工作簿。将该对象用于将来对工作簿的引用。像这样:

var
  ExcelApp, Workbook: OleVariant;
  anExcelFileName: String;
begin
  try
    ExcelApp := CreateOleObject('Excel.Application');
    anExcelFileName := 'D:\sample.xlsx';

    Workbook := ExcelApp.Workbooks.Open(anExcelFileName);
    ExcelApp.Visible := True;

    Workbook.Sheets[1].Range['A1'].Value := 'HELLO';
  except
    on E: Exception do
      showMessage('Error on something: ' + E.Message);
  end;
end;

谢谢你的回答。问题解决了:)您有推荐的url或书籍来解释Excel自动化中的基本内容吗?@WisnuWidiarta不太有。我确实有一本关于OLE自动化的书,但它大约有三英寸厚,而且太重了。最后,我只是从网上取了一些样本,然后玩了起来。一旦你掌握了基本知识,MSDN就是一个很好的资源。