สรุป

คุณสามารถเรียกใช้ฟังก์ชัน Windows API ShellExecute()จาก Visual Basic สำหรับแมโครที่โปรแกรมประยุกต์เพื่อเริ่มการทำงานโปรแกรมอื่นภายใต้ Microsoft Windows ใช้ShellExecute()แทนที่เป็นเชลล์(คำสั่ง Visual Basic) หรือWinExec() (ฟังก์ชัน Windows API) เพื่อหลีกเลี่ยงข้อจำกัดของคำสั่งหลัง:กับเชลล์และWinExec()คุณไม่สามารถเริ่มโปรแกรมประยุกต์ ด้วยการระบุชื่อแฟ้มเท่านั้น ตัวอย่างเช่นเชลล์คำสั่งต่อไปนี้จะล้มเหลว:

x = Shell("C:\My Documents\Book1.Xls")

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

Microsoft แสดงตัวอย่างการเขียนโปรแกรมเพื่อประกอบการเท่านั้น โดยไม่มีการรับประกันทั้งโดยชัดแจ้ง หรือโดยนัย กรณีนี้รวมถึงแต่มิได้จำกัดเพียงแค่การการรับประกันโดยนัยเกี่ยวกับการจำหน่ายเป็นสินค้าหรือความเหมาะสำหรับวัตถุประสงค์เฉพาะ บทความนี้ตั้งสมมติฐานว่าคุณมีความคุ้นเคยกับภาษาการเขียนโปรแกรมที่กำลังแสดงและคุ้นเคยกับเครื่องมือที่ใช้ใน การสร้างและแก้จุดบกพร่องของขั้นตอนการดำเนินการ วิศวกรฝ่ายสนับสนุนของ Microsoft สามารถช่วยอธิบายฟังก์ชันการทำงานของกระบวนการเฉพาะได้ แต่จะไม่ปรับเปลี่ยนตัวอย่างเหล่านี้เพื่อให้ฟังก์ชันการทำงานเพิ่มเติมหรือสร้างกระบวนงานเพื่อตอบสนองความต้องการเฉพาะของคุณ ด้านล่างนี้คือ ตัวอย่าง Visual Basic สำหรับแมโครที่โปรแกรมประยุกต์ที่เรียกฟังก์ชัน Windows API ShellExecute() ShellExecute()กำหนดว่า Microsoft Excel กำลังทำงาน ถ้าเป็นเช่นนั้น โหลด Book1.xls ลงในเซสชันปัจจุบันของ Microsoft Excel หาก Microsoft Excel จะไม่เรียกใช้ShellExecute()เริ่มต้น Microsoft Excel และโหลด Book1.xls

Declare Function ShellExecute Lib "shell32.dll" Alias _   "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation _   As String, ByVal lpFile As String, ByVal lpParameters _   As String, ByVal lpDirectory As String, ByVal nShowCmd _   As Long) As LongDeclare Function apiFindWindow Lib "User32" Alias "FindWindowA" _   (ByVal lpclassname As Any, ByVal lpCaption As Any) As LongGlobal Const SW_SHOWNORMAL = 1Sub ShellExecuteExample()   Dim hwnd   Dim StartDoc   hwnd = apiFindWindow("OPUSAPP", "0")   StartDoc = ShellExecute(hwnd, "open", "C:\My Documents\Book1.xls", "", _      "C:\", SW_SHOWNORMAL)End Sub

ฟังก์ชันShellExecute()เปิด หรือพิมพ์แฟ้มที่ระบุ ต่อไปนี้คือ ข้อมูลเกี่ยวกับ ShellExecute() จากหน้า 901 904 ของ Microsoft Windows ซอฟต์แวร์พัฒนา Kit (SDK) "ของนักเขียนโปรแกรมการอ้างอิง ปริมาตร 2:Functions"

พารามิเตอร์

                Parameter            Description---------------------------------------------------------------------------   hwnd                 Identifies the parent window.   lpszOp               A string specifying the operation to perform. This                        string can be "open" or "print".   lpszFile             Points to a string specifying the file to open.   lpszParams           Points to a string specifying parameters passed to                        the application when the lpszFile parameter                        specifies an executable file. If lpszFile points to                        a string specifying a document file, this parameter                        is NULL.   lpszDir              Points to a string specifying the default                        directory.   fsShowCmd            Specifies whether the application window is to be                        shown when the application is opened. This                        parameter can be one of the following values:      Value          Meaning      ---------------------------------------------------------------------      0              Hides the window and passes activation to another                     window.      1              Activates and displays a window. If the window is                     minimized or maximized, Windows restores it to its                     original size and position (same as 9).      2              Activates a window and displays it as an icon.      3              Activates a window and displays it as a maximized                     window.      4              Displays a window in its most recent size and                     position. The window that is currently active remains                     active.      5              Activates a window and displays it in its current                     size and position.      6              Minimizes the specified window and activates the                     top-level window in the system's list.      7              Displays a window as an icon. The window that is                     currently active remains active.      8              Displays a window in its current state. The window                     that is currently active remains active.      9              Activates and displays a window. If the window is                     minimized or maximized, Windows restores it to its                     original size and position (same as 1).

ส่งกลับค่า

ค่าที่ส่งคืนมีหมายเลขอ้างอิงอินสแตนซ์ของแอพลิเคชันที่ถูกเปิด หรือ พิมพ์ ถ้าฟังก์ชันประสบความสำเร็จ (จุดจับนี้ยังอาจหมายเลขอ้างอิงของโปรแกรมประยุกต์เซิร์ฟเวอร์ DDE) การคืนค่าน้อยกว่า หรือเท่ากับ 32 ข้อผิดพลาดที่ระบุ

ข้อผิดพลาด

ฟังก์ชันShellExecute()ส่งกลับค่า 31 ถ้าไม่มีการเชื่อมโยงสำหรับชนิดแฟ้มที่ระบุ หรือ ถ้าไม่มีความสัมพันธ์สำหรับการดำเนินการที่ระบุภายในชนิดของแฟ้ม ค่าข้อผิดพลาดที่เป็นไปได้อื่น ๆ มีดังนี้:Value Meaning--------------------------------------------------------------------------- 0 System was out of memory, executable file was corrupt, or relocations were invalid. 2 File was not found. 3 Path was not found. 5 Attempt was made to dynamically link to a task, or there was a sharing or network-protection error. 6 Library required separate data segments for each task. 8 There was insufficient memory to start the application. 10 Windows version was incorrect. 11 Executable file was invalid. Either it was not a Windows application, or there was an error in the .exe image. 12 Application was designed for a different operating system. 13 Application was designed for MS-DOS 4.0. 14 Type of executable file was unknown. 15 Attempt was made to load a real-mode application (developed for an earlier version of Windows). 16 Attempt was made to load a second instance of an executable file containing multiple data segments that were not marked read-only. 19 Attempt was made to load a compressed executable file. The file must be decompressed before it can be loaded. 20 Dynamic-link library (DLL) file was invalid. One of the DLLs required to run this application was corrupt. 21 Application requires Microsoft Windows 32-bit extensions.

ข้อคิดเห็น

แฟ้มที่ระบุ โดยพารามิเตอร์ lpszFile สามารถเป็นแฟ้มเอกสารหรือแฟ้มปฏิบัติการได้ ถ้าเป็นไฟล์เอกสาร ฟังก์ชันนี้เปิด หรือพิมพ์ ขึ้นอยู่กับค่าของพารามิเตอร์ lpszOp ถ้าเป็นแฟ้มปฏิบัติการได้ ฟังก์ชันนี้เปิด แม้ว่าสายอักขระ "พิมพ์" จะชี้ไปตาม lpszOp

ข้อมูลอ้างอิง

สำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีการใช้ตัวอย่างรหัสในบทความนี้ คลิกหมายเลขบทความต่อไปนี้เพื่อดูบทความในฐานความรู้ของ Microsoft:

212536 OFF2000: วิธีการเรียกใช้โค้ดตัวอย่างจากบทความฐานข้อมูลองค์ความรู้สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการรับความช่วยเหลือเกี่ยวกับ Visual Basic สำหรับโปรแกรมประยุกต์ โปรดดูบทความฐานความรู้ของ Microsoft ต่อไปนี้:

226118 OFF2000: เขียนโปรแกรมทรัพยากรสำหรับ Visual Basic สำหรับโปรแกรมประยุกต์Microsoft Windows SDK "การอ้างอิงของนักเขียนโปรแกรม ปริมาตร 2: ฟังก์ชัน, " หน้า 901 904

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

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

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