นำไปใช้กับ
Microsoft Office Professional Edition 2003 Excel 2010

สรุป

เมื่อย้ายผลิตภัณฑ์ Office จาก Visual Basic โดยอัตโนมัติ การย้ายส่วนของโค้ดไปยังมอดูล Microsoft Visual Basic for Applications (VBA) ที่สามารถเรียกใช้ภายในพื้นที่กระบวนการของเซิร์ฟเวอร์เป็นประโยชน์อย่างมาก ซึ่งสามารถเพิ่มประสิทธิภาพการปฏิบัติการโดยรวมของแอปพลิเคชันของคุณ และช่วยลดปัญหาถ้าเซิร์ฟเวอร์มีแต่การปฏิบัติการเมื่อการโทรอยู่ในกระบวนการเท่านั้นบทความนี้แสดงวิธีการเพิ่มมอดูล VBA ไปยังแอปพลิเคชัน Office ที่เรียกใช้จาก Visual Basic แล้วเรียกใช้แมโครเพื่อเติมเวิร์กชีตในกระบวนการ

ข้อมูลเพิ่มเติม

ตัวอย่างต่อไปนี้แสดงการแทรกโค้ดโมดูลลงใน Microsoft Excel แต่คุณสามารถใช้เทคนิคเดียวกันใน Word และ PowerPoint เนื่องจากทั้งสองใช้เครื่องมือ VBA เดียวกันตัวอย่างจะใช้ไฟล์ข้อความคงที่ของโค้ดมอดูลที่แทรกExcelเนื้อหา You may want to consider moving the code into a resource file that you can compily into your application, and then extract into a temporary file when needed at run time. ซึ่งช่วยให้โครงการสามารถจัดการได้มากกว่าการแจกแจงแบบใหม่เริ่มต้นด้วย Microsoft Office XP ผู้ใช้ต้องให้สิทธิ์การเข้าถึงรูปแบบวัตถุ VBA ก่อนที่โค้ด Automation ใดๆ ที่เขียนขึ้นเพื่อจัดการ VBA จะได้ผล นี่คือฟีเจอร์ความปลอดภัยใหม่ที่มีOffice XP โปรดดูบทความฐานความรู้ต่อไปนี้:

282830 การเข้าถึงทางโปรแกรมOffice VBA ของ XP Projectถูกปฏิเสธ

ขั้นตอนในการสร้างตัวอย่าง

  1. ก่อนอื่น ให้สร้างไฟล์ข้อความใหม่ที่ชื่อว่า KbTest.bas (.txtนามสกุล) นี่คือโค้ดมอดูลที่เราจะแทรกExcelข้อมูลในเวลาที่เรียกใช้

  2. ในไฟล์ข้อความ ให้เพิ่มบรรทัดโค้ดต่อไปนี้:

       Attribute VB_Name = "KbTest"   ' Your Microsoft Visual Basic for Applications macro function takes 1    ' parameter, the sheet object that you are going to fill.   Public Sub DoKbTest(oSheetToFill As Object)      Dim i As Integer, j As Integer      Dim sMsg As String      For i = 1 To 100         For j = 1 To 10            sMsg = "Cell(" & Str(i) & "," & Str(j) & ")"            oSheetToFill.Cells(i, j).Value = sMsg         Next j      Next i   End Sub
  3. บันทึกไฟล์ข้อความในไดเรกทอรี C:\KbTest.bas แล้วปิดไฟล์

  4. เริ่มVisual Basicสร้างโครงการมาตรฐาน Form1 จะถูกสร้างขึ้นตามค่าเริ่มต้น

  5. บนเมนู Project ให้คลิก การอนุมาน จากนั้นเลือกเวอร์ชันไลบรารีชนิดที่เหมาะสมซึ่งอนุญาตให้คุณใช้การผูกข้อมูลล่วงหน้าExcelได้ตัวอย่างเช่น เลือกอย่างใดอย่างหนึ่งต่อไปนี้:

    • For Microsoft Office Excel 2007, select the 12.0 library.

    • For Microsoft Office Excel 2003, select the 11.0 library.

    • For Microsoft Excel 2002, select the 10.0 library.

    • For Microsoft Excel 2000, select the 9.0 library.

    • For Microsoft Excel 97, select the 8.0 library.

  6. เพิ่มปุ่มลงใน Form1 และวางโค้ดต่อไปนี้ลงในตัวจัดการเหตุการณ์ Click ของปุ่ม

       Private Sub Command1_Click()      Dim oXL As Excel.Application      Dim oBook As Excel.Workbook      Dim oSheet As Excel.Worksheet      Dim i As Integer, j As Integer      Dim sMsg As String    ' Create a new instance of Excel and make it visible.      Set oXL = CreateObject("Excel.Application")      oXL.Visible = True    ' Add a new workbook and set a reference to Sheet1.      Set oBook = oXL.Workbooks.Add      Set oSheet = oBook.Sheets(1)    ' Demo standard Automation from out-of-process,    ' this routine simply fills in values of cells.      sMsg = "Fill the sheet from out-of-process"      MsgBox sMsg, vbInformation Or vbMsgBoxSetForeground      For i = 1 To 100         For j = 1 To 10            sMsg = "Cell(" & Str(i) & "," & Str(j) & ")"            oSheet.Cells(i, j).Value = sMsg         Next j      Next i    ' You're done with the first test, now switch sheets    ' and run the same routine via an inserted Microsoft Visual Basic     ' for Applications macro.      MsgBox "Done.", vbMsgBoxSetForeground      Set oSheet = oBook.Sheets.Add      oSheet.Activate      sMsg = "Fill the sheet from in-process"      MsgBox sMsg, vbInformation Or vbMsgBoxSetForeground    ' The Import method lets you add modules to VBA at    ' run time. Change the file path to match the location    ' of the text file you created in step 3.      oXL.VBE.ActiveVBProject.VBComponents.Import "C:\KbTest.bas"    ' Now run the macro, passing oSheet as the first parameter      oXL.Run "DoKbTest", oSheet    ' You're done with the second test      MsgBox "Done.", vbMsgBoxSetForeground    ' Turn instance of Excel over to end user and release    ' any outstanding object references.      oXL.UserControl = True      Set oSheet = Nothing      Set oBook = Nothing      Set oXL = Nothing   End Sub
  7. For Excel 2002 and for later versions Excel, you must turn on access the VBA project. เมื่อต้องการเลือกวิธีนี้ ให้ใช้วิธีใดวิธีหนึ่งต่อไปนี้

    • ใน Excel 2007 Microsoft Office ให้คลิก ปุ่มการExcelคลิก ตัวเลือก คลิก ศูนย์ความเชื่อถือ แล้วคลิก ศูนย์ความเชื่อถือ การตั้งค่าโฟลเดอร์ คลิก การตั้งค่าแมโคร คลิกเพื่อเลือกกล่องกาเครื่องหมาย เชื่อถือ Access ไปยังรูปแบบวัตถุของโครงการ VBA แล้วคลิก ตกลง สองครั้ง

    • ใน Excel 2003 และใน Excelเวอร์ชันก่อนหน้า ให้ชี้ไปที่ แมโคร บนเมนู เครื่องมือ แล้วคลิก ความปลอดภัย ในกล่องโต้ตอบ ความปลอดภัย ให้คลิกแท็บ แหล่งข้อมูลที่เชื่อถือได้ จากนั้นคลิกเพื่อเลือกกล่องกาเครื่องหมาย เชื่อถือการเข้าถึงVisual Basic Projectเครือข่าย

  8. เรียกใช้Visual Basicโครงการ

อ้างอิง

For more information on Automation of Office from Visual Basic, please see the Office Development Support site at the following address:

http://support.microsoft.com/ofd

ต้องการความช่วยเหลือเพิ่มเติมหรือไม่

ต้องการตัวเลือกเพิ่มเติมหรือไม่

สํารวจสิทธิประโยชน์ของการสมัครใช้งาน เรียกดูหลักสูตรการฝึกอบรม เรียนรู้วิธีการรักษาความปลอดภัยอุปกรณ์ของคุณ และอื่นๆ