{"id":18,"date":"2010-05-27T18:33:52","date_gmt":"2010-05-27T10:33:52","guid":{"rendered":"http:\/\/j178.mtgbb.com\/270"},"modified":"2010-05-27T18:33:52","modified_gmt":"2010-05-27T10:33:52","slug":"%e7%94%a8-vb-net-%e5%9f%b7%e8%a1%8c-office-%e7%9a%84%e5%b7%a8%e9%9b%86","status":"publish","type":"post","link":"https:\/\/j178.mtgbb.com\/?p=18","title":{"rendered":"\u7528 VB.NET \u57f7\u884c Office \u7684\u5de8\u96c6"},"content":{"rendered":"<p>\u9996\u5148, \u5148\u6e96\u5099\u4e00\u500b office \u6a94\u6848, \u5167\u5bb9\u52a0\u5165\u5de8\u96c6\u5982\u4e0b<\/p>\n<pre lang=\"vb\" line=\"1\">\n'Display a message box that displays the application name.\nPublic Sub DoKbTest()\n   MsgBox \"Hello from \" &amp; Application.Name\nEnd Sub\n\n'Display a message box with the string passed from the\n'Automation client.\nPublic Sub DoKbTestWithParameter( sMsg As String )\n   MsgBox sMsg\nEnd Sub\n<\/pre>\n<p>\u8a3b: \u4e00\u500b\u662f\u76f4\u63a5\u547c\u53eb, \u53e6\u4e00\u500b\u662f\u52a0\u5165\u50b3\u5165\u53c3\u6578<\/p>\n<p><!--more--><\/p>\n<p>\u7136\u5f8c, \u5efa\u7acb\u4e00\u500b\u65b0\u7684 VB.Net \u5c08\u6848 (form)<\/p>\n<p>1. \u5c08\u6848\u88e1\u52a0\u5165 COM reference,\u00a0 \u627e\u5230 Microsoft Word 10.0 Object Library or Microsoft Word 11.0 Object Library\uff0c\u7136\u5f8c\u6309\u4e00\u4e0b [\u9078\u53d6].<\/p>\n<p>2. \u91cd\u8907\u5148\u524d\u6b65\u9a5f\u4e2d\uff0c\u5b58\u53d6\u3001 Excel \u53ca PowerPoint Object Library.<\/p>\n<p>3. \u5c07\u4e0b\u5217\u7a0b\u5f0f\u78bc\u8cbc\u5230 Button1_Click \u4e2d\uff1a<\/p>\n<pre lang=\"vbnet\" line=\"1\">\nSelect Case ComboBox1.SelectedItem\n\nCase \"Access\"\n\nDim oAccess As Access.ApplicationClass\n\n'Start Access and open the database.\noAccess = CreateObject(\"Access.Application\")\noAccess.Visible = True\noAccess.OpenCurrentDatabase(\"c:db1.mdb\", False)\n\n'Run the macros.\noAccess.Run (\"DoKbTest\")\noAccess.Run(\"DoKbTestWithParameter\", \"Hello from VB .NET Client\")\n\n'Clean-up: Quit Access without saving changes to the database.\noAccess.DoCmd().Quit (Access.AcQuitOption.acQuitSaveNone)\nSystem.Runtime.InteropServices.Marshal.ReleaseComObject (oAccess)\noAccess = Nothing\n\nCase \"Excel\"\n\nDim oExcel As Excel.ApplicationClass\nDim oBook As Excel.WorkbookClass\nDim oBooks As Excel.Workbooks\n\n'Start Excel and open the workbook.\noExcel = CreateObject(\"Excel.Application\")\noExcel.Visible = True\noBooks = oExcel.Workbooks\noBook = oBooks.Open(\"c:book1.xls\")\n\n'Run the macros.\noExcel.Run (\"DoKbTest\")\noExcel.Run(\"DoKbTestWithParameter\", \"Hello from VB .NET Client\")\n\n'Clean-up: Close the workbook and quit Excel.\noBook.Close (False)\nSystem.Runtime.InteropServices.Marshal.ReleaseComObject (oBook)\noBook = Nothing\nSystem.Runtime.InteropServices.Marshal.ReleaseComObject (oBooks)\noBooks = Nothing\noExcel.Quit()\nSystem.Runtime.InteropServices.Marshal.ReleaseComObject (oExcel)\noExcel = Nothing\n\nCase \"PowerPoint\"\n\nDim oPP As PowerPoint.ApplicationClass\nDim oPresSet As PowerPoint.Presentations\nDim oPres As PowerPoint.PresentationClass\n\n'Start PowerPoint and open the presentation.\noPP = CreateObject(\"PowerPoint.Application\")\noPP.Visible = True\noPresSet = oPP.Presentations\noPres = oPresSet.Open(\"c:pres1.ppt\", , , True)\n\n'Run the macros.\noPP.Run (\"'pres1.ppt'!DoKbTest\")\noPP.Run(\"'pres1.ppt'!DoKbTestWithParameter\", \"Hello from VB .NET Client\")\n\n'Clean-up: Close the presentation and quit PowerPoint.\noPres.Close()\nSystem.Runtime.InteropServices.Marshal.ReleaseComObject (oPres)\noPres = Nothing\nSystem.Runtime.InteropServices.Marshal.ReleaseComObject (oPresSet)\noPresSet = Nothing\noPP.Quit()\nSystem.Runtime.InteropServices.Marshal.ReleaseComObject (oPP)\noPP = Nothing\n\nCase \"Word\"\n\nDim oWord As Word.ApplicationClass\n\n'Start Word and open the document.\noWord = CreateObject(\"Word.Application\")\noWord.Visible = True\noWord.Documents.Open (\"C:Doc1.doc\")\n\n'Run the macros.\noWord.Run (\"DoKbTest\")\noWord.Run(\"DoKbTestWithParameter\", \"Hello from VB .NET Client\")\n\n'Quit Word.\noWord.Quit()\nSystem.Runtime.InteropServices.Marshal.ReleaseComObject (oWord)\noWord = Nothing\n\nEnd Select\nGC.Collect()\n<\/pre>\n<p>4. \u5728 form1_load \u88e1\u52a0\u4e0a\u4ee5\u4e0b\u7a0b\u5f0f\u78bc<\/p>\n<pre lang=\"vbnet\" line=\"1\">\nComboBox1.DropDownStyle = ComboBoxStyle.DropDownList\nDim a As String() = {\"Access\", \"Excel\", \"PowerPoint\", \"Word\"}\nComboBox1.Items.AddRange(a)\nComboBox1.SelectedIndex = 0\n<\/pre>\n<p>5. \u628a\u4ee5\u4e0b\u7a0b\u5f0f import \u9032\u53bb<\/p>\n<pre lang=\"vbnet\" line=\"1\">\nImports Access = Microsoft.Office.Interop.Access\nImports Excel = Microsoft.Office.Interop.Excel\nImports Word = Microsoft.Office.Interop.Word\nImports PowerPoint = Microsoft.Office.Interop.PowerPoint\n<\/pre>\n<p>\u63a5\u4e0b\u4f86\u5c31\u53ef\u4ee5\u958b\u59cb\u6e2c\u8a66\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148, \u5148\u6e96\u5099\u4e00\u500b office \u6a94\u6848, \u5167\u5bb9\u52a0\u5165\u5de8\u96c6\u5982\u4e0b &#8216;Display a message box th &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35,15],"tags":[34,33,36],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-excel-vba","category-vs-net","tag-excel","tag-vb-net","tag-vba"],"_links":{"self":[{"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18"}],"version-history":[{"count":0,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions"}],"wp:attachment":[{"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/j178.mtgbb.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}