Export from Outlook to Excel - Follow on from Q25831986

This is a follow on from Q25831986 which appeared to be working but there is a problem somewhere.

The idea was to export data from outlook to excel, the code appeared to work, but for some reason no data is being added to excel

The code is as follows, the first bit is in [ThisOutlookSession], the second bit is in [Module1].

It gives the impression its working, the option appears in the context menu, when the option to Update Excel is selected it seems to work and the message box reports "done" but nothing ias added to the Excel sheet.


In [ThisOutlookSession]

'### Context
Private WithEvents ActiveExplorerCBars As CommandBars
Private WithEvents ContextButton As CommandBarButton 'A flag, so we don't respond to our own changes in OnUpdate Private IgnoreCommandbarsChanges As Boolean
'### Context

Private Sub Application_Startup()
'### Context
    Set ActiveExplorerCBars = ActiveExplorer.CommandBars
'### Context
End Sub


'### Context
Private Sub ContextButton_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
    GetTheData
End Sub

'This fires when the user right-clicks a contact, and also for a lot of other things!
Private Sub ActiveExplorerCBars_OnUpdate()
    Dim bar As CommandBar

    If IgnoreCommandbarsChanges Then Exit Sub

    'Try for the context menu
    On Error Resume Next
    Set bar = ActiveExplorerCBars.Item("Context Menu")
    On Error GoTo 0

    If Not bar Is Nothing Then
        AddContextButton bar
    End If
End Sub

Private Sub AddContextButton(ContextMenu As CommandBar)
    Dim b As CommandBarButton
    Dim Control As CommandBarControl

    'User cannot play with the Context Menu, so we know there is at most
    'only one copy of the control there
    Set Control = ContextMenu.FindControl(Type:=MsoControlType.msoControlButton, Tag:="UpdateExcel")

    If Control Is Nothing Then

        'Unprotect context menu
        ChangingBar ContextMenu, Restore:=False

        'Create the control
        Set Control = ContextMenu.Controls.Add(Type:=msoControlButton)

        'Set up control
        Control.Tag = "UpdateExcel"
        Control.Caption = "Update Excel"
        Control.Priority = 1
        Control.Visible = True

        'Reprotect context menu
        ChangingBar ContextMenu, Restore:=True

        'Hook the Click event
        Set ContextButton = Control

    Else
        'Note that Outlook has a bad habbit of changing our Context Menu buttons
        'to be priority dropped.
        Control.Priority = 1
    End If

End Sub

'Called once to prepare for changes to the command bar, then again with 'Restore = true once changes are complete.
Private Sub ChangingBar(bar As CommandBar, Restore As Boolean)

  Static oldProtectFromCustomize, oldIgnore As Boolean

  If Restore Then

    'Restore the Ignore Changes flag
    IgnoreCommandbarsChanges = oldIgnore

    'Restore the protect-against-customization bit
    If oldProtectFromCustomize Then bar.Protection = _
      bar.Protection And msoBarNoCustomize

  Else

    'Store the old Ignore Changes flag
    oldIgnore = IgnoreCommandbarsChanges
    IgnoreCommandbarsChanges = True

    'Store old protect-against-customization bit setting then clear
    'CAUTION: Be careful not to alter the property if there is no need,
    'as changing the Protection will cause any visible CommandBarPopup
    'to disappear unless it is the popup we are altering.
    oldProtectFromCustomize = bar.Protection And msoBarNoCustomize
    If oldProtectFromCustomize Then bar.Protection = bar.Protection _
      And Not msoBarNoCustomize

  End If

End Sub
'### Context





