2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Access_VBA】参照設定_Excel Object LibraryをVerに合わせて自動設定する

Posted at

自分用に記載したメモ

Accessにて、ファイルダイアログを使用しファイルをインポートする際
ExcelやPowerPointのライブラリを使用することが多いと思います。

上記の様な仕組みを導入したツールを作成し、現場に共有した際
ユーザーに参照設定をしてもらうのは論外。
かと言って、何百台のPCひとつひとつに手動で設定するのは手間なので、
以下の様な、仕組みを入れておくと楽。

1.Excelのライブラリ設定を外す
※すでに設定されているライブラリを追加するとエラーになるので。

For Each Ref In Refrences

If Ref.Name = "Excel" Then
    Application.References.Remove Ref
End If

Next Ref

2.ExcelのVerに合わせて、ライブラリを設定

Dim Ref As Reference
Const strExcel As String = "{00020813-0000-0000-C000-000000000046}"

Select Case CLng(Application.Version)
    Case 16: Set Ref = References.AddFromGuid(strExcel, 1, 9)   'Office2016
    Case 15: Set Ref = References.AddFromGuid(strExcel, 1, 8)   'Office2013
    Case 14: Set Ref = References.AddFromGuid(strExcel, 1, 7)   'Office2010
    Case 12: Set Ref = References.AddFromGuid(strExcel, 1, 6)   'Office2007
End Select

Set Ref = Nothing

以上のコードを、作成したツールに則したイベントに挿入。

※以下、例。
 フォームが開いた時 → 参照設定追加
 フォームを閉じた時 → 参照設定外す

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?