{"id":553,"date":"2012-03-11T16:57:23","date_gmt":"2012-03-11T06:57:23","guid":{"rendered":"http:\/\/www.desiderata.com.au\/blog\/?p=553"},"modified":"2012-04-26T18:38:48","modified_gmt":"2012-04-26T08:38:48","slug":"connecting-to-a-password-protected-microsoft-excel-spreadsheet-using-delphi-tadoconnection","status":"publish","type":"post","link":"http:\/\/www.desiderata.com.au\/blog\/?p=553","title":{"rendered":"Connecting to a password-protected Microsoft Excel spreadsheet using Delphi TADOConnection"},"content":{"rendered":"<p>There is a little trick to connecting to a password protected .xls Excel spreadsheet using the Jet 4.0 OLEDB driver.<\/p>\n<p>&nbsp;<\/p>\n<p>If one uses the obvious Password=xxx;User Id=Admin you will get an error &#8220;Workgroup Information file missing or file is in use by another user&#8221;.<\/p>\n<p>&nbsp;<\/p>\n<p>The ADOConnection will return an active connection is using the following connection string but in Delphi 2007 I am still getting an error on trying to read the file &#8220;Could not decrypt file&#8221;.<\/p>\n<p>&nbsp;<\/p>\n<p>So here is the Connection String which will open the connection successfully in Read Only mode:<\/p>\n<p>&nbsp;<\/p>\n<p><code>Provider:=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\temp\\MyExcel.xls;Mode=Read;Extended Properties=Excel 8.0;Jet OLEDB:Database Password=\"xxx\";<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>It appears that &#8220;Could not decrypt file&#8221; error is due to a well known limitation of the Jet OLEDB 4.0 engine in that it always returns this error on trying to read a password protected Excel file even if passwords are known.<\/p>\n<p>The hack around this is to open the file in Excel first in full read\/write mode BEFORE opening the TADOConnection &#8211; just opening it in READ ONLY mode in Excel WILL NOT work!<\/p>\n<p>This is not only an issue in Delphi but in any application that uses Jet 4.0 OLEDB engine.<\/p>\n<p><strong>Of course, another solution is to use COM OLE automation such as:<\/strong><\/p>\n<ul>\n<li>CreateOLEObject method &#8211; see <a href=\"http:\/\/www.scalabium.com\/faq\/dct0153.htm\" target=\"_blank\">here<\/a><\/li>\n<\/ul>\n<h2>Excel 2007 and higher files (.xlsx):<\/h2>\n<p>If you are dealing with .xlsx Excel 2007 or later files, you cannot use the Jet engine, but you can use the ACE OLEDB driver:<\/p>\n<ul>\n<li><strong>ACE 32bit OLEDB driver if 32bit Office is installed:<\/strong><\/li>\n<ul>\n<li><code>Provider := Microsoft.ACE.OLEDB.12.0;Data Source=\"C:\\temp\\MyExcel.xlsx\";Extended Properties=\"Excel 12.0 Xml;HDR=YES;<\/code>&#8220;;Jet OLEDB:Database Password=&#8221;&#8221;;<\/li>\n<\/ul>\n<li>\u00a0<strong>ACE 64bit OLEDB driver if 64bit Office installed and 64bit ACE installed:<\/strong><\/li>\n<ul>\n<li><code>Provider := Microsoft.ACE.OLEDB.14.0;Data Source=\"C:\\temp\\MyExcel.xlsx\";Extended Properties=\"Excel 12.0 Xml;HDR=YES;<\/code>&#8220;;Jet OLEDB:Database Password=&#8221;&#8221;;<\/li>\n<\/ul>\n<li>NB. YES you read correctly, that is Jet OLEDB:Database Password=&#8221;&#8221;; and NOT ACE OLEDB:Database Password=&#8221;&#8221;; !!! AND strangely enough, you can connect to a password protected file without supplying a password at all BUT, whether or not you supply a password, the password protected Excel file MUST be already open in full edit mode within Excel first!<\/li>\n<li>NB. to install ACE 64bit on a machine which already has ACE 32bit installed, use command line: AccessDatabaseEngine_X64.exe \/passive<\/li>\n<li>to connect to Excel 97-2003 .xls\u00a0via ACE use Extended Properties=&#8221;Excel 8.0;HDR=YES;&#8221;<\/li>\n<li>to connect to Excel 2007-2010 .xlsx\u00a0via ACE use Extended Properties=&#8221;Excel 12.0 Xml;HDR=YES;&#8221;<\/li>\n<li>to connect to Excel 2007-2010 .xlsm\u00a0via ACE use Extended Properties=&#8221;Excel 12.0 Macro;HDR=YES;&#8221;<\/li>\n<li>to connect to Excel 2007-2010 .xlsb\u00a0via ACE use Extended Properties=&#8221;Excel 12.0;HDR=YES;&#8221;<\/li>\n<li>if you get the error &#8220;could not find installable ISAM&#8221;, you have probably made an error with the connection string.<\/li>\n<li>unfortunately, if the Excel file is password protected to read, you will get an error &#8220;External table is not in the expected format&#8221; unless it is already opened in Excel and user has edit rights to the spreadsheet! So we have the same old situation as with the Jet OLEDB provider!<\/li>\n<li>and don&#8217;t bother trying Password=&#8221;xxx&#8221;;User ID=Admin; as additional parameters, because as soon as you add a value for password, you get the same old Jet issue of &#8220;The workgroup information file is missing or opened exclusively by another user&#8221;.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>There is a little trick to connecting to a password protected .xls Excel spreadsheet using the Jet 4.0 OLEDB driver. &nbsp; If one uses the obvious Password=xxx;User Id=Admin you will get an error &#8220;Workgroup Information file missing or file is &hellip; <a href=\"http:\/\/www.desiderata.com.au\/blog\/?p=553\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[34,43,35],"class_list":["post-553","post","type-post","status-publish","format-standard","hentry","category-delphi","tag-ado","tag-delphi","tag-excel"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/www.desiderata.com.au\/blog\/index.php?rest_route=\/wp\/v2\/posts\/553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.desiderata.com.au\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.desiderata.com.au\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.desiderata.com.au\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.desiderata.com.au\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=553"}],"version-history":[{"count":18,"href":"http:\/\/www.desiderata.com.au\/blog\/index.php?rest_route=\/wp\/v2\/posts\/553\/revisions"}],"predecessor-version":[{"id":555,"href":"http:\/\/www.desiderata.com.au\/blog\/index.php?rest_route=\/wp\/v2\/posts\/553\/revisions\/555"}],"wp:attachment":[{"href":"http:\/\/www.desiderata.com.au\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.desiderata.com.au\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=553"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.desiderata.com.au\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}