In [Module1]
Sub GetTheData()
      
    Dim TheMessage As String
    Dim arr(1 To 12, 1 To 2) As Variant
    Dim Counter As Long
    Dim TheLine As String
    Dim xlApp As Object
    Dim xlWb As Object
    Dim xlWs As Object
    Dim NextR As Long
    Dim ColNum As Long
    Dim ValueStr As String
    Dim Interests As Variant
    Dim XLWBook As String
    
    XLWBook = "C:\XLData\Responses.xls"  'change as needed
      
    ' On second dimension, 1=headings from message, 2=headings in Excel file
      
    arr(1, 1) = "First Name": arr(1, 2) = "First Name"
    arr(2, 1) = "Surname": arr(2, 2) = "Surname"
    arr(3, 1) = "Email": arr(3, 2) = "Email"
    arr(4, 1) = "Address": arr(4, 2) = "Address1"
    arr(5, 1) = "Address 2": arr(5, 2) = "Address2"
    arr(6, 1) = "Town": arr(6, 2) = "Town"
    arr(7, 1) = "County": arr(7, 2) = "County"
    arr(8, 1) = "Postcode": arr(8, 2) = "Postcode"
    arr(9, 1) = "Telephone": arr(9, 2) = "Telephone"
    arr(10, 1) = "Age range": arr(10, 2) = "Age Range"
    arr(11, 1) = "Interests": arr(11, 2) = "Interests"
      
    TheMessage = Application.ActiveExplorer.Selection(1)
      
    Set xlApp = CreateObject("Excel.Application")
    Set xlWb = xlApp.Workbooks.Open(XLWBook)
    Set xlWs = xlWb.Worksheets(1)
    
    With xlWs
        NextR = .Cells(.Rows.Count, "a").End(-4162).Row + 1 '-4162 = xlUp
        For Counter = 1 To 11
            TheLine = RegExpFind(TheMessage, arr(Counter, 1) & ":[^\n]*\n", 1, False)
            If TheLine <> "" Then
                ValueStr = Split(TheLine, ":")(1)
                ColNum = xlApp.Match(arr(Counter, 2), .[1:1], 0)
                If Counter < 11 Then
                    .Cells(NextR, ColNum) = Trim(ValueStr)
                Else
                    Interests = Split(Replace(ValueStr, ", ", ","), ",")
                    .Cells(NextR, ColNum).resize(1, UBound(Interests) + 1).Value = Interests
                End If
                .Cells(NextR, "a") = Date
            End If
        Next
    End With
      
    xlWb.Save
    xlWb.Close
    Set xlWs = Nothing
    Set xlWb = Nothing
    xlApp.Quit
    Set xlApp = Nothing
      
    MsgBox "Done"
      
End Sub
  
