Sammanfattning

Du kan anropa funktionen ShellExecute () -API för Windows från ett Visual Basic for Applications-makro för att starta ett annat program i Microsoft Windows. Undvik följande begränsning av dessa kommandon med ShellExecute () i stället för Shell (en Visual Basic-uttryck) eller WinExec() (en Windows-API-funktion):Du kan inte starta ett program genom att ange ett filnamn endast med skal och WinExec(). Till exempel misslyckas följande Shell -sats:

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

Mer Information

Microsoft tillhandahåller programmeringsexempel endast utan garanti varken uttryckliga eller underförstådda. Detta inkluderar men är inte begränsat till, underförstådda garantier om säljbarhet eller lämplighet för ett särskilt ändamål. Den här artikeln förutsätter att du är bekant med det programmeringsspråk som demonstreras och de verktyg som används för att skapa och felsöka procedurer. Microsofts supporttekniker kan hjälpa till med att förklara funktionen hos en viss procedur, men kan inte ändra dessa exempel för att ge ytterligare funktioner eller skapa procedurer som motsvarar dina speciella behov. Nedan är ett exempel på ett Visual Basic for Applications-makro som anropar funktionen ShellExecute () -API: N för Windows. ShellExecute () bestämmer om Microsoft Excel körs redan; Om så är fallet läser in Book1.xls i den aktuella sessionen i Microsoft Excel. Om Microsoft Excel inte redan körs startas Microsoft Excel ShellExecute () och laddar 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 () -funktionen öppnar och skriver ut den angivna filen. Följande är information om ShellExecute () från sidor 901 904 av de Microsoft Windows Software Development Kit (SDK) ”Programmer's Reference, volym 2:Functions”.

Parametrar

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

Returer

Returvärdet är handtaget instans av programmet som öppnas eller skrivs ut, om funktionen lyckas. (Den här referensen kan också vara referensen till ett DDE-serverprogram.) En RETUR värde som är mindre än eller lika med 32 anger ett fel.

Fel

ShellExecute () -funktionen returnerar värdet 31 om ingen association för den angivna filtypen eller om det finns likheter för den angivna åtgärden inom filtypen. Andra möjliga felvärden är följande: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.

Kommentarer

Filen som anges av parametern lpszFile kan vara en dokumentfil eller en körbar fil. Om det är en dokumentfil funktionen öppnas eller skrivs ut, beroende på värdet för parametern lpszOp. Om det är en körbar fil öppnas den funktionen även om strängen ”Skriv ut” är som leder till lpszOp.

Referenser

Mer information om hur du använder exempelkoden i den här artikeln klickar du på artikelnumret nedan och läser artikeln i Microsoft Knowledge Base:

212536 OFF2000: hur du kör exempelkod från artiklar i Knowledge BaseMer information om hur du får hjälp med Visual Basic for Applications finns i följande artikel i Microsoft Knowledge Base:

226118 OFF2000: programmeringsresurser för Visual Basic for ApplicationsMicrosoft Windows SDK ”Programmer's Reference, volym 2: funktioner”, sidor 901 904

Behöver du mer hjälp?

Vill du ha fler alternativ?

Utforska prenumerationsförmåner, bläddra bland utbildningskurser, lär dig hur du skyddar din enhet med mera.