SyntaxSocialist
Registered User.
- Local time
- , 20:16
- Joined
- Apr 18, 2013
- Messages
- 109
Hi all. Hopefully I'm not duplicating a topic here, but I haven't been able to find what I've been looking for elsewhere so here goes:
I've built an HTML web form that sends an email to me upon submission with all the field inputs. I have previously found success using VBA in Outlook to parse out these emails and feed the substrings into an Excel Sheet (hooray!), but now am looking to feed the substrings into Access, specifically into a table in an existing .mdb file. I figure the way Outlook will "talk to" Access will likely be a bit different from the way it does for Excel, so I'd greatly appreciate any pointers, resources, starting points, or the like that could get me on my way.
The way my previous script engaged with Excel was through a rule; every time a new email was received, the following script would run if the message had the subject line generated by my web form. The script would check whether Excel and the target workbook/sheet was open, act accordingly, and then input the substrings (I've excluded that part below), then return Excel and the wb/ws to their initial state. I imagine though, that with Access I might be able to feed the substrings into the .mdb without having to open it proper, the way a front-end talks to a back-end. But perhaps I'm mistaken on that front.
I've built an HTML web form that sends an email to me upon submission with all the field inputs. I have previously found success using VBA in Outlook to parse out these emails and feed the substrings into an Excel Sheet (hooray!), but now am looking to feed the substrings into Access, specifically into a table in an existing .mdb file. I figure the way Outlook will "talk to" Access will likely be a bit different from the way it does for Excel, so I'd greatly appreciate any pointers, resources, starting points, or the like that could get me on my way.
The way my previous script engaged with Excel was through a rule; every time a new email was received, the following script would run if the message had the subject line generated by my web form. The script would check whether Excel and the target workbook/sheet was open, act accordingly, and then input the substrings (I've excluded that part below), then return Excel and the wb/ws to their initial state. I imagine though, that with Access I might be able to feed the substrings into the .mdb without having to open it proper, the way a front-end talks to a back-end. But perhaps I'm mistaken on that front.
Code:
Option Explicit
Option Compare Text
Public Const xlUp As Integer = -4162 'I genuinely do not know what this is _
about. It came with the initial _
borrowed code.
Public Const wbPath As String = "C:\...Workbook.xlsx" 'Workbook path
Public Const wbName As String = "Woorkbook.xlsx" 'Workbook file name
Public Const wbSheet1 As String = "Sheet1" 'Title of 1st sheet in WB
Public Sub ExportToExcel(MyMail As MailItem)
Dim olMail As Outlook.MailItem
Dim olNS As Outlook.NameSpace
Dim oXLApp As Object, oXLwb As Object, oXLws As Object
Dim i As Byte 'Counter
Dim lRow As Byte 'Rows in WS
Dim excelOpen As Boolean, wbOpen As Boolean
excelOpen = False
wbOpen = False
Dim strID As String, respCut As String
strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set olMail = olNS.GetItemFromID(strID)
'Retrieve/establish an Excel application object
On Error GoTo NoExcel: 'See bottom of Sub
Set oXLApp = GetObject(, "Excel.Application")
excelOpen = True
If IsWorkBookOpen(wbPath) = True Then
wbOpen = True
End If
RunUpdates:
'Open the relevant file if not already opened
If wbOpen = False Then
Set oXLwb = oXLApp.Workbooks.Open(wbPath)
Else
Set oXLwb = oXLApp.Workbooks(wbName)
End If
Set oXLws = oXLwb.Sheets(wbSheet1)
'Find first empty row to enter data
lRow = oXLws.Range("A" & oXLApp.Rows.Count).End(xlUp).Row + 1
'Substrings then fed into Excel (code excluded)
'Define what to do when done; depends on initial state of application and _
workbook
Select Case excelOpen + wbOpen
Case -2 'TT
oXLwb.Save
Case -1 'TF
oXLwb.Close (True)
Case 0 'FF
oXLwb.Close (True)
oXLApp.Quit
End Select
Set oXLws = Nothing
Set oXLApp = Nothing
Set oXLwb = Nothing
Set olMail = Nothing
Set olNS = Nothing
Exit Sub
NoExcel:
'If Excel is not open then create new instance
Set oXLApp = CreateObject("Excel.Application")
Err.Clear
On Error GoTo 0
Resume RunUpdates
End Sub
'-----------------------------------------------------------------
Private Function IsWorkBookOpen(FileName As String)
Dim ff As Long, ErrNo As Long
On Error GoTo ErrCapture:
ff = FreeFile()
Open FileName For Input Lock Read As #ff
Close ff
ErrCapture:
ErrNo = Err
On Error GoTo 0
Select Case ErrNo
Case 0: IsWorkBookOpen = False
Case 70: IsWorkBookOpen = True
Case Else: Error ErrNo
End Select
End Function
Last edited: