2012-06-25 105 views
7

我在網絡共享上有一個.dotm模板文件。有對Word,Office和Outlook對象庫的引用的宏。我們使用兩種不同的平臺,Windows XP和Windows 7以及Microsoft Office 2007和Office 2010.當用戶打開模板文件時,Word和Office的引用將自動調整並相應地進行調整(即,它們設置爲Microsoft Word 12對象庫或Microsoft Word 14對象庫),並且宏運行沒有問題。Outlook對象庫不能在版本12和14之間切換

Microsoft Outlook Object Library從版本12正確切換到14.它無法從版本14正確切換到12.在這種情況下,它會提供找不到該庫的錯誤。這是一個錯誤?有沒有解決方法?我忽略的東西?

+5

我相信你正在使用'EarlyBinding'你有沒有考慮過使用'LateBinding(LB)'? LB的好處是你不必擔心在不同的計算機上運行不同的版本。代碼將始終與正在運行代碼的pc中存在的版本綁定。請參閱他的鏈接:http://support.microsoft.com/kb/245115 –

+0

謝謝。我保留這個選項。相反,我問爲什麼其他兩個引用自動更新在任何方向,但該Outlook具體而言,不。如果是這樣,我將不得不使用後期綁定,你 – ForEachLoop

+0

我遇到了同樣的問題,使用Excel VBA。我還注意到,對Microsoft Office和Microsoft Excel庫的(早期綁定)引用會自動在版本12和14之間切換,但對Microsoft Outlook的引用不會。也就是說,它的自動更改從12到14,但從未回到12. – comecme

回答

3

ForEachLoop,

看來您的問題已基本得到解答。爲了清楚起見,我只會添加一些信息,並提供這個問題的答案。正如Siddarth Rout所述,Microsoft論壇Ossiemac上的用戶指出LateBinding was the way to go。正如Siddarth所暗示的那樣,這意味着您不必擔心引用。 -

〜約爾

Private Sub btnLateBindMethod_Click() 
    ' Variables used for LateBinding 
    Dim objOutlook As Object 'Outlook.Application 
    Dim objEmail As Object  'Outlook.MailItem  
    Dim objNameSpace As Object 'Outlook.NameSpace  
    Const OutLookMailItem As Long = 0 'For Late Binding 
    Const OutLookFolderInbox As Long = 6 'For Late Binding 
    Const OutLookFormatHTML As Long = 2 'For Late Binding 
    Dim strSubject As String 
    Dim strAddress As String 


On Error Resume Next 
Set objOutlook = GetObject(, "Outlook.Application") 
On Error GoTo 0  

    If objOutlook Is Nothing Then 
     Set objOutlook = CreateObject("Outlook.Application") 
     Set objNameSpace = objOutlook.GetNamespace("MAPI") 
     objNameSpace.GetDefaultFolder(OutLookFolderInbox).Display 
    End If 

Set objEmail = objOutlook.CreateItem(OutLookMailItem) 

strSubject = "Hello World" 

    With objEmail 

     '.To = strToAddress 'Commented to prevent accidental send 

     .Subject = strSubject 

     .BodyFormat = OutLookFormatHTML 

     .Display 

     'Full Name of window can change depending on Tools -> Options -> Mail Format 
     'Changing this option for outgoing mail changes the window name. 
     'However, AppActivate appears not to require entire name but needs up to end 
     'of - Message which is included in heading following the Subject string 
     'irrespective of the Mail Format option chosen. 
     AppActivate (strSubject & " - Message") 

    End With  
End Sub 

吉米·佩納有一篇文章討論contrast of EarlyBinding and LateBinding

Ossiemac對電子郵件的發送,我已經重新格式化並放在這裏使用LateBinding提供了一些示例代碼

+1

謝謝。我最終使用了後期綁定。這是一篇很棒的文章!該解決方案很尷尬,因爲你失去了編輯器的許多酷功能。另外,如果您使用編譯器(例如Visual Studio),則可以解決此問題,例如,通過並行加載Word和Excel 2007和2010並選擇要編譯的版本。不幸的是,兩個版本的Outlook不能並行加載,所以無論如何你都會遇到遲滯綁定。 – ForEachLoop

+0

迷人!我很高興它的功能,並且通過使用VS進行編譯的不同版本的單詞之間傳遞的概念將在未來進行進一步的探索......感謝有關實現的細節! 〜JOL – JackOrangeLantern

+0

謝謝你,這是非常有用的。我必須爲Google提供所需的所有常量,因爲我正在處理日曆約會而不是郵件項目,但是這又讓我重新開始運行。由於Outlook的不同版本,我必須這樣做。 –

相關問題