Hello, COM(VBA) World!

Posted on 5月 13th, 2012 by cx20

COM(VBA)

COM(Component Object Model)はマイクロソフトの提唱するプログラム部品の仕様である。
COM を用いて開発された部品であれば言語を問わず利用することができる。
以下は VBA による COM クライアント(事前バインディングならびに実行時バインディング)の例となっている。

ソースコード(事前バインディング)

Sub Main()
    Dim shell As New Shell32.shell
 
    Dim vRootFolder
    vRootFolder = Shell32.ShellSpecialFolderConstants.ssfWINDOWS
 
    Dim folder As Shell32.folder
    Set folder = shell.BrowseForFolder(0, "Hello, COM(VBA) World!", 0, vRootFolder)
 
    If Not folder Is Nothing Then
        Set folder = Nothing
    End If
    Set shell = Nothing
End Sub

事前バインディング(参照設定)の設定有無は、以下のコードで確認することが可能である。

ソースコード(参照設定確認)

Sub ShowReferences()
    Dim vbp
    Set vbp = ActiveWorkbook.VBProject
 
    Dim ref
    For Each ref In vbp.References
        If ref.BuiltIn = False Then
            Debug.Print "[" & ref.Name & "]"
            Debug.Print "GUID        : [" & ref.GUID & "]"
            Debug.Print "FullPath    : [" & ref.FullPath & "]"
            Debug.Print "Description : [" & ref.Description&; "]"
            Debug.Print ""
        End If
    Next
End Sub

実行結果(参照設定確認)

[stdole]
GUID        : [{00020430-0000-0000-C000-000000000046}]
FullPath    : [C:\Windows\system32\stdole2.tlb]
Description : [OLE Automation]
 
[Office]
GUID        : [{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}]
FullPath    : [C:\Program Files\Common Files\Microsoft Shared\OFFICE14\MSO.DLL]
Description : [Microsoft Office 14.0 Object Library]
 
[Shell32]
GUID        : [{50A7E9B0-70EF-11D1-B75A-00A0C90564FE}]
FullPath    : [C:\Windows\system32\shell32.dll]
Description : [Microsoft Shell Controls And Automation]

ソースコード(実行時バインディング)

Sub Main()
    Dim shell
    Set shell = CreateObject("Shell.Application")
 
    Dim vRootFolder
    vRootFolder = 36 ' ssfWINDOWS
 
    Dim folder
    Set folder = shell.BrowseForFolder(0, "Hello, COM(VBA) World!", 0, vRootFolder)
 
    If Not folder Is Nothing Then
        Set folder = Nothing
    End If
    Set shell = Nothing
End Sub

実行結果

+----------------------------------------+
|Browse For Folder                    [X]|
+----------------------------------------+
| Hello, COM(VBA) Wolrd!                 |
|                                        |
| +------------------------------------+ |
| |[Windows]                           | |
| | +[addins]                          | |
| | +[AppCompat]                       | |
| | +[AppPatch]                        | |
| | +[assembly]                        | |
| |     :                              | |
| |     :                              | |
| |     :                              | |
| +------------------------------------+ |
| [Make New Folder]    [  OK  ] [Cancel] |
+----------------------------------------+

Tags:

Categories: COM, VBA

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

WP-SpamFree by Pole Position Marketing