2017-06-20 27 views
0

我有一個列表,其中包含名稱和電子郵件地址的列,後面跟着帶有「發送電子郵件」按鈕的另一列,以啓動下面的宏。Excel VBA - 無法定義應用程序調用者範圍

宏應該通過application.caller函數拉動當前行的位置,然後將行號與預定義的列號結合起來,以在填充我的電子郵件元素之前成功拉取名字,姓氏,電子郵件地址。這似乎不再有效,因爲application.caller返回empty,因此當按下發送電子郵件按鈕時,沒有傳遞單元/行信息。下面

代碼:

Sub Mail_ThankYouNote() 
'Working in 2000-2010 
    Dim OutApp As Object 
    Dim outMail As Object 
    Dim rng As Range 
    Dim lastrow, nationality, SentDate, EmailCount, Title, MailTO, LangStatus As Range 

    With Application 
     .ScreenUpdating = False 
     .EnableEvents = False 
    End With 

    ActiveWorkbook.Sheets("Data").Select 

' check if outlook is open, if not launch application 
    Call CheckOutlook 

' define column locations for nationality, sent-date, email count, title 
    Set OutApp = CreateObject("Outlook.Application") 
    Set outMail = OutApp.CreateItem(0) 
    Set caller_button = ActiveSheet.Cells(ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row, 14) 
    Set nationality = ActiveSheet.Cells(ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row, 7) 
    Set SentDate = ActiveSheet.Cells(ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row, 13) 
    Set EmailCount = ActiveSheet.Cells(ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row, 15) 
    Set Title = ActiveSheet.Cells(ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row, 8) 
    Set MailTO = ActiveSheet.Cells(ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row, 12) 

    MsgBox caller_button, vbOKOnly, "Info" 

' check if title is empty 
    If Title = "" Then 
     MsgBox "The guest title is empty, please fill in a title before sending the post stay email.", vbOKOnly, "No title" 
     Exit Sub 
    End If 

真的不知道是什麼原因造成這個,因爲它以前的工作...

讚賞的任何幫助。 A2K

編輯 事實證明,這是關係到我辦公室2個版本的語言差異的問題。英文複製工作正常,但中文複製要求我刪除按鈕並重新創建它們。據我所知,這是因爲英文版將形狀標識爲(矩形形狀XXX),而中國人將其標識爲UTF 8字符。

刪除形狀並重新創建按鈕的竅門。

回答

2

你確定application.caller返回空嗎?因爲這似乎不太可能。在程序開始時輸入

debug.print application.caller 

然後點擊按鈕,不要使用F8或F5運行它。您應該能夠在即時窗口中看到該值。另外我在使用ActiveX按鈕時遇到了奇怪的問題。嘗試將其更改爲正常按鈕。除非我們看看工作本身,否則很難說爲什麼會出現這個問題。

+0

感謝您的評論。事實證明,這是需要刪除並重新添加的按鈕本身。這似乎是一個語言相關的問題,因爲我的英文版excel沒有問題,但是在我的中文版上遇到了這個錯誤。無論如何,它的工作。非常感謝。 – Armitage2k

相關問題