다른 응용 프로그램의 기능을 사용하려면 개체 변수의 초기 또는 후기 바인딩을 사용할지 결정해야합니다.

초기 바인딩

개체 변수와 개체 간의 바인딩은 응용 프로그램이 컴파일 될 때 발생합니다.

따라서 애플리케이션이 실행될 때 바인딩이 발생하는 경우 (지연 바인딩)에 비해 성능이 향상됩니다.

초기 바인딩을 만들려면 사용할 “외부”개체 라이브러리에 대한 참조를 설정해야합니다.

이 작업은 도구, 참조 …​ 메뉴를 사용하여 VBE에서 수행됩니다. VBProject에 개체 라이브러리에 대한 참조가있을 때 특정 개체 변수 (예 : Dim oDoc As Word.Document)를 선언 할 수 있습니다. 이렇게하면 VBE가 작업중인 응용 프로그램에 속한 개체에 대해 표시하는 속성, 메서드 및 이벤트에 대한 동일한 프로그래밍 도움말을 표시하므로 “외래 개체”를보다 쉽게 ​​프로그래밍 할 수 있습니다 (VBE는 자동으로 이 응용 프로그램을 미리 참조하십시오).

다음은 vba 자동화 오류를 보여주는 일반적인 코드 예제입니다.

Sub OLEAutomationEarlyBinding()

' replace xxx with one of the following:

' Access, Excel, Outlook, PowerPoint or Word

Dim oApp As xxx.Application ' early binding

Dim oDoc As xxx.Document

' Excel.Workbook, Outlook.MailItem, PowerPoint.Presentation, Word.Document

On Error Resume Next ' ignore errors

Set oApp = GetObject(, "xxx.Application")

' reference an existing application instance

If oApp Is Nothing Then ' no existing application is running

Set oApp = New xxx.Application ' create a new application instance

End If

On Error GoTo 0 ' resume normal error handling

If oApp Is Nothing Then ' not able to create the application

MsgBox "The application is not available!", vbExclamation

End If

With oApp

.Visible = True ' make the application object visible

' at this point the application is visible

' do something depending on the application...

Set oDoc = .Documents.Open("c:\foldername\filename.doc")

' open a document

'        ...

oDoc.Close True ' close and save the document

.Quit ' close the application

End With

Set oDoc = Nothing ' free memory

Set oApp = Nothing ' free memory

End Sub

후기 바인딩

개체 변수와 개체 간의 바인딩은 응용 프로그램이 실행될 때 발생합니다.

이로 인해 애플리케이션이 컴파일 될 때 바인딩이 발생할 때 (초기 바인딩)에 비해 성능이 느려집니다.

“외부”응용 프로그램에 속하는 objectlibrary에 대한 참조를 추가하지 않으면 일반 개체 변수 (예 : Dim oDoc As Object)를 선언해야합니다. 이렇게하면 VBE가 작업중인 응용 프로그램에 속한 개체에 대해 표시하는 속성, 메서드 및 이벤트에 대한 동일한 프로그래밍 도움말을 표시하지 않기 때문에 “외부 개체”를 프로그래밍하기가 더 어려워집니다.

다음은 일반적인 코드 예입니다.

Sub OLEAutomationLateBinding()

' replace xxx with one of the following:

' Access, Excel, Outlook, PowerPoint or Word

Dim oApp As Object ' late binding

Dim oDoc As Object ' late binding

On Error Resume Next ' ignore errors

Set oApp = GetObject(, "xxx.Application")

' reference an existing application instance

If oApp Is Nothing Then ' no existing application is running

Set oApp = CreateObject("xxx.Application")

' create a new application instance

End If

On Error GoTo 0 ' resume normal error handling

If oApp Is Nothing Then ' not able to create the application

MsgBox "The application is not available!", vbExclamation

End If

With oApp

.Visible = True ' make the application object visible

' at this point the application is visible

' do something depending on the application...

Set oDoc = .Documents.Open("c:\foldername\filename.doc")

' open a document

'        ...

oDoc.Close True ' close and save the document

.Quit ' close the application

End With

Set oDoc = Nothing ' free memory

Set oApp = Nothing ' free memory

End Sub