Aanmelden met Microsoft
Meld u aan of maak een account.
Hallo,
Selecteer een ander account.
U hebt meerdere accounts
Kies het account waarmee u zich wilt aanmelden.

Samenvatting

U kunt de functie ShellExecute() Windows API aanroepen vanuit een Visual Basic for Applications-macro om een ander programma onder Microsoft Windows te starten. ShellExecute() in plaats van de Shell (een Visual Basic-instructie) of WinExec() (een Windows API-functie) gebruiken om de volgende beperking van de laatste opdrachten te omzeilen:

Met Shell en WinExec() kunt u een toepassing niet starten door alleen een bestandsnaam aan te geven. Bijvoorbeeld de volgende Shell-instructie zal mislukken:

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

Meer informatie

Microsoft verstrekt programmeervoorbeelden dienen uitsluitend ter illustratie expliciete of impliciete. Dit omvat, maar is niet beperkt tot, de impliciete garanties van verkoopbaarheid of geschiktheid voor een bepaald doel. In dit artikel wordt ervan uitgegaan dat u bekend met de programmeertaal die wordt aangetoond en met de hulpprogramma's die worden gebruikt bent voor het maken van en naar procedures voor foutopsporing. Ondersteuningstechnici van Microsoft kunnen voor uitleg over de functionaliteit van een bepaalde procedure, maar zij zullen deze voorbeelden bieden extra functionaliteit of desgewenst uw specifieke vereisten niet wijzigen. Hieronder vindt u een voorbeeld van Visual Basic for Applications-macro die de Windows API-functie ShellExecute() aanroept. ShellExecute() bepaalt of Microsoft Excel reeds actief is; in dat geval wordt Book1.xls geladen in de huidige sessie van Microsoft Excel. Als Microsoft Excel nog niet actief is, worden ShellExecute() Microsoft Excel wordt gestart en laadt 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

De functie ShellExecute() wordt geopend of het opgegeven bestand wordt afgedrukt. Hier volgt informatie over ShellExecute() van 901 904 's van de Microsoft Windows Software Development Kit (SDK) "Programmer's Reference 2:Functions Volume."

Parameters

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

Geeft als resultaat

De geretourneerde waarde is de sessiehandler van de toepassing die werd geopend of afgedrukt, als de functie voltooid is. (Deze koppeling kan ook de greep van een DDE-servertoepassing worden.) Een return waarde kleiner is dan of gelijk aan 32 geeft een fout.

Fouten

De functie ShellExecute() retourneert de waarde 31 als er geen koppeling voor het opgegeven bestandstype of als er geen koppeling voor de opgegeven actie binnen het bestandstype. De andere mogelijke waarden zijn als volgt:


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.

Opmerkingen

Het bestand dat is opgegeven met de parameter lpszFile kan een document of een uitvoerbaar bestand zijn. Als het een bestand is, wordt deze functie wordt geopend of afgedrukt, afhankelijk van de waarde van de parameter lpszOp. Als het een uitvoerbaar bestand is, deze functie wordt geopend, zelfs als de tekenreeks 'afdrukken' is waarnaar wordt verwezen door lpszOp.

Referenties

Voor meer informatie over het gebruik van de voorbeeldcode in dit artikel, klikt u op het volgende artikelnummer om het artikel in de Microsoft Knowledge Base:

212536 Office 2000: voorbeeldcode uit Knowledge Base-artikelen uitvoeren
Voor meer informatie over het opvragen van help voor Visual Basic for Applications, raadpleegt u het volgende artikel in de Microsoft Knowledge Base:

226118 Office 2000: Programming Resources for Visual Basic for Applications


Microsoft Windows SDK "Programmer's Reference, Volume 2: functies, ' 901 904 pagina's

Meer hulp nodig?

Meer opties?

Verken abonnementsvoordelen, blader door trainingscursussen, leer hoe u uw apparaat kunt beveiligen en meer.

Community's helpen u vragen te stellen en te beantwoorden, feedback te geven en te leren van experts met uitgebreide kennis.

Was deze informatie nuttig?

Hoe tevreden bent u met de taalkwaliteit?
Wat heeft uw ervaring beïnvloed?
Als u op Verzenden klikt, wordt uw feedback gebruikt om producten en services van Microsoft te verbeteren. Uw IT-beheerder kan deze gegevens verzamelen. Privacyverklaring.

Hartelijk dank voor uw feedback.

×