首页
/
每日頭條
/
科技
/
vba調用api能幹什麼
vba調用api能幹什麼
更新时间:2026-06-30 16:42:21

  程序語言開發應用程序都可以調用系統庫和其它應用程序的外部庫,VBA也不例外。

  1 操作系統API調用 API stands for Application Programming Interface.

  API表示(操作系統提供的)應用程序編程接口。

  APIs for VBA imply a set of methods that allow direct interaction with the operating system.

  VBA的API意味着一組允許與操作系統直接交互的方法。

  System calls can be made by executing procedures defined in DLL files.

  可以通過執行DLL文件中定義的過程來進行系統調用。

  引入Windows API庫的某個函數或過程需要在模塊的頂點聲明,如

  Declare PtrSafe Function GetWindowsDirectoryA Lib kernel32 _ (ByVal lpBuffer As String, ByVal nSize As Long) As Long

  declare:在模塊級用于聲明對動态鍊接庫(DLL)中的外部過程的引用;

  PtrSafe:同時兼容Excel的32位和64位版本;

  GetWindowsDirectoryA:函數名,可以VBA過程或函數中調用;

  kernel32:表示上述函數所在的動态庫(DLL);

  該函數參數lpBuffer:返回Windows所在目錄名稱;

  該函數參數nSize:Windows所在目錄名稱的字符串長度包含在此參數中;

  如果代碼模塊是UserFor、Sheet或ThisWorkbook的代碼模塊,就必須用Private關鍵字聲明這個API函數。

  1.1 Lib User32 Function

  Option Explicit GetSystemMetrics32 info: http://msdn.microsoft.com/en-us/library/ms724385(VS.85).aspx #If Win64 Then Private Declare Function GetSystemMetrics32 Lib User32 Alias GetSystemMetrics (ByVal nIndex As Long) As Long #ElseIf Win32 Then Private Declare Function GetSystemMetrics32 Lib User32 Alias GetSystemMetrics (ByVal nIndex As Long) As Long #End If VBA Wrappers: Public Function dllGetMonitors() As Long Const SM_CMONITORS = 80 dllGetMonitors = GetSystemMetrics32(SM_CMONITORS) End Function Public Function dllGetHorizontalResolution() As Long Const SM_CXVIRTUALSCREEN = 78 dllGetHorizontalResolution = GetSystemMetrics32(SM_CXVIRTUALSCREEN) End Function Public Function dllGetVerticalResolution() As Long Const SM_CYVIRTUALSCREEN = 79 dllGetVerticalResolution = GetSystemMetrics32(SM_CYVIRTUALSCREEN) End Function Public Sub ShowDisplayInfo() Debug.Print Total monitors: & vbTab & vbTab & dllGetMonitors Debug.Print Horizontal Resolution: & vbTab & dllGetHorizontalResolution Debug.Print Vertical Resolution: & vbTab & dllGetVerticalResolution Total monitors: 1 Horizontal Resolution: 1920 Vertical Resolution: 1080 End Sub

  1.2 Lib kernel32 Sub

  Private Declare Sub Sleep Lib kernel32 (ByVal dwMilliseconds As Long) Public Sub TestPause() Dim start As Double start = Timer Sleep 9000 Pause execution for 9 seconds Debug.Print Paused for & Format(Timer - start, #,###.000) seconds Immediate window result: Paused for 9.000 seconds End Sub

  更加細節參考:Excel VBA|在VBA中調用Windows API庫中函數-今日頭條

  可以在下面的地址中查看Windows API文檔:http://www.office-cn.net/t/api/index.html?apihelp.htm

  vba調用api能幹什麼(操作系統API調用和使用其它應用程序的對象庫)(1)

  2 引用其它應用程序的對象庫 If you use the objects in other applications as part of your Visual Basic application, you may want to establish a reference to the object libraries of those applications (Such as Windows Shell, Internet Explorer, XML HttpRequest, and others).

  如果将其他應用程序中的對象用作Visual Basic應用程序的一部分,則可能需要建立對這些應用程序的對象庫的引用,如Windows Shell、Internet Explorer、XML HttpRequest等。

  包括的文件類型有:

  類型庫 (*.olb, *.tlb, *.dll) 可執行文件 (*.exe, *.dll) ActiveX控件 (*.ocx) 所有文件 (*.*)

  更多細節參考:VBA|如何添加外部對象庫(或控件)引用來擴展VBA功能

  如使用Microsoft VBScript Regular Expressions可引用以下庫:

  vba調用api能幹什麼(操作系統API調用和使用其它應用程序的對象庫)(2)

  即可建立以下對象

  Set createVBScriptRegExObject = CreateObject(vbscript.RegExp)

  demo code:

  Populate, enumerate, locate and remove entries in a dictionary that was created with late binding Sub iterateDictionaryLate() Dim k As Variant, dict As Object Set dict = CreateObject(Scripting.Dictionary)// 引用Microsoft Scripting Runtime dict.CompareMode = vbTextCompare non-case sensitive compare model populate the dictionary dict.Add Key:=Red, Item:=Balloon dict.Add Key:=Green, Item:=Balloon dict.Add Key:=Blue, Item:=Balloon iterate through the keys For Each k In dict.Keys Debug.Print k - & dict.Item(k) Next k locate the Item for Green Debug.Print dict.Item(Green) remove key/item pairs from the dictionary dict.Remove blueremove individual key/item pair by key dict.RemoveAll remove all remaining key/item pairs End Sub

  Access ADODB.Connection 需要引用:

  vba調用api能幹什麼(操作系統API調用和使用其它應用程序的對象庫)(3)

  demo code:

  Const SomeDSN As String = DSN=SomeDSN;Uid=UserName;Pwd=MyPassword; Public Sub Example() Dim database As ADODB.Connection Set database = OpenDatabaseConnection(SomeDSN) If Not database Is Nothing Then ... Do work. database.Close Make sure to close all database connections. End If End Sub Public Function OpenDatabaseConnection(ConnString As String) As ADODB.Connection On Error GoTo Handler Dim database As ADODB.Connection Set database = New ADODB.Connection With database .ConnectionString = ConnString .ConnectionTimeout = 10 Value is given in seconds. .Open End With OpenDatabaseConnection = database Exit Function Handler: Debug.Print Database connection failed. Check your connection string. End Function

  更多細節參考:VBA|數據庫操作01:使用ADO訪問數據庫-今日頭條

  Collection無須外部引用:

  Public Sub Example() Dim foo As New Collection With foo .Add One .Add Two .Add Three .Add Four End With Debug.Print foo.Count Prints 4 End Sub

  ref:

  《VBA Notes For Professionals》

  htt

Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
直播平台源碼選擇
直播平台源碼選擇
搭建直播源碼網站最重要的就是實現直播功能,直播的實現離不開推流。直播源碼網站實現直播推拉流需要用到推流SDK,這樣才能保證推流的順利進行,如果需要實現直播連麥的話,推流SDK就不再适用了,那麼應該怎樣直播連麥呢?直播源碼網站一、音視頻SDK...
2026-06-30
聯想筆記本打開黑屏是什麼原因
聯想筆記本打開黑屏是什麼原因
筆記本是一種常見的電子設備,可以說是筆記本最常見的配件之一了。許多人都有購買筆記本的習慣。其中聯想筆記本就是一種較為常見的品牌,因為有着良好的性能和優秀的産品,所以受到了很多消費者們的喜愛。但是對于聯想筆記本黑屏開不了機,大家并不是很了解原...
2026-06-30
手機端開會時錄音轉文字免費軟件
手機端開會時錄音轉文字免費軟件
大家都知道我們的手機裡面深藏了很多會議神器,但是下面這一個你見過嗎?1.錄音轉文字助手第一個隐藏的逆天功能就是錄音轉文字助手這個小程序啦,通過這個小程序,我們可以輕松實現會議錄音轉換為文字的功能,免去手抄的煩惱,大幅提升辦公效率,讓你快速成...
2026-06-30
win7安裝vmware虛拟機教程
win7安裝vmware虛拟機教程
當今,虛拟化技術已經非常成熟,相關産品也好幾種,但最實用、最常用的就當屬VMware虛拟機。VMwareWorkstation是VMware公司的專業虛拟機軟件老牌産品,可以虛拟現有任何操作系統,而且使用簡單、容易上手。首先要上官網下載軟件...
2026-06-30
用了9年的電腦
用了9年的電腦
很多不大懂電腦的朋友,在裝配主機的時候,很容易被有些不良商家忽悠,高U低顯、高顯低U,或者是高端CPU配一塊低端主闆,要麼就是配個垃圾電源。上大學時很喜歡玩遊戲,為了讓遊戲運行更流暢,就老去研究電腦硬件,隔壁宿舍有個小胖當時算是個電腦高手,...
2026-06-30
Copyright 2023-2026 - www.tftnews.com All Rights Reserved