Years ago I’ve written a Delphi 6 application that uses OLE automation to generate content in Excel and Word. This code functioned correctly for years on older systems, but fails on my current Windows 10 machine with Office 2016 with the error "Interface not supported".
Environment: Delphi 6 Enterprise, Windows 10 Pro (Version 21H2), Microsoft Office Professional Plus 2016
My full code is too long to paste here verbatim so I’ve extracted a sample that’s sufficient to demonstrate a failure scenario. The error occurs when invoking the CreateOleObject
line.
unit Unit1;
interface
uses
Windows, ComObj, ActiveX, Variants, SysUtils, Dialogs;
procedure OpenExcelAndWordWithText;
implementation
procedure OpenExcelAndWordWithText;
var
ExcelApp, Workbook, Worksheet: Variant;
WordApp, Document: Variant;
begin
CoInitialize(nil);
try
try
ExcelApp := CreateOleObject('Excel.Application');
ExcelApp.Visible := True;
Workbook := ExcelApp.Workbooks.Add;
Worksheet := Workbook.Worksheets[1];
Worksheet.Cells[1, 1] := 'Hello world';
except
on E: Exception do
begin
ShowMessage('Excel Error: ' + E.Message);
Exit;
end;
end;
try
WordApp := CreateOleObject('Word.Application');
WordApp.Visible := True;
Document := WordApp.Documents.Add;
Document.Content.Text := 'Hello world';
except
on E: Exception do
ShowMessage('Word Error: ' + E.Message);
end;
finally
CoUninitialize;
end;
end;
end.
How can I fix the issue?
The problem was that the previous version of Excel was uninstalled incorrectly on the machine. To fix it, I used the following instructions from Microsoft:
HKEY_CLASSES_ROOT >> TypeLib >> {00020813-0000-0000-C000-000000000046}
(The Excel PIA key is {00020813-0000-0000-C000-000000000046}
)A similar fix can be applied to other Office products:
Excel
HKEY_CLASSES_ROOT\TypeLib{00020813-0000-0000-C000-000000000046}\
- 1.7 is for Office 2010
- 1.8 is for Office 2013
- 1.9 is for Office 2016
Word
HKEY_CLASSES_ROOT\TypeLib{00020905-0000-0000-C000-000000000046}\
- 8.5 is for Office 2010
- 8.6 is for Office 2013
- 8.7 is for Office 2016
PowerPoint
HKEY_CLASSES_ROOT\TypeLib{91493440-5A91-11CF-8700-00AA0060263B}\
- 2.a is for Office 2010
- 2.b is for Office 2013
- 2.c is for Office 2016
Outlook
HKEY_CLASSES_ROOT\TypeLib{00062FFF-0000-0000-C000-000000000046}\
- 9.4 is for Office 2010
- 9.5 is for Office 2013
- 9.6 is for Office 2016
Here's a sample regedit file to fix the Registry for Office 2016:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.9]
"PrimaryInteropAssemblyName"="Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"
@="Microsoft Graph 16.0 Object Library"
[HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.9\0]
[HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.9\0\win64]
@="C:\\Program Files\\Microsoft Office\\Office16\\EXCEL.EXE"
[HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.9\FLAGS]
@="0"
[HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.9\HELPDIR]
@="C:\\Program Files\\Microsoft Office\\Office16\\"
[HKEY_CLASSES_ROOT\TypeLib\{00020905-0000-0000-C000-000000000046}\8.7]
"PrimaryInteropAssemblyName"="Microsoft.Office.Interop.Word, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"
@="Microsoft Graph 16.0 Object Library"
[HKEY_CLASSES_ROOT\TypeLib\{00020905-0000-0000-C000-000000000046}\8.7\0]
[HKEY_CLASSES_ROOT\TypeLib\{00020905-0000-0000-C000-000000000046}\8.7\0\win64]
@="C:\\Program Files\\Microsoft Office\\Office16\\MSWORD.OLB"
[HKEY_CLASSES_ROOT\TypeLib\{00020905-0000-0000-C000-000000000046}\8.7\FLAGS]
@="0"
[HKEY_CLASSES_ROOT\TypeLib\{00020905-0000-0000-C000-000000000046}\8.7\HELPDIR]
@="C:\\Program Files\\Microsoft Office\\Office16\\"