Hi Everyone,
Just been upgraded from Win XP to Win 7, and I have another problem I can't solve. This macro exports emails from a folder in Outlook into an already opened Excel worksheet:
This macro errors at the 'Set appExcel = GetObject(, "Excel.Application")' line.
I've tried commenting it out in favour of the line below, but that errors too. The error dialog that appears has "-2147319779; Description" or "-91; Description", with the other line, and an OK button which stops the macro.
The only thing that is different from when it was working is the O/S - Win XP to Win 7. I've added the Excel12 Object Library in Tools-References, so I'm a bit stumped.
Any ideas welcomed..
Thanks
Just been upgraded from Win XP to Win 7, and I have another problem I can't solve. This macro exports emails from a folder in Outlook into an already opened Excel worksheet:
Code:
Sub ExportToExcel2()
On Error GoTo ErrHandler
Dim appExcel As Excel.Application
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Dim rng As Excel.Range
Dim strSheet As String
Dim strPath As String
Dim intRowCounter As Integer
Dim intColumnCounter As Integer
Dim Msg As Outlook.MailItem
Dim nms As Outlook.NameSpace
Dim fld As Outlook.MAPIFolder
Dim itm As Object
Debug.Print strSheet
'Select export folder
Set nms = Application.GetNamespace("MAPI")
Set fld = nms.PickFolder
'Handle potential errors with Select Folder dialog box.
If fld Is Nothing Then
MsgBox "There are no mail messages to export", vbOKOnly, _
"Error"
Exit Sub
ElseIf fld.DefaultItemType <> olMailItem Then
MsgBox "There are no mail messages to export", vbOKOnly, _
"Error"
Exit Sub
ElseIf fld.Items.Count = 0 Then
MsgBox "There are no mail messages to export", vbOKOnly, _
"Error"
Exit Sub
End If
'Activate existing open Excel workbook.
>>>>>>>>>>>>>>>>>>>>>>>> Set appExcel = GetObject(, "Excel.Application")
'appExcel.Workbooks.Open (strSheet) 'causes "doesn't exist" error
Set wkb = appExcel.ActiveWorkbook
Set wks = wkb.ActiveSheet
wks.Activate
appExcel.Application.Visible = True
'try changing:
'Range("A65536").End(xlUp).Offset(1, 0).Select
'to:
intRowCounter = wks.Range("A65536").End(xlUp).Row '+ 1
'Copy field items in mail folder.
For Each itm In fld.Items
intColumnCounter = 1
Set Msg = itm
intRowCounter = intRowCounter + 1
'From
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = Msg.SenderEmailAddress
intColumnCounter = intColumnCounter + 1
'To
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = Msg.To
intColumnCounter = intColumnCounter + 1
'CC
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = Msg.CC
intColumnCounter = intColumnCounter + 1
'Date sent
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = Msg.SentOn
intColumnCounter = intColumnCounter + 1
'Date received
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = Msg.ReceivedTime
intColumnCounter = intColumnCounter + 1
'Subject
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = Msg.Subject
intColumnCounter = intColumnCounter + 1
'Message
Set rng = wks.Cells(intRowCounter, intColumnCounter)
rng.Value = Msg.Body
'Set rng = wks.Cells(intRowCounter, intColumnCounter)
'rng.Value = msg.Body
'intColumnCounter = intColumnCounter + 1
Next itm
'run a macro in Excel:
appExcel.Run "TidyOutput_v2"
Set appExcel = Nothing
Set wkb = Nothing
Set wks = Nothing
Set rng = Nothing
Set Msg = Nothing
Set nms = Nothing
Set fld = Nothing
Set itm = Nothing
Exit Sub
ErrHandler:
If Err.Number = 1004 Then
MsgBox strSheet & " doesn't exist", vbOKOnly, _
"Error"
Else
MsgBox Err.Number & "; Description: ", vbOKOnly, _
"Error"
End If
Set appExcel = Nothing
Set wkb = Nothing
Set wks = Nothing
Set rng = Nothing
Set Msg = Nothing
Set nms = Nothing
Set fld = Nothing
Set itm = Nothing
End Sub
This macro errors at the 'Set appExcel = GetObject(, "Excel.Application")' line.
I've tried commenting it out in favour of the line below, but that errors too. The error dialog that appears has "-2147319779; Description" or "-91; Description", with the other line, and an OK button which stops the macro.
The only thing that is different from when it was working is the O/S - Win XP to Win 7. I've added the Excel12 Object Library in Tools-References, so I'm a bit stumped.
Any ideas welcomed..
Thanks