概要
Visual Basic から Office 製品を自動化する場合、コードの一部をサーバーのプロセス空間内で実行できる Microsoft Visual Basic for Applications (VBA) モジュールに移動すると便利な場合があります。 これにより、アプリケーションの全体的な実行速度が向上し、呼び出しが途中で行われたときにのみサーバーがアクションを実行する場合の問題を軽減するのに役立ちます。
この資料では、Visual Basic から実行中の Office アプリケーションに VBA モジュールを動的に追加し、マクロを呼び出してワークシートに入力する方法を説明します。
追加情報
次のサンプルでは、コード モジュールを Microsoft Excel に挿入する方法を示しますが、Word と PowerPoint には同じ VBA エンジンが組み込まれているため、同じ手法を使用できます。
このサンプルでは、Excel に挿入されたコード モジュールに静的テキスト ファイルを使用します。 アプリケーションにコンパイルできるリソース ファイルにコードを移動し、実行時に必要になったときに一時ファイルに抽出することを検討してください。 これにより、プロジェクトを再配布するためにより管理しやすくなります。
Microsoft Office XP 以降では、VBA を操作するために記述されたオートメーション コードが動作する前に、ユーザーが VBA オブジェクト モデルへのアクセス権を付与する必要があります。 これは、Office XP の新しいセキュリティ機能です。 詳細については、次のサポート技術情報を参照してください。
282830 Office XP VBA プロジェクトへのプログラムのアクセスが拒否される
サンプルをビルドする手順
最初に、KbTest.bas という名前の新しいテキスト ファイルを作成します (.txt 拡張子なし)。 これは、実行時に Excel に挿入するコード モジュールです。
テキスト ファイルに、次のコード行を追加します。
Attribute VB_Name = "KbTest" ' Your Microsoft Visual Basic for Applications macro function takes 1 ' parameter, the sheet object that you are going to fill. Public Sub DoKbTest(oSheetToFill As Object) Dim i As Integer, j As Integer Dim sMsg As String For i = 1 To 100 For j = 1 To 10 sMsg = "Cell(" & Str(i) & "," & Str(j) & ")" oSheetToFill.Cells(i, j).Value = sMsg Next j Next i End Subテキスト ファイルを C:\KbTest.bas ディレクトリに保存し、ファイルを閉じます。
Visual Basic を起動し、標準プロジェクトを作成します。 デフォルトで Form1 が作成されます。
[ プロジェクト ] メニューの [参照] をクリックし、Excel への早期バインドを使用できる適切なタイプ ライブラリ バージョンを選択します。
たとえば、次のいずれかを選択します。
- Microsoft Office Excel 2007 の場合は、12.0 ライブラリを選択します。
- Microsoft Office Excel 2003 の場合は、11.0 ライブラリを選択します。
- Microsoft Excel 2002 の場合は、10.0 ライブラリを選択します。
- Microsoft Excel 2000 の場合は、9.0 ライブラリを選択します。
- Microsoft Excel 97 の場合は、8.0 ライブラリを選択します。
Form1 にボタンを追加し、次のコードをボタンの Click イベントのハンドラに配置します。
Private Sub Command1_Click() Dim oXL As Excel.Application Dim oBook As Excel.Workbook Dim oSheet As Excel.Worksheet Dim i As Integer, j As Integer Dim sMsg As String ' Create a new instance of Excel and make it visible. Set oXL = CreateObject("Excel.Application") oXL.Visible = True ' Add a new workbook and set a reference to Sheet1. Set oBook = oXL.Workbooks.Add Set oSheet = oBook.Sheets(1) ' Demo standard Automation from out-of-process, ' this routine simply fills in values of cells. sMsg = "Fill the sheet from out-of-process" MsgBox sMsg, vbInformation Or vbMsgBoxSetForeground For i = 1 To 100 For j = 1 To 10 sMsg = "Cell(" & Str(i) & "," & Str(j) & ")" oSheet.Cells(i, j).Value = sMsg Next j Next i ' You're done with the first test, now switch sheets ' and run the same routine via an inserted Microsoft Visual Basic ' for Applications macro. MsgBox "Done.", vbMsgBoxSetForeground Set oSheet = oBook.Sheets.Add oSheet.Activate sMsg = "Fill the sheet from in-process" MsgBox sMsg, vbInformation Or vbMsgBoxSetForeground ' The Import method lets you add modules to VBA at ' run time. Change the file path to match the location ' of the text file you created in step 3. oXL.VBE.ActiveVBProject.VBComponents.Import "C:\KbTest.bas" ' Now run the macro, passing oSheet as the first parameter oXL.Run "DoKbTest", oSheet ' You're done with the second test MsgBox "Done.", vbMsgBoxSetForeground ' Turn instance of Excel over to end user and release ' any outstanding object references. oXL.UserControl = True Set oSheet = Nothing Set oBook = Nothing Set oXL = Nothing End SubExcel 2002 以降のバージョンの Excel では、VBA プロジェクトへのアクセスを有効にする必要があります。 これを行うには、以下のいずれかの方法を使用します。
- Excel 2007 では、[ Microsoft Office] ボタンをクリックし、[ Excel のオプション] をクリックします。 [セキュリティ センター] をクリックし、[セキュリティ センターの設定] をクリックします。 [マクロの設定] をクリックし、[VBA プロジェクト オブジェクト モデルへのアクセスを信頼する] チェック ボックスをオンにし、[OK] を 2 回クリックします。
- Excel 2003 およびそれ以前のバージョンの Excel では、[ツール] メニューの [マクロ] をポイントし、[セキュリティ] をクリックします。 [セキュリティ] ダイアログ ボックスで、[信頼されたソース] タブをクリックし、[Visual Basic プロジェクトへのアクセスを信頼する] チェック ボックスをクリックして選びます。
Visual Basic プロジェクトを実行します。
参考資料
Visual Basic から Office を自動化する方法の詳細については、次の場所にある Office 開発サポート サイトを参照してください。