简体   繁体   English

Outlook 表单:从 Excel 导入/VLOOKUP 数据?

[英]Outlook Forms: Importing / VLOOKUP Data from Excel?

I am a bit new to Outlook forms, but not to VBA overall - nor HTML/Web design of forms.我对 Outlook 表单有点陌生,但对 VBA 整体而言并不陌生——也不是表单的 HTML/Web 设计。 However, my problem is finding a way to combine the two.但是,我的问题是找到一种将两者结合起来的方法。

I am trying to design a form for users to fill out, and based on what they fill out in drop-down box's, it will then tell them what we want them to attach in the email.我正在尝试设计一个供用户填写的表单,并根据他们在下拉框中填写的内容,然后告诉他们我们希望他们在电子邮件中附加什么。 Currently we have this done in Excel, based on dropbox's it then VLOOKUPS to the 2nd Spreadsheet that contains the forms required.目前我们在 Excel 中完成了这项工作,基于 dropbox 的 it 然后 VLOOKUPS 到包含所需表格的第二个电子表格。

Is there anyway I can bring in the Excel with the VLOOKUP behind the scenes in my VBA Outlook Form so that it can look-up what attachments we want the user to do?无论如何,我是否可以在 VBA Outlook 表单的幕后使用 VLOOKUP 引入 Excel,以便它可以查找我们希望用户执行的附件? Otherwise, it would be a TON of SELECT CASE statements in VBA =/否则,它将是 VBA 中的大量 SELECT CASE 语句 =/

This seems to the do the trick for me.这似乎对我有用。 Some of it I have cobbled together from sites like this, the rest has been created by myself from scratch.其中一些是我从这样的网站拼凑而成的,其余的则是我自己从头开始创建的。

When I click my button:当我点击我的按钮时:

  • An input box appears, which is the value that will be looked up in the spreadsheet.出现一个输入框,这是将在电子表格中查找的值。
  • it looks in the range (specified in the code), for a match它在范围内(在代码中指定)查找匹配
  • returns the value, two columns to the left of it.返回值,它左边两列。
  • when it finds a match it puts it in the Subject line in Outlook.当它找到匹配项时,它会将其放在 Outlook 的主题行中。
Dim jobno As String
Dim Proj As String

Sub Test()
    jobno = InputBox("Job Number?", "Test")
    GetNameFromXL
    If jobno <> "" Then
        Set myItem = Application.CreateItem(0)
        If Proj <> "" Then
            myItem.Subject = jobno & " - " & Proj & " - " & Format(Date, "dd.mm.yy")
        Else
            myItem.Subject = jobno & " - " & Format(Date, "dd.mm.yy")
        End If
        myItem.Display
    Else
        Exit Sub
    End If
End Sub


Sub GetNameFromXL()

'Late binding.  No reference to Excel Object required.
Dim xlApp As Object
Dim xlWB As Object
Dim xlWS As Object

Set xlApp = CreateObject("Excel.Application")
'Open the spreadsheet to get data
Set xlWB = xlApp.Workbooks.Open("X:\...\FILENAME.xlsx") ' <-- Put your file path and name here
Set xlWS = xlWB.Worksheets(1) ' <-- Looks in the 1st Worksheet

Debug.Print "-----Start of 'For Each' loop"
For Each c In xlWS.Range("A6:A100") 'Change range to value you want to 'VLookUp'
Proj = c.Offset(0, 2).Value 'This looks at the 2nd column after the range above
Debug.Print c & Proj
    If jobno = c Then
        Debug.Print "-----Match Found:  " & jobno & " = " & Proj
        GoTo lbl_Exit
    Else
    End If
Next c
Debug.Print "-----End of For Each loop"
MsgBox jobno & " not found in WorkBook."

'Clean up
Set xlWS = Nothing
Set xlWB = Nothing
Set c = Nothing
Proj = ""
xlApp.Quit
Set xlApp = Nothing
lbl_Exit:
Exit Sub
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM