概要
Visual Basic から Office 製品を自動化する場合、コードの一部を、サーバーのプロセス領域内で実行できる Microsoft Visual Basic for Applications (VBA) モジュールに移動すると便利な場合があります。 これにより、アプリケーションの全体的な実行速度が向上し、プロセス内で呼び出しが行われた場合にのみサーバーがアクションを実行する場合の問題を軽減できます。
この記事では、Visual Basic から実行中の Office アプリケーションに VBA モジュールを動的に追加し、マクロを呼び出してワークシートのインプロセスを埋める方法について説明します。詳細情報
次のサンプルでは、Microsoft Excel にコード モジュールを挿入する方法を示していますが、どちらも同じ VBA エンジンを組み込むため、Word と PowerPoint で同じ手法を使用できます。
このサンプルでは、コード モジュールに静的テキスト ファイルを使用します。このファイルは、Excel。 アプリケーションにコンパイルできるリソース ファイルにコードを移動し、実行時に必要に応じて一時ファイルに抽出することができます。 これにより、プロジェクトの再配布の管理が簡単になります。 MICROSOFT OFFICE XP から、VBA を操作するために記述された Automation コードが機能する前に、ユーザーは VBA オブジェクト モデルへのアクセスを許可する必要があります。 これは、XP を使用した新しいセキュリティOfficeです。 詳細については、次のサポート技術情報の記事を参照してください。282830 プログラムによる XP VBA OfficeへのProjectアクセスが拒否される
サンプルをビルドする手順
-
まず、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 は既定で作成されます。
-
[Project] メニューの [参照] をクリックし、適切な種類のライブラリ バージョンを選択します。このバージョンでは、初期バインドを使用してExcel。
たとえば、次のいずれかを選択します。-
2007 Microsoft Office Excel、12.0 ライブラリを選択します。
-
2003 Microsoft Office Excel、11.0 ライブラリを選択します。
-
2002 Microsoft Excel、10.0 ライブラリを選択します。
-
2000 Microsoft Excel、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 Sub -
2002 Excel以降のバージョンの場合はExcel VBA プロジェクトへのアクセスを有効にする必要があります。 これを行うには、以下のいずれかの方法を使用します。
-
2007 Excelでは、[ボタン] ボタンをMicrosoft Officeし、[オプション] をExcelします。 [セキュリティ センター] をクリックし、[セキュリティ センターの設定] をクリックします。 [マクロ設定をクリックし、[VBA プロジェクト オブジェクト モデルへのアクセスを信頼する] チェック ボックスをオンにし、[OK] を 2 回クリックします。
-
2003 Excel以前のバージョンの Excel で、[ツール] メニューの [マクロ] をポイントし、[セキュリティ] をクリックします。 [セキュリティ] ダイアログ ボックスで、[信頼できるソース] タブをクリックし、[信頼できるソースへのアクセスを信頼する] チェック Visual Basic Project選択します。
-
-
プロジェクトをVisual Basicします。
参照情報
Automation of Office from Visual Basicの詳細については、Office の開発サポート サイトを参照してください。