如何使用 Visual C#自动化 Microsoft Access C# 自动化Access失败:Microsoft Offic...

\u5982\u4f55\u4f7f\u7528 Visual C#\u6765\u81ea\u52a8\u6267\u884c Office \u7a0b\u5e8f\u7684\u8fd0\u884c\u5b9e\u4f8b

\u672c\u6587\u5206\u6b65\u9aa4\u4ecb\u7ecd\u5982\u4f55\u4ece Visual C# .NET \u81ea\u52a8\u5316\u5ba2\u6237\u7aef\u8c03\u7528 Office \u5b8f\u3002

\u901a\u8fc7\u4f7f\u7528 Microsoft Office \u81ea\u52a8\u5316\uff0c\u60a8\u53ef\u4ee5\u6253\u5f00\u6216\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5305\u542b Visual Basic for Applications (VBA) \u5b8f\u7684\u6587\u6863\u5e76\u5728\u8fd0\u884c\u65f6\u6267\u884c\u8be5\u5b8f\u3002

\u4e0b\u9762\u7684\u81ea\u52a8\u5316\u5ba2\u6237\u7aef\u793a\u4f8b\u6839\u636e\u60a8\u5728\u7a97\u4f53\u4e0a\u7684\u9009\u62e9\u64cd\u7eb5 Office \u81ea\u52a8\u5316\u670d\u52a1\u5668\uff08Access\u3001Excel\u3001PowerPoint \u6216 Word\uff09\u3002\u5f53\u5ba2\u6237\u7aef\u542f\u52a8\u81ea\u52a8\u5316\u670d\u52a1\u5668\u540e\uff0c\u5c06\u6253\u5f00\u4e00\u4e2a\u6587\u6863\u5e76\u63a5\u7740\u8c03\u7528\u4e24\u4e2a\u5b8f\u3002\u7b2c\u4e00\u4e2a\u5b8f\u662f DoKbTest\uff0c\u5b83\u6ca1\u6709\u53c2\u6570\u3002\u7b2c\u4e8c\u4e2a\u5b8f\u662f DoKbTestWithParameter\uff0c\u6709\u4e00\u4e2a\u201cString\u201d\u7c7b\u578b\u7684\u53c2\u6570\u3002

\u521b\u5efa\u5305\u542b\u5b8f\u7684 Office \u6587\u6863

\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a C:\Doc1.doc \u7684 Word \u6587\u6863\u3002\u4e3a\u6b64\uff0c\u8bf7\u6309\u7167\u4e0b\u5217\u6b65\u9aa4\u64cd\u4f5c\uff1a
\u5728 Word \u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u6863\u3002
\u6309 Alt+F11 \u6253\u5f00 Visual Basic \u7f16\u8f91\u5668\u3002
\u5728\u201c\u63d2\u5165\u201d\u83dc\u5355\u4e0a\uff0c\u5355\u51fb\u201c\u6a21\u5757\u201d\u3002
\u5c06\u4e0b\u9762\u7684\u5b8f\u4ee3\u7801\u7c98\u8d34\u5230\u65b0\u6a21\u5757\u4e2d\uff1a
'Display a message box that displays the application name.
Public Sub DoKbTest()
MsgBox "Hello from " & Application.Name
End Sub

'Display a message box with the string passed from the
'Automation client.
Public Sub DoKbTestWithParameter( sMsg As String )
MsgBox sMsg
End Sub

\u5173\u95ed Visual Basic \u7f16\u8f91\u5668\uff0c\u4fdd\u5b58\u8be5 Word \u6587\u6863\uff0c\u7136\u540e\u9000\u51fa Word\u3002
\u4f7f\u7528\u4e0e\u521b\u5efa Word \u6587\u6863\u7c7b\u4f3c\u7684\u6b65\u9aa4\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a C:\Book1.xls \u7684 Excel \u5de5\u4f5c\u7c3f\u3002
\u4f7f\u7528\u4e0e\u521b\u5efa Word \u6587\u6863\u7c7b\u4f3c\u7684\u6b65\u9aa4\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a C:\Pres1.ppt \u7684 PowerPoint \u6f14\u793a\u6587\u7a3f\u3002
\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a C:\Db1.mdb \u7684\u65b0 Access \u6570\u636e\u5e93\u3002\u4e3a\u6b64\uff0c\u8bf7\u6309\u7167\u4e0b\u5217\u6b65\u9aa4\u64cd\u4f5c\uff1a
\u5728\u201c\u63d2\u5165\u201d\u83dc\u5355\u4e0a\uff0c\u5355\u51fb\u201c\u6a21\u5757\u201d\u3002
\u5c06\u5b8f\u4ee3\u7801\u7c98\u8d34\u5230\u8be5\u65b0\u6a21\u5757\u4e2d\u3002
\u4fdd\u5b58\u8be5\u6a21\u5757\uff0c\u7136\u540e\u9000\u51fa Access\u3002
\u521b\u5efa Visual C# .NET \u81ea\u52a8\u5316\u5ba2\u6237\u7aef

\u542f\u52a8 Microsoft Visual Studio .NET\u3002\u5728\u201c\u6587\u4ef6\u201d\u83dc\u5355\u4e0a\uff0c\u5355\u51fb\u201c\u65b0\u5efa\u201d\uff0c\u7136\u540e\u5355\u51fb\u201c\u9879\u76ee\u201d\u3002\u5728\u201c\u9879\u76ee\u7c7b\u578b\u201d\u4e0b\uff0c\u5355\u51fb\u201cVisual C# \u9879\u76ee\u201d\uff0c\u7136\u540e\u5355\u51fb\u201c\u6a21\u677f\u201d\u4e0b\u7684\u201cWindows \u5e94\u7528\u7a0b\u5e8f\u201d\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4f1a\u521b\u5efa Form1\u3002
\u6dfb\u52a0\u5bf9 Access\u3001Excel\u3001PowerPoint \u548c Word \u5bf9\u8c61\u5e93\u7684\u5f15\u7528\u3002\u4e3a\u6b64\uff0c\u8bf7\u6309\u7167\u4e0b\u5217\u6b65\u9aa4\u64cd\u4f5c\uff1a
\u5728\u201c\u9879\u76ee\u201d\u83dc\u5355\u4e0a\uff0c\u5355\u51fb\u201c\u6dfb\u52a0\u5f15\u7528\u201d\u3002
\u5728\u201cCOM\u201d\u9009\u9879\u5361\u4e0a\uff0c\u627e\u5230\u201cMicrosoft Word 10.0 \u5bf9\u8c61\u5e93\u6216 Microsoft Word 11.0 \u5bf9\u8c61\u5e93\u201d\uff0c\u7136\u540e\u5355\u51fb\u201c\u9009\u62e9\u201d\u3002\u6ce8\u610f\uff1a\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662f Office XP \u4e14\u5c1a\u672a\u6267\u884c\u6b64\u64cd\u4f5c\uff0cMicrosoft \u5efa\u8bae\u60a8\u4e0b\u8f7d\u5e76\u5b89\u88c5 Microsoft Office XP \u4e3b\u4e92\u64cd\u4f5c\u7a0b\u5e8f\u96c6 (PIA)\u3002\u6709\u5173 Office XP PIA \u7684\u5176\u4ed6\u4fe1\u606f\uff0c\u8bf7\u5355\u51fb\u4e0b\u9762\u7684\u6587\u7ae0\u7f16\u53f7\uff0c\u4ee5\u67e5\u770b Microsoft \u77e5\u8bc6\u5e93\u4e2d\u76f8\u5e94\u7684\u6587\u7ae0\uff1a
328912 Microsoft Office XP \u4e3b\u4e92\u64cd\u4f5c\u7a0b\u5e8f\u96c6 (PIA) \u53ef\u4f9b\u4e0b\u8f7d
\u4e3a Access\u3001Excel \u548c PowerPoint \u5bf9\u8c61\u5e93\u91cd\u590d\u4e0a\u4e00\u6b65\u3002
\u5728\u201c\u6dfb\u52a0\u5f15\u7528\u201d\u5bf9\u8bdd\u6846\u4e2d\uff0c\u5355\u51fb\u201c\u786e\u5b9a\u201d\u4ee5\u63a5\u53d7\u60a8\u7684\u9009\u62e9\u3002\u5982\u679c\u7cfb\u7edf\u63d0\u793a\u60a8\u4e3a\u9009\u5b9a\u7684\u5e93\u751f\u6210\u5305\u88c5\uff0c\u8bf7\u5355\u51fb\u201c\u662f\u201d\u3002
\u5728\u201c\u89c6\u56fe\u201d\u83dc\u5355\u4e0a\uff0c\u5355\u51fb\u201c\u5de5\u5177\u7bb1\u201d\u3002\u5411 Form1 \u6dfb\u52a0\u4e00\u4e2a\u201cCombo Box\u201d\u63a7\u4ef6\u548c\u4e00\u4e2a\u201cButton\u201d\u63a7\u4ef6\u3002
\u53cc\u51fb\u201cButton1\u201d\u4e3a\u6309\u94ae\u7684\u201cClick\u201d\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u751f\u6210\u5b9a\u4e49\u3002
\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u7c98\u8d34\u5230\u201cbutton1_Click\u201d\u5904\u7406\u7a0b\u5e8f\u4e2d\uff1a
private void button1_Click(object sender, System.EventArgs e)
{
// Object for missing (or optional) arguments.
object oMissing = System.Reflection.Missing.Value;

//Switch based on the user selection.
switch (comboBox1.SelectedIndex)
{
case 0:
// Create an instance of Microsoft Access, make it visible,
// and open Db1.mdb.
Access.ApplicationClass oAccess = new Access.ApplicationClass();
oAccess.Visible = true;
oAccess.OpenCurrentDatabase("c:\\db1.mdb", false, "");

// Run the macros.
RunMacro(oAccess, new Object[]{"DoKbTest"});
RunMacro(oAccess, new Object[]{"DoKbTestWithParameter",
"Hello from C# Client."});

// Quit Access and clean up.
oAccess.DoCmd.Quit(Access.AcQuitOption.acQuitSaveNone);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oAccess);
oAccess = null;

break;

case 1:
// Create an instance of Microsoft Excel, make it visible,
// and open Book1.xls.
Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
oExcel.Visible = true;
Excel.Workbooks oBooks = oExcel.Workbooks;
Excel._Workbook oBook = null;
oBook = oBooks.Open("c:\\book1.xls", oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

// Run the macros.
RunMacro(oExcel, new Object[]{"DoKbTest"});
RunMacro(oExcel, new Object[]{"DoKbTestWithParameter",
"Hello from C# Client."});

// Quit Excel and clean up.
oBook.Close(false, oMissing, oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oBook);
oBook = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject (oBooks);
oBooks = null;
oExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (oExcel);
oExcel = null;

break;

case 2:

// Create an instance of PowerPoint, make it visible,
// and open Pres1.ppt.
PowerPoint.ApplicationClass oPP = new PowerPoint.ApplicationClass();
oPP.Visible = MsoTriState.msoTrue;
PowerPoint.Presentations oPresSet = oPP.Presentations;
PowerPoint._Presentation oPres = oPresSet.Open("c:\\pres1.ppt",
MsoTriState.msoFalse, MsoTriState.msoFalse,
MsoTriState.msoTrue);

// Run the macros.
RunMacro(oPP, new Object[]{"'pres1.ppt'!DoKbTest"});
RunMacro(oPP, new Object[]{"'pres1.ppt'!DoKbTestWithParameter",
"Hello from C# Client."});

// Quit PowerPoint and clean up.
oPres.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject (oPres);
oPres = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject (oPresSet);
oPresSet = null;
oPP.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (oPP);
oPP = null;

break;

case 3:

// Create an instance of Word, make it visible,
// and open Doc1.doc.
Word.ApplicationClass oWord = new Word.ApplicationClass();
oWord.Visible = true;
Word.Documents oDocs = oWord.Documents;
object oFile = "c:\\doc1.doc";

// If the Microsoft Word 10.0 Object Library is referenced
// use the following code.
Word._Document oDoc = oDocs.Open(ref oFile, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing);

// If the Microsoft Word 11.0 Object Library is referenced comment
// the previous line of code and uncomment the following code.
//Word._Document oDoc = oDocs.Open(ref oFile, ref oMissing,
//ref oMissing, ref oMissing, ref oMissing, ref oMissing,
//ref oMissing, ref oMissing, ref oMissing, ref oMissing,
//ref oMissing, ref oMissing, ref oMissing, ref oMissing,
//ref oMissing, ref oMissing);

// Run the macros.
RunMacro(oWord, new Object[]{"DoKbTest"});
RunMacro(oWord, new Object[]{"DoKbTestWithParameter",
"Hello from C# Client."});

// Quit Word and clean up.
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oDoc);
oDoc = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject (oDocs);
oDocs = null;
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWord);
oWord = null;