Function RegExpFind(LookIn As String, PatternStr As String, Optional Pos, _
    Optional MatchCase As Boolean = True, Optional ReturnType As Long = 0, _
    Optional MultiLine As Boolean = False)
      
    ' Function written by Patrick G. Matthews.  You may use and distribute this code freely,
    ' as long as you properly credit and attribute authorship and the URL of where you
    ' found the code
      
    ' For more info, please see:
    ' http://www.experts-exchange.com/articles/Programming/Languages/Visual_Basic/Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html
      
    ' This function relies on the VBScript version of Regular Expressions, and thus some of
    ' the functionality available in Perl and/or .Net may not be available.  The full extent
    ' of what functionality will be available on any given computer is based on which version
    ' of the VBScript runtime is installed on that computer
      
    ' This function uses Regular Expressions to parse a string (LookIn), and return matches to a
    ' pattern (PatternStr).  Use Pos to indicate which match you want:
    ' Pos omitted               : function returns a zero-based array of all matches
    ' Pos = 1                   : the first match
    ' Pos = 2                   : the second match
    ' Pos = <positive integer>  : the Nth match
    ' Pos = 0                   : the last match
    ' Pos = -1                  : the last match
    ' Pos = -2                  : the 2nd to last match
    ' Pos = <negative integer>  : the Nth to last match
    ' If Pos is non-numeric, or if the absolute value of Pos is greater than the number of
    ' matches, the function returns an empty string.  If no match is found, the function returns
    ' an empty string.  (Earlier versions of this code used zero for the last match; this is
    ' retained for backward compatibility)
      
    ' If MatchCase is omitted or True (default for RegExp) then the Pattern must match case (and
    ' thus you may have to use [a-zA-Z] instead of just [a-z] or [A-Z]).
      
    ' ReturnType indicates what information you want to return:
    ' ReturnType = 0            : the matched values
    ' ReturnType = 1            : the starting character positions for the matched values
    ' ReturnType = 2            : the lengths of the matched values
      
    ' If you use this function in Excel, you can use range references for any of the arguments.
    ' If you use this in Excel and return the full array, make sure to set up the formula as an
    ' array formula.  If you need the array formula to go down a column, use TRANSPOSE()
      
    ' Note: RegExp counts the character positions for the Match.FirstIndex property as starting
    ' at zero.  Since VB6 and VBA has strings starting at position 1, I have added one to make
    ' the character positions conform to VBA/VB6 expectations
      
    ' Normally as an object variable I would set the RegX variable to Nothing; however, in cases
    ' where a large number of calls to this function are made, making RegX a static variable that
    ' preserves its state in between calls significantly improves performance
      
    Static RegX As Object
    Dim TheMatches As Object
    Dim Answer()
    Dim Counter As Long
      
    ' Evaluate Pos.  If it is there, it must be numeric and converted to Long
      
    If Not IsMissing(Pos) Then
        If Not IsNumeric(Pos) Then
            RegExpFind = ""
            Exit Function
        Else
            Pos = CLng(Pos)
        End If
    End If
      
    ' Evaluate ReturnType
      
    If ReturnType < 0 Or ReturnType > 2 Then
        RegExpFind = ""
        Exit Function
    End If
      
    ' Create instance of RegExp object if needed, and set properties
      
    If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = True
        .IgnoreCase = Not MatchCase
        .MultiLine = MultiLine
    End With
          
    ' Test to see if there are any matches
      
    If RegX.Test(LookIn) Then
          
        ' Run RegExp to get the matches, which are returned as a zero-based collection
          
        Set TheMatches = RegX.Execute(LookIn)
          
        ' Test to see if Pos is negative, which indicates the user wants the Nth to last
        ' match.  If it is, then based on the number of matches convert Pos to a positive
        ' number, or zero for the last match
          
        If Not IsMissing(Pos) Then
            If Pos < 0 Then
                If Pos = -1 Then
                    Pos = 0
                Else
                      
                    ' If Abs(Pos) > number of matches, then the Nth to last match does not
                    ' exist.  Return a zero-length string
                      
                    If Abs(Pos) <= TheMatches.Count Then
                        Pos = TheMatches.Count + Pos + 1
                    Else
                        RegExpFind = ""
                        GoTo Cleanup
                    End If
                End If
            End If
        End If
          
        ' If Pos is missing, user wants array of all matches.  Build it and assign it as the
        ' function's return value
          
        If IsMissing(Pos) Then
            ReDim Answer(0 To TheMatches.Count - 1)
            For Counter = 0 To UBound(Answer)
                Select Case ReturnType
                    Case 0: Answer(Counter) = TheMatches(Counter)
                    Case 1: Answer(Counter) = TheMatches(Counter).FirstIndex + 1
                    Case 2: Answer(Counter) = TheMatches(Counter).Length
                End Select
            Next
            RegExpFind = Answer
          
        ' User wanted the Nth match (or last match, if Pos = 0).  Get the Nth value, if possible
          
        Else
            Select Case Pos
                Case 0                          ' Last match
                    Select Case ReturnType
                        Case 0: RegExpFind = TheMatches(TheMatches.Count - 1)
                        Case 1: RegExpFind = TheMatches(TheMatches.Count - 1).FirstIndex + 1
                        Case 2: RegExpFind = TheMatches(TheMatches.Count - 1).Length
                    End Select
                Case 1 To TheMatches.Count      ' Nth match
                    Select Case ReturnType
                        Case 0: RegExpFind = TheMatches(Pos - 1)
                        Case 1: RegExpFind = TheMatches(Pos - 1).FirstIndex + 1
                        Case 2: RegExpFind = TheMatches(Pos - 1).Length
                    End Select
                Case Else                       ' Invalid item number
                    RegExpFind = ""
            End Select
        End If
      
    ' If there are no matches, return empty string
      
    Else
        RegExpFind = ""
    End If
      
Cleanup:
    ' Release object variables
      
    Set TheMatches = Nothing
      
End Function

Open in new window

LVL 70
Brian PiercePhotographerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris BottomleySoftware Quality Lead EngineerCommented:
The following is effectively unchanged but with some output data in the immediate window, (ctrl + G in the VBE)

What does the output data suggest about the overall flow?

