从c#中调用Access中的宏
2012-02-06
下面是在C#中调用Access中的宏来实现一定的功能
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace ABC
{
/// <summary>
/// Shell for the sample.
/// </summary>
public class ToAccess
{
// These are the Win32 error code for file not found or access denied.
const int ERROR_FILE_NOT_FOUND =2;
const int ERROR_ACCESS_DENIED = 5;
/// <summary>
/// Prints a file with a .doc extension.
/// </summary>
public void PrintDoc()
{
Process myProcess = new Process();
try
{
// Get the path that stores user documents.
myProcess.StartInfo.FileName = @"C:\Program Files\Microsoft Office\Office10\MSACCESS.EXE";
myProcess.StartInfo.Arguments = @"C:\antos\Programs\Access\ANTOS_Print_Program.mdb /x 受注チェックリスト /nostartup";
myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
myProcess.Start();
}
catch (Win32Exception e)
{
if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
Console.WriteLine(e.Message + ". Check the path.");
}
else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
{
// Note that if your word processor might generate exceptions
// such as this, which are handled first.
Console.WriteLine(e.Message +
". You do not have permission to print this file.");
}
}
}
public static void Main()
{
ToAccess myProcess = new ToAccess();
myProcess.PrintDoc();
}
}
}
Access命令行参数
可以参见
/x macro
启动 Microsoft Access,并运行指定的宏。另一个在打开数据库时运行宏的方法是使用 Autoexec 宏。
/nostartup
启动 Microsoft Access 而不显示“启动”对话框(当启动 Microsoft Access 时所看到的第二个对话框)。