break;

}

GC.Collect(); //Garbage collection.
}

\u5728\u201cbutton1_Click\u201d\u5904\u7406\u7a0b\u5e8f\u4e4b\u540e\u6dfb\u52a0\u4e0b\u9762\u7684\u51fd\u6570\uff1a
private void RunMacro(object oApp, object[] oRunArgs)
{
oApp.GetType().InvokeMember("Run",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.InvokeMethod,
null, oApp, oRunArgs);
}

\u5728\u201c\u89c6\u56fe\u201d\u83dc\u5355\u4e0a\uff0c\u5355\u51fb\u201c\u8bbe\u8ba1\u5668\u201d\uff0c\u7136\u540e\u53cc\u51fb\u201cForm1\u201d\uff0c\u4ee5\u751f\u6210\u8be5\u7a97\u4f53\u7684\u201cLoad\u201d\u4e8b\u4ef6\u7684\u5b9a\u4e49\u3002
\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u7c98\u8d34\u5230\u201cForm1_Load\u201d\u5904\u7406\u7a0b\u5e8f\u4e2d\uff1a
private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.Items.AddRange(new object[]
{"Access", "Excel", "PowerPoint", "Word"});
comboBox1.SelectedIndex = 0;
}

\u6eda\u52a8\u5230\u4ee3\u7801\u7a97\u53e3\u7684\u9876\u90e8\uff0c\u7136\u540e\u5c06\u4e0b\u9762\u7684\u4ee3\u7801\u884c\u6dfb\u52a0\u5230\u201cusing\u201d\u6307\u4ee4\u5217\u8868\u7684\u672b\u5c3e\uff1a
using System.Reflection;
using Access = Microsoft.Office.Interop.Access;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;

\u8fd0\u884c\u5e76\u6d4b\u8bd5\u81ea\u52a8\u5316\u5ba2\u6237\u7aef

\u6309 F5\uff0c\u4ee5\u8fd0\u884c\u8be5\u5e94\u7528\u7a0b\u5e8f\u3002
\u4ece\u201cComboBox1\u201d\u4e2d\u9009\u62e9\u4e00\u4e2a Office \u5e94\u7528\u7a0b\u5e8f\uff0c\u7136\u540e\u5355\u51fb\u201cButton1\u201d\u3002\u968f\u5373\u5c06\u542f\u52a8\u60a8\u6240\u9009\u7684 Office \u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u8fd0\u884c DoKBTest \u548c DoKBTestWithParameter \u5b8f\u3002
\u7591\u96be\u89e3\u7b54

\u5f53\u60a8\u5728 Visual C# .NET \u9879\u76ee\u4e2d\u5f15\u7528 Access 10.0 \u5bf9\u8c61\u5e93\u65f6\uff0c\u53ef\u80fd\u4f1a\u6536\u5230\u4e00\u6761\u9519\u8bef\u6d88\u606f\uff0c\u8bf4\u660e\u672a\u80fd\u5c06\u8be5\u5e93\u8f6c\u6362\u4e3a .NET \u7a0b\u5e8f\u96c6\u3002

\u6709\u5173\u5982\u4f55\u89e3\u51b3\u6b64\u9519\u8bef\u4ee5\u6210\u529f\u5f15\u7528 Access 10.0 \u5bf9\u8c61\u5e93\u7684\u5176\u4ed6\u4fe1\u606f\uff0c\u8bf7\u5355\u51fb\u4e0b\u9762\u7684\u6587\u7ae0\u7f16\u53f7\uff0c\u4ee5\u67e5\u770b Microsoft \u77e5\u8bc6\u5e93\u4e2d\u76f8\u5e94\u7684\u6587\u7ae0\uff1a
317157 PRB\uff1a\u5728\u4f7f\u7528 Visual Studio .NET \u5f15\u7528 Access 10.0 \u7c7b\u578b\u5e93\u65f6\u51fa\u9519

\u6570\u636e\u5e93\u6709\u5bc6\u7801\uff1f


  
要预览或打印 Access 报表,可以调用 DoCmd 对象的 OpenReport 方法。当您调用OpenReport 方法时,您传递的一个参数将确定是在屏幕上预览报表还是将报表发送到打印机: 
// Preview a report named Sales: oAccess.DoCmd.OpenReport( 
   "Sales", //ReportName 
   Access.AcView.acViewPreview, //View 
   System.Reflection.Missing.Value, //FilterName    System.Reflection.Missing.Value //WhereCondition    );  