Chris
Sub GetTheData()
      
    Dim TheMessage As String
    Dim arr(1 To 12, 1 To 2) As Variant
    Dim Counter As Long
    Dim TheLine As String
    Dim xlApp As Object
    Dim xlWb As Object
    Dim xlWs As Object
    Dim NextR As Long
    Dim ColNum As Long
    Dim ValueStr As String
    Dim Interests As Variant
    Dim XLWBook As String
    
    XLWBook = "C:\XLData\Responses.xls"  'change as needed
      
    ' On second dimension, 1=headings from message, 2=headings in Excel file
      
    arr(1, 1) = "First Name": arr(1, 2) = "First Name"
    arr(2, 1) = "Surname": arr(2, 2) = "Surname"
    arr(3, 1) = "Email": arr(3, 2) = "Email"
    arr(4, 1) = "Address": arr(4, 2) = "Address1"
    arr(5, 1) = "Address 2": arr(5, 2) = "Address2"
    arr(6, 1) = "Town": arr(6, 2) = "Town"
    arr(7, 1) = "County": arr(7, 2) = "County"
    arr(8, 1) = "Postcode": arr(8, 2) = "Postcode"
    arr(9, 1) = "Telephone": arr(9, 2) = "Telephone"
    arr(10, 1) = "Age range": arr(10, 2) = "Age Range"
    arr(11, 1) = "Interests": arr(11, 2) = "Interests"
      
    TheMessage = Application.ActiveExplorer.Selection(1)
      
    Set xlApp = CreateObject("Excel.Application")
    Set xlWb = xlApp.Workbooks.Open(XLWBook)
    Set xlWs = xlWb.Worksheets(1)
    
    With xlWs
        NextR = .Cells(.Rows.Count, "a").End(-4162).Row + 1 '-4162 = xlUp
        For Counter = 1 To 11
            TheLine = RegExpFind(TheMessage, arr(Counter, 1) & ":[^\n]*\n", 1, False)
debug.print theline
            If TheLine <> "" Then
                ValueStr = Split(TheLine, ":")(1)
                ColNum = xlApp.Match(arr(Counter, 2), .[1:1], 0)
                If Counter < 11 Then
debug.print "counter < 11"
                    .Cells(NextR, ColNum) = Trim(ValueStr)
                Else
debug.print "counter >+ 11"
                    Interests = Split(Replace(ValueStr, ", ", ","), ",")
                    .Cells(NextR, ColNum).resize(1, UBound(Interests) + 1).Value = Interests
                End If
                .Cells(NextR, "a") = Date
            End If
        Next
    End With
      
    xlWb.Save
    xlWb.Close
    Set xlWs = Nothing
    Set xlWb = Nothing
    xlApp.Quit
    Set xlApp = Nothing
      
    MsgBox "Done"
      
End Sub
  

Open in new window

0
Brian PiercePhotographerAuthor Commented:
I took the liberty of changing the
Debug.print TheLine#
to
Debug.print "TheLine " & TheLine
as I wasn't seeeing anything, it would appear that it is empty as it returns the following in the immediate window

Started...
The Line
The Line
The Line
The Line
The Line
The Line
The Line
The Line
The Line
The Line
The Line



If I change the line

TheMessage = Application.ActiveExplorer.Selection(1)
back to
TheMessage = ActiveInspector.CurrentItem.Body

and open the message before running the macro, then it works and the output is as shown below so it seems there is a problem with this line.

The Line First Name: Ann

counter < 11
The Line Surname: Other

counter < 11
The Line Email: atest3@yahoo.co.uk

counter < 11
The Line Address: 234 High Street

counter < 11
The Line Address 2: East End

counter < 11
The Line Town: Truro

counter < 11
The Line County: Cornwall

counter < 11
The Line Postcode: TR1 3WE

counter < 11
The Line Telephone: 06666 666666

counter < 11
The Line Age range: 34-90

counter < 11
The Line Interests: Golf Breaks, Gardens

counter >+ 11
Started...
The Line First Name: Sharon

counter < 11
The Line Surname: Walls

counter < 11
The Line Email: atest1@yahoo.co.uk

counter < 11
The Line Address: a house

counter < 11
The Line Address 2: a street

counter < 11
The Line Town: a town

counter < 11
The Line County: a county

counter < 11
The Line Postcode: tr7 3lt

counter < 11
The Line Telephone: 07777 777777

counter < 11
The Line Age range: 25-34

counter < 11
The Line Interests: Xmas Shopping Breaks, Tour of Cornwall

counter >+ 11


As a side issue, when the macro works, the data in the Excel cells all have a square blob at the end of the text and soemof the cells seem to have some invisible characters (or formatting?) in front of the data - see what I mean on the attached sample
Responses.xls
0
David LeeCommented:
This looks like my fault.  Change

TheMessage = Application.ActiveExplorer.Selection(1)

to

TheMessage = Application.ActiveExplorer.Selection(1).Body
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Chris BottomleySoftware Quality Lead EngineerCommented:
Try the following which has Davids correction and a replacement for the vbcrlf string.  Might not be all the formatting chars but worth a try.

Chris
Sub GetTheData()
      
    Dim TheMessage As String
    Dim arr(1 To 12, 1 To 2) As Variant
    Dim Counter As Long
    Dim TheLine As String
    Dim xlApp As Object
    Dim xlWb As Object
    Dim xlWs As Object
    Dim NextR As Long
    Dim ColNum As Long
    Dim ValueStr As String
    Dim Interests As Variant
    Dim XLWBook As String
    
    XLWBook = "C:\XLData\Responses.xls"  'change as needed
      
    ' On second dimension, 1=headings from message, 2=headings in Excel file
      
    arr(1, 1) = "First Name": arr(1, 2) = "First Name"
    arr(2, 1) = "Surname": arr(2, 2) = "Surname"
    arr(3, 1) = "Email": arr(3, 2) = "Email"
    arr(4, 1) = "Address": arr(4, 2) = "Address1"
    arr(5, 1) = "Address 2": arr(5, 2) = "Address2"
    arr(6, 1) = "Town": arr(6, 2) = "Town"
    arr(7, 1) = "County": arr(7, 2) = "County"
    arr(8, 1) = "Postcode": arr(8, 2) = "Postcode"
    arr(9, 1) = "Telephone": arr(9, 2) = "Telephone"
    arr(10, 1) = "Age range": arr(10, 2) = "Age Range"
    arr(11, 1) = "Interests": arr(11, 2) = "Interests"
      
    TheMessage = Application.ActiveExplorer.Selection(1).body
      
    Set xlApp = CreateObject("Excel.Application")
    Set xlWb = xlApp.Workbooks.Open(XLWBook)
    Set xlWs = xlWb.Worksheets(1)
    
    With xlWs
        NextR = .Cells(.Rows.Count, "a").End(-4162).Row + 1 '-4162 = xlUp
        For Counter = 1 To 11
            TheLine = RegExpFind(TheMessage, arr(Counter, 1) & ":[^\n]*\n", 1, False)
            If TheLine <> "" Then
                ValueStr = replace(Split(TheLine, ":")(1), vbcrlf, "")
                ColNum = xlApp.Match(arr(Counter, 2), .[1:1], 0)
                If Counter < 11 Then
                    .Cells(NextR, ColNum) = Trim(ValueStr)
                Else
                    Interests = Split(Replace(ValueStr, ", ", ","), ",")
                    .Cells(NextR, ColNum).resize(1, UBound(Interests) + 1).Value = Interests
                End If
                .Cells(NextR, "a") = Date
            End If
        Next
    End With
      
    xlWb.Save
    xlWb.Close
    Set xlWs = Nothing
    Set xlWb = Nothing
    xlApp.Quit
    Set xlApp = Nothing
      
    MsgBox "Done"
      
End Sub
  

Open in new window

0
Brian PiercePhotographerAuthor Commented:
That did the trick - the only issue remaining then is how to strip the strange characters/formatting from the excel data. As ou can see from this image the data has a square blob at the end and some data (such as the interests) seems to have some invisible characters in front of the text


Untitled.jpg
0
Brian PiercePhotographerAuthor Commented:
I'll aslo uplad the xls file incase it helps
Responses.xls
0
Brian PiercePhotographerAuthor Commented:
That did the trick - many thanks once more
0
Brian PiercePhotographerAuthor Commented:
Excellent - just what I needed
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.