Domanda

I am a bit new to Outlook forms, but not to VBA overall - nor HTML/Web design of forms. 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.

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? Otherwise, it would be a TON of SELECT CASE statements in VBA =/

Nessuna soluzione corretta

Altri suggerimenti

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.
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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top