title: VBA macro called in Outlook not sorting in Excel
body: <p>So basically I have an excel object called xlobj, and in my code before this snippit if I am referring to the xlobj I would say </p>
  
  <pre><code>xlobj.Range(blahh blahh blahh).Value = "something"
  </code></pre>
  
  <p>Now my goal is to sort all my values that I took using the rest of my script from newest to oldest in Excel.  I used the record macro function in excel to get the macro code, then pasted it in my Outlook VBA and attached the xlobj.<em>_</em>  infront of all of the necessary codelines.</p>
  
  <p>It doesn't work, it does everything but the physical sorting (selection happens and the code is compiling)</p>
  
  <pre><code>xlobj.Range("A2:E900").Select
  xlobj.Sort.SortFields.Clear
  xlobj.Sort.SortFields.Add Key:=xlobj.Range("A1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
  With xlobj.Sort
    .SetRange xlobj.Range("A2:E900")
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  End With
  </code></pre>
  
  <p>Does anyone see the problem with the code?</p>
  
  <p><em>EDIT</em></p>
  
  <pre><code>Private Sub Extract(ByVal oFolder As Outlook.Folder)
  
  On Error Resume Next
  Set myOlApp = Outlook.Application
  Set mynamespace = myOlApp.GetNamespace("mapi")
  Set MyFolder = oFolder
  
  Set xlobj = CreateObject("excel.application.14")
  Set xlobjWbk = xlobj.Workbooks.Open("c:\Users\(username)\Desktop\(filename).xlsx")
  xlobj.Visible = True
  xlobj.Application.DisplayAlerts = False
  
  'Set Heading
  xlobj.Range("a" &amp; 1).Value = "Recieved Time"
  xlobj.Range("b" &amp; 1).Value = "Sender Email"
  xlobj.Range("c" &amp; 1).Value = "Subject"
  xlobj.Range("d" &amp; 1).Value = "Sender Name"
  xlobj.Range("e" &amp; 1).Value = "Body"
   xlobj.Columns("A").ColumnWidth = 17
   xlobj.Columns("B").ColumnWidth = 32
   xlobj.Columns("C").ColumnWidth = 36
   xlobj.Columns("D").ColumnWidth = 19
   xlobj.Columns("E").ColumnWidth = 56
  
  For i = MyFolder.Items.Count To MyFolder.Items.Count + 100
   xlobj.Range("a" &amp; i + 1).Value = ""
   xlobj.Range("b" &amp; i + 1).Value = ""
   xlobj.Range("C" &amp; i + 1).Value = ""
   xlobj.Range("d" &amp; i + 1).Value = ""
   xlobj.Range("e" &amp; i + 1).Value = ""
  Next
  
  For i = 1 To MyFolder.Items.Count
   Set myItem = MyFolder.Items(i)
   msgtext = myItem.body
  
   xlobj.Range("a" &amp; i + 1).Value = myItem.ReceivedTime
   xlobj.Range("b" &amp; i + 1).Value = myItem.SenderEmailAddress
   xlobj.Range("C" &amp; i + 1).Value = myItem.Subject
   xlobj.Range("d" &amp; i + 1).Value = myItem.SenderName
   xlobj.Range("e" &amp; i + 1).Value = msgtext
   xlobj.Columns("A:E").VerticalAlignment = xlVAlignTop
  Next
  
  xlobj.Range("A2:E900").Select
  xlobj.Sort.SortFields.Clear
  xlobj.Sort.SortFields.Add Key:=xlobj.Range("A1"), SortOn:=xlSortOnValues,     Order:=xlDescending, DataOption:=xlSortNormal
  With xlobj.Sort
      .SetRange xlobj.Range("A1:E900")
      .Header = xlNo
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
  End With
  </code></pre>
  
  <p>This script goes to the folder passed in through the parameter, and then dumps the inbox into an excel sheet</p>