Dim wrkbk As Variant
Dim User As Variant
Dim Vers As Variant
Dim memfree As Variant
Dim memtotal As Variant
Dim memused As Variant
On Error Resume Next
wrkbk = Application.ActiveWorkbook.Name
User = Application.UserName
Vers = Application.version
memfree = Application.MemoryFree 'EXCEL 2003 ONLY USE
memtotal = Application.MemoryTotal 'EXCEL 2003 ONLY USE
memused = Application.MemoryUsed 'EXCEL 2003 ONLY USE
MsgBox _
"Memory Total: " & memtotal & Chr(13) & _
"Memory Used: " & memused & Chr(13) & _
"Memory Free: " & memfree & Chr(13) & _
Chr(13) & _
"Name of workbook: " & wrkbk & Chr(13) & _
"Name of User: " & User & Chr(13) & _
"Version of VBA: " & Vers
End Sub
如何: 從 Excel 2000 中的 Windows API 取得 Windows 的狀態資訊來源 URL: http://support.microsoft.com/kb/213267
如何: 從 Excel 2000 中的 Windows API 取得 Windows 的狀態資訊
在 Microsoft Excel 可以以程式設計的方式取得系統狀態資訊,並顯示在工作表中。本文將逐步提供 Microsoft Visual Basic for Applications 範例會顯示下列的資訊,藉由使用 Windows API 函式的 Sub 程序:
* GetVersionEx API 函式的 Windows 版本號碼。
* 具有 GetSystemInfo API 函式的 CPU 處理器型別。
* 使用 GlobalMemoryStatus API 函式的可用記憶體數量。
若要取得 Windows 狀態資訊的範例程式碼Microsoft 會提供程式設計範例僅供說明,不做任何明示或默示的保證,其中包括但不是限於適售性以及適合某特定用途之默示擔保責任。本文假設您已熟悉我們所示範的程式設計語言以及用來建立和偵錯程序的工具。Microsoft 技術支援工程師可以協助解釋特定程序的功能,但它們不會修改這些範例以提供附加功能或建構程序來滿足您的特定需求。
如果您有限制的程式設計經驗,您可能想要連絡微軟認證夥伴 」 或 「 Microsoft 諮詢服務。如需詳細資訊,請造訪下列 Microsoft 網站取得:
Microsoft 認證合作夥伴- https://partner.microsoft.com/global/30000104
Microsoft 諮詢服務- http://support.microsoft.com/gp/advisoryservice
如需有關可用的支援選項,以及有關如何連絡 Microsoft 的詳細資訊,請造訪下列 Microsoft 網站: http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS 若要使用 Windows API 呼叫,以取得 Windows 狀態資訊,請依照下列步驟執行:
1. 啟動 Microsoft Excel,然後建立新的活頁簿。
2. 在 [工具] 功能表上指向 [巨集,然後按一下 [ Visual Basic 編輯器] (或按下 ALT + F11)。
3. 按一下 [插入] 功能表的 [模組]。
4. 在模組工作表中,輸入下列程式碼:
Option Explicit
Type SYSTEM_INFO
dwOemID As Long
dwPageSize As Long
lpMinimumApplicationAddress As Long
lpMaximumApplicationAddress As Long
dwActiveProcessorMask As Long
dwNumberOrfProcessors As Long
dwProcessorType As Long
dwAllocationGranularity As Long
dwReserved As Long
End Type
Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
Type MEMORYSTATUS
dwLength As Long
dwMemoryLoad As Long
dwTotalPhys As Long
dwAvailPhys As Long
dwTotalPageFile As Long
dwAvailPageFile As Long
dwTotalVirtual As Long
dwAvailVirtual As Long
End Type
'The following three Declare lines must be each entered on a single
'line.
Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
(LpVersionInformation As OSVERSIONINFO) As Long
Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As _
MEMORYSTATUS)
Declare Sub GetSystemInfo Lib "kernel32" (lpSystemInfo As _
SYSTEM_INFO)
Public Const PROCESSOR_INTEL_386 = 386
Public Const PROCESSOR_INTEL_486 = 486
Public Const PROCESSOR_INTEL_PENTIUM = 586
Public Const PROCESSOR_MIPS_R4000 = 4000
Public Const PROCESSOR_ALPHA_21064 = 21064
Sub SystemInformation()
Dim msg As String ' Status information.
Dim NewLine As String ' New-line.
Dim ret As Integer ' OS Information
Dim ver_major As Integer ' OS Version
Dim ver_minor As Integer ' Minor Os Version
Dim Build As Long ' OS Build
NewLine = Chr(13) + Chr(10) ' New-line.
' Get operating system and version.
Dim verinfo As OSVERSIONINFO
verinfo.dwOSVersionInfoSize = Len(verinfo)
ret = GetVersionEx(verinfo)
If ret = 0 Then
MsgBox "Error Getting Version Information"
End
End If
Select Case verinfo.dwPlatformId
Case 0
msg = msg + "Windows 32s "
Case 1
msg = msg + "Windows 95/98 "
Case 2
msg = msg + "Windows NT/2000 "
End Select