// Print a report named Sales: oAccess.DoCmd.OpenReport( 
   "Sales", //ReportName 
   Access.AcView.acViewNormal, //View 
   System.Reflection.Missing.Value, //FilterName    System.Reflection.Missing.Value //WhereCondition    );  
 
 
 
 
请注意,View 参数确定是在 Access 中显示报表还是将报表发送到打印机。WhereCondition 参数在您使用有效的 SQL WHERE 子句时可以限制报表的记录集(无需使用 WHERE 一词)。请注意,您可以使用 System.Reflection.Missing.Value 来跳过任何可选的对象参数。  
如果您要预览报表,请确保设置了 Application 对象的 Visible 属性,以使 Access 在屏幕上可见。这样,用户就可以在 Access 窗口中查看报表了。  
还可以通过另外一种方式来打印报表或数据库中的其他对象,这就是使用 DoCmd 对象的 PrintOut方法。在本示例中,用户在“数据库”窗口中选择了名为 Employees 的报表,然后调用 PrintOut 来打印选定的对象。PrintOut 方法允许您提供与 Access 中的“打印”对话框相对应的参数: 
// Select the Employees report in the database window: oAccess.DoCmd.SelectObject( 
   Access.AcObjectType.acReport, //ObjectType    "Employees", //ObjectName    true //InDatabaseWindow    );  
// Print 2 copies of the selected object: 
  
oAccess.DoCmd.PrintOut( 
   Access.AcPrintRange.acPrintAll, //PrintRange    System.Reflection.Missing.Value, //PageFrom    System.Reflection.Missing.Value, //PageTo    Access.AcPrintQuality.acHigh, //PrintQuality    2, //Copies 
   false //CollateCopies    );   
 
 
 
或者,在某些情况下,您可能希望同时使用 OpenReport 方法和 PrintOut 方法打印报表。假设您希望仅为特定雇员打印多份 Employees 报表副本。本示例首先使用 OpenReport 在预览模式下打开 Employees 报表,使用 WhereCondition 参数将记录限制为特定的雇员。然后使用 PrintOut打印活动对象的多份副本: 
// Open the report in preview mode using a WhereCondition: oAccess.DoCmd.OpenReport( 
   "Employees", //ReportName 
   Access.AcView.acViewPreview, //View 
   System.Reflection.Missing.Value, //FilterName    "[EmployeeID]=1" //WhereCondition    );  
// Print 2 copies of the active object:  oAccess.DoCmd.PrintOut( 
   Access.AcPrintRange.acPrintAll, //PrintRange    System.Reflection.Missing.Value, //PageFrom    System.Reflection.Missing.Value, //PageTo    Access.AcPrintQuality.acHigh, //PrintQuality    2, //Copies 
   false //CollateCopies    );  
// Close the report preview window:  
oAccess.DoCmd.Close( 
   Access.AcObjectType.acReport, //ObjectType    "Employees", //ObjectName 
   Access.AcCloseSave.acSaveNo //Save    );  
 
 
 
 
  
Access 2002 引入了 Printer 对象。可以使用此对象自定义 Access 打印机设置,这比早期版本的 Access 更加容易。要获得在 Access 中使用 Printer 对象打印报表的示例,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章: 284286  如何重新设置 Application.Printer 对象的更改 显示和编辑 Access 窗体 
Visual C# .NET 具有非常强大的窗体功能。但是,可能会时常出现以下情况,即您希望用户查看以前在 Access 中开发的窗体。或者,您的 Access 数据库中可能有一个提供查询或报表条件的窗体,您必须打开该窗体才能预览或打印报表。要打开并显示 Access 窗体,需要调用 DoCmd 对象的 OpenForm 方法: 
// Show a form named Employees: oAccess.DoCmd.OpenForm( 
   "Employees", //FormName 
   Access.AcFormView.acNormal, //View 
   System.Reflection.Missing.Value, //FilterName 
   System.Reflection.Missing.Value, //WhereCondition    Access.AcFormOpenDataMode.acFormPropertySettings, //DataMode 
   Access.AcWindowMode.acWindowNormal, //WindowMode    System.Reflection.Missing.Value //OpenArgs    );   
 
 
 
现在,您可以编辑窗体上的控件。


扩展阅读:visual studio 2022 ... visual c++安装教程 ... microsoft visual c++ ... visual c++ 6.0使用教程 ... visual c++ 6.0怎么使用 ... c和c#和c++区别 ... visual studio c# ... c#程序设计 ... c#官网微软 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网