0

所以基本上我有一个名为 xlobj 的 excel 对象,如果我指的是 xlobj,我会在这段代码之前的代码中说

xlobj.Range(blahh blahh blahh).Value = "something"

现在我的目标是在 Excel 中将我使用脚本的其余部分从最新到最旧的所有值排序。我使用excel中的记录宏功能来获取宏代码,然后将其粘贴到我的Outlook VBA中并附加xlobj。_ 在所有必要的代码行之前。

它不起作用,除了物理排序之外,它什么都做(选择发生并且代码正在编译)

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

有人看到代码的问题吗?

编辑

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" & 1).Value = "Recieved Time"
xlobj.Range("b" & 1).Value = "Sender Email"
xlobj.Range("c" & 1).Value = "Subject"
xlobj.Range("d" & 1).Value = "Sender Name"
xlobj.Range("e" & 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" & i + 1).Value = ""
 xlobj.Range("b" & i + 1).Value = ""
 xlobj.Range("C" & i + 1).Value = ""
 xlobj.Range("d" & i + 1).Value = ""
 xlobj.Range("e" & i + 1).Value = ""
Next

For i = 1 To MyFolder.Items.Count
 Set myItem = MyFolder.Items(i)
 msgtext = myItem.body

 xlobj.Range("a" & i + 1).Value = myItem.ReceivedTime
 xlobj.Range("b" & i + 1).Value = myItem.SenderEmailAddress
 xlobj.Range("C" & i + 1).Value = myItem.Subject
 xlobj.Range("d" & i + 1).Value = myItem.SenderName
 xlobj.Range("e" & 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

这个脚本到通过参数传入的文件夹,然后将收件箱转储到excel表格中

4

2 回答 2

0

更改为 xlobj.Sort.SortFields.Add Key:=xlobj.Range("A2"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal

于 2013-05-30T15:57:14.657 回答
0
With xlobj.Sort
    .SetRange xlobj.Range("A1:E900") '<---
    .Header = xlYes                  '<---
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
于 2012-08-23T18:48:41.470 回答