如何在不创建访问对象的情况下从C#调用MS Access MACRO [英] How to call MS Access MACRO from C# without creating access object
问题描述
我正在未安装Microsoft Office但已安装访问运行时的环境中工作.
这种情况是我有一个包含链接表的访问文件以实现excel,而我的问题是更新这些链接表的连接字符串的方法.
我创建了一个模块函数和一个宏来进行更新.
问题在于以某种方式调用宏或模块函数而不创建访问对象,因为这样做会引发COM异常.
如果我的解决方案有问题?如果不是,那还有什么替代方法?
I''m working on an environment that I don''t have Microsoft Office installed, but the access runtime is installed.
The scenario is that I have an access file containing linked tables to excel and my problem is the way to update the connection string of these linked tables.
I have created a module function and a macro in order to do the update.
The problem is to call the macro or the module function function in a way without creating an access object because doing so will raise an COM exception.
If there a problem to my solution? If not what are the alternatives?
推荐答案
看看是否 ^ ]帮助.
我发现了一个我在解决方案中采用的解决方案.
I have found a solution that I have adopted in my solution.
public void UpdateLinkedTablesConnectionString()
{
try
{
//Get the excel file location from the directory of the current assembly since the .accdb and the .xlsx are in the same directory location.
string currentAssemblyDirectoryName = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
StringBuilder strPath = new StringBuilder();
strPath.Append(currentAssemblyDirectoryName);
strPath.Append(Common.GetResourceValue(_directory_DataInput, false, null));
strPath.Append(Common.GetResourceValue(_excelFileName, false, string.Empty));
ADODB.Connection Con = new ADODB.Connection();
Con.ConnectionString = myAccessDatabase.ConnectionString;
ADOX.Catalog Cat = new ADOX.Catalog();
Con.Open();
Cat.ActiveConnection = Con;
List<string> lstLinkedTablesNames = GetTablesNames();
foreach (string name in lstLinkedTablesNames )
{
Cat.Tables[name].Properties["Jet OLEDB:Link Datasource"].Value = strPath.ToString();
}
Con.Close();
}
catch (Exception exception)
{
throw exception;
}
}
这篇关于如何在不创建访问对象的情况下从C#调用MS Access MACRO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!