Consulting

Results 1 to 3 of 3

Thread: Early?late Binding?

  1. #1
    VBAX Regular
    Joined
    Jul 2005
    Posts
    74
    Location

    Early?late Binding?

    From Excel, I prepare a small Word memo
    [VBA]
    Public Sub Memo()
    Dim a As Integer, b As Integer, c As Integer, d As Integer
    Dim WordApp As Object
    Dim myrange1 As Object, myrange2 As Object
    Dim wordDoc As Object
    Set WordApp = CreateObject("Word.Application")

    SaveAsName = ThisWorkbook.Path & "\" & "wren" _
    & Format(Date, "mmddyyyy") & ".doc"

    With WordApp
    .documents.Add
    Set wordDoc = WordApp.activedocument
    With .Selection
    .Font.Size = 14
    .Font.Bold = True
    .paragraphformat.Alignment = 1
    .typetext Text:="MY REPORT"
    .typeparagraph
    .typeparagraph
    .Font.Size = 12
    .paragraphformat.Alignment = 0
    .Font.Bold = False
    .typetext Text:="Date:" & vbTab & _
    Format(Date, "mmmm d, yyyy")
    [/VBA]

    Obviosly not the whole routine, but as Word is set as an object, this would be an example of late binding.

    so when i use

    [VBA]
    Public Sub Memo()
    Dim a As Integer, b As Integer, c As Integer, d As Integer
    Dim WordApp As Word.Application
    Dim myrange1 As Object, myrange2 As Object
    Dim wordDoc As Word.Document
    Set WordApp = New Word.Application
    SaveAsName = ThisWorkbook.Path & "\" & "wren" _
    & Format(Date, "mmddyyyy") & ".doc"

    With WordApp
    .documents.Add
    Set wordDoc = WordApp.activedocument
    With .Selection
    .Font.Size = 14
    .Font.Bold = True
    .paragraphformat.Alignment = 1
    .typetext Text:="MY REPORT"
    .typeparagraph
    .typeparagraph
    .Font.Size = 12
    .paragraphformat.Alignment = 0
    .Font.Bold = False
    .typetext Text:="Date:" & vbTab & _
    Format(Date, "mmmm d, yyyy")
    [/VBA]

    I would have expected this example of "very early" binding to execute significantly quicker, but I was disappointed. Any ideas as to why. Is my binding strategy flawed in some way? I have set the reference to Word.
    Last edited by clvestin; 12-21-2005 at 07:35 AM. Reason: Add'l info

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    There is not enough travel back and forth between Excel and the Word type library to make the early binding noticeable.

  3. #3
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    I don't see any flaw with what you've done.
    With late-binding, at run-time, the class object must be found and each subsequent reference compiled whereas with early-binding that work is already done.
    I'm not sure what kind of performance increase you were expecting but it is reasonable to expect early-binding to be much more efficient.

    I ran a couple of quick comparison tests (using your code looped a number of times) and found late-binding took around 2-3 times as long as early-binding.
    However, whether this is noticable in a single instance on a fast PC is debatable.
    K :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •