2013-09-25 34 views
2

由於this problem不可解析,因此我正在探索一個用於自動生成InfoPath電子郵件的不同選項。如何在對象接管時使用CreateObject並傳遞用戶輸入?

下面的代碼工作,除了一旦我使用.Submit它會提示我使用輸入 - 基本上InfoPath窗口要求我在對話框中點擊「發送」。

我希望有一種方法可以基本上做下面的代碼在僞代碼中做的事情。問題是當.Submit被調用時(documentation here - 看起來好像沒有任何標誌可以通過/設置忽略這個),VBA等待按鍵繼續(因此SendKeys不能像下面那樣工作,至少按Alt + S)。

請參閱下面的圖像出現確認的例子:

enter image description here

我想要麼抑制這種(即使我不得不改變我是如何創建的InfoPath電子郵件)或能假冒「發送」按鍵。

Private Sub GenerateInfoPathEmail() 

    Dim mFilePath As String 
    Dim oApp As Object 
    Dim objEmailAdapter As EmailAdapterObject 

    mFilePath = "A:\tmp\testform.xsn" 
    Set oApp = CreateObject("InfoPath.Application") 
    oApp.XDocuments.NewFromSolution (mFilePath) 

    'infopath form has a connection called "email" 
    Set objEmailAdapter = oApp.XDocuments(0).DataAdapters("email") 
    objEmailAdapter.To = "[email protected]" 
    objEmailAdapter.CC = "" 
    objEmailAdapter.Intro = "intro" 
    objEmailAdapter.Subject = "subject" 
    objEmailAdapter.AttachmentType = 1 'xdXmlXns 
    objEmailAdapter.AttachmentFileName = mFilePath 

    'I want to be able to pass through 
    'the silly user input here to basically 
    'hit "send" (alt+s for keyboard shortcut) 
    objEmailAdapter.Submit 
    SendKeys ("%s") ' This obviously won't work because the code waits for user input 


End Sub 

回答

0

我不使用InfoPath所以無法測試appraoch那裏,但你可以使用的SendKeys通過填寫所需的字段,並使用{tab}代碼(見下文)來解決這個問題。我希望Tab鍵能夠在用戶輸入表單中的字段之間移動。

有一點要小心,sendkeys可能不可靠,所以它可能不是最好的想法,但如果你找不到另一種解決方法,可能值得一試。

Sub testSendKeys() 
    Dim SendkeyString As String 

    Application.Wait (3)      'Wait 3 seconds to allow 
               'form to load 
    SendkeyString = "[email protected]"  'Fill in the to field that appears 
               'to be selected by default 
    SendkeyString = SendkeyString & "{tab}"  'Tab to CC field 
    SendkeyString = SendkeyString & "{tab}"  'Tab to BCC field 
    SendkeyString = SendkeyString & "{tab}"  'Tab to Subject field 
    SendkeyString = SendkeyString & "subject" 'Fill in subject details 
    SendkeyString = SendkeyString & "%s"  'Alt-S to send 

    SendKeys (SendkeyString) 
End Sub