Masuk dengan Microsoft
Masuk atau buat akun.
Halo,
Pilih akun lain.
Anda memiliki beberapa akun
Pilih akun yang ingin Anda gunakan untuk masuk.

Ringkasan

Anda dapat memanggil fungsi Windows API ShellExecute() dari Visual Basic untuk aplikasi makro untuk memulai program lain pada Microsoft Windows. Menggunakan ShellExecute() Shell (Visual Basic pernyataan) atau WinExec() (Windows API fungsi) untuk mengatasi keterbatasan berikut perintah kedua:

Shell dan WinExec(), Anda tidak dapat memulai aplikasi dengan menetapkan nama berkas yang hanya. Sebagai contoh, Shell pernyataan berikut akan gagal:

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

Informasi lebih lanjut

Microsoft menyediakan pemrogaman hanya untuk ilustrasi, tanpa garansi baik tersurat maupun tersirat. Ini mencakup, namun tidak terbatas pada, garansi yang tersirat dapat diperjualbelikan atau kesesuaian untuk tujuan tertentu. Artikel ini menganggap bahwa Anda sudah terbiasa dengan bahasa pemrograman yang ditunjukkan dan terbiasa dengan alat-alat yang digunakan untuk membuat dan prosedur debug. Teknisi dukungan Microsoft dapat membantu menjelaskan fungsionalitas prosedur tertentu, namun mereka tidak akan mengubah contoh tersebut untuk memberikan fungsionalitas tambahan atau menyusun prosedur untuk memenuhi persyaratan khusus Anda. Di bawah ini adalah contoh Visual Basic untuk aplikasi makro yang memanggil fungsi Windows API ShellExecute() . ShellExecute() menentukan apakah Microsoft Excel telah menjalankan; Jika demikian, memuat Book1.xls ke sesi Microsoft Excel saat ini. Jika Microsoft Excel tidak sudah berjalan, ShellExecute() memulai Microsoft Excel dan memuat 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 Long

Declare Function apiFindWindow Lib "User32" Alias "FindWindowA" _
(ByVal lpclassname As Any, ByVal lpCaption As Any) As Long

Global Const SW_SHOWNORMAL = 1

Sub ShellExecuteExample()
Dim hwnd
Dim StartDoc
hwnd = apiFindWindow("OPUSAPP", "0")

StartDoc = ShellExecute(hwnd, "open", "C:\My Documents\Book1.xls", "", _
"C:\", SW_SHOWNORMAL)
End Sub

ShellExecute() function terbuka atau mencetak berkas yang ditetapkan. Berikut ini adalah informasi tentang ShellExecute() dari halaman 901-904 dari Microsoft Software Development Kit (SDK) Windows "Programmer referensi, Volume 2:Functions."

Parameter

                
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).

Pengembalian

Nilai kembali adalah contoh menangani aplikasi yang dibuka atau dicetak, jika fungsi yang berhasil. (Handle ini juga dapat menangani aplikasi server DDE.) Kembali nilai kurang dari atau sama dengan 32 menentukan galat.

Galat

Fungsi ShellExecute() mengembalikan nilai 31 jika tidak ada Asosiasi jenis berkas tertentu atau apabila tidak ada Asosiasi untuk tindakan tertentu dalam jenis berkas. Nilai kesalahan kemungkinan lain adalah sebagai berikut:


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.

Komentar

Berkas yang ditentukan oleh lpszFile parameter dapat berkas dokumen atau berkas yang dapat dijalankan. Jika berkas dokumen, fungsi ini terbuka atau mencetak, tergantung pada nilai lpszOp parameter. Jika berkas yang dapat dijalankan, fungsi ini akan membuka, meskipun string "cetak" menunjuk ke oleh lpszOp.

Referensi

Untuk informasi selengkapnya tentang cara menggunakan kode contoh dalam artikel ini, klik nomor artikel di bawah ini untuk melihat artikel di Pangkalan Pengetahuan Microsoft:

212536 OFF2000: cara mengoperasikan kode contoh dari artikel Pangkalan Pengetahuan
Untuk informasi lebih lanjut tentang mendapatkan bantuan dengan Visual Basic untuk aplikasi, baca artikel Pangkalan Pengetahuan Microsoft berikut ini:

226118 OFF2000: pemrograman sumber untuk Visual Basic untuk aplikasi


Microsoft Windows SDK "Programmer referensi, Volume 2: fungsi," halaman 901-904

Perlu bantuan lainnya?

Ingin opsi lainnya?

Jelajahi manfaat langganan, telusuri kursus pelatihan, pelajari cara mengamankan perangkat Anda, dan banyak lagi.

Komunitas membantu Anda bertanya dan menjawab pertanyaan, memberikan umpan balik, dan mendengar dari para ahli yang memiliki pengetahuan yang luas.

Apakah informasi ini berguna?

Seberapa puaskah Anda dengan kualitas bahasanya?
Apa yang memengaruhi pengalaman Anda?
Dengan menekan kirim, umpan balik Anda akan digunakan untuk meningkatkan produk dan layanan Microsoft. Admin TI Anda akan dapat mengumpulkan data ini. Pernyataan Privasi.

Terima kasih atas umpan balik Anda!

×