+ Reply to Thread
Results 1 to 2 of 2

Repost:Excel not active / loses focus.Pls help!

  1. #1
    michael.beckinsale
    Guest

    Repost:Excel not active / loses focus.Pls help!

    Hi All,

    There are 2 code routines pasted below and both work fine individually.



    However if l call the 2nd routine from the 1st Excel seems to 'lose
    focus' ie the active workbook name is greyed out and flashing. If l
    activate Excel by placing and clicking the cursor anywhere in the Excel

    environment the code continues without a problem.


    I have tried combining the code but the same problem manifests itself.


    This is my 1st foray into extracting data from Outlook and l am
    wondering if it has something to do with security but that would not
    explain why the code continues immediately on return to the Excel
    environment. Alternatively i think l might need to 'grab' the Excel
    application and activate it.


    Please can somebody help me overcome this infuriating problem?


    Sub ListUnsubscribed()
    'Variables for the Outlook Object Library
    Dim myOlApp As Outlook.Application
    Dim mpfInbox As Outlook.MAPIFolder
    Dim obj As Outlook.MailItem
    'Other variables
    Dim i As Integer
    Dim r As Long
    Dim r1 As Long
    'Define the variables
    Set myOlApp =3D CreateObject("Outlook.Application")
    Set mpfInbox =3D
    myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("Unsub=
    =ADscribers")

    'Set calcualtion to manual for more speed
    Application.Calculation =3D xlManual
    'Find next empty row on list
    Sheets("Removed").Activate
    Range("A2").Activate
    r =3D ActiveCell.End(xlDown).Row + 1
    If r =3D 65536 Then
    MsgBox ("You have reached the limit of 65536 Unsubscribers")
    Exit Sub
    End If
    If r < 65536 Or r > 1 Then
    r =3D r
    Else
    r =3D 2
    End If
    'Set 1st row for copy to TemporaryList
    r1 =3D r
    'Loop all items in the Inbox\Unsubscribers Folder
    For i =3D 1 To mpfInbox.Items.Count
    If mpfInbox.Items(i).Class =3D olMail Then
    Set obj =3D mpfInbox.Items.Item(i)
    If obj.Subject =3D "unsubscribe" Or obj.Subject =3D "RE:
    unsubscribe" Then
    With Sheets("Removed")
    .Cells(r, 1).Value =3D obj.SenderEmailAddress
    .Cells(r, 2).Value =3D obj.Subject
    .Cells(r, 3).Value =3D obj.ReceivedTime
    .Cells(r, 4).Value =3D Now
    .Cells.Columns.AutoFit
    End With
    'Delete the email
    'obj.Delete
    r =3D r + 1
    End If
    End If
    Next
    'Copy to TemporaryList
    Sheets("Removed").Range("A" & r1 & ":D" & r).Copy
    Destination:=3DSheets("TemporaryList").Range("A2")
    End Sub


    Sub Delete_Unsubscribers()
    'Delete unsubscribers from 'Current' sheet
    Dim delName As String
    Application.ScreenUpdating =3D True
    Sheets("TemporaryList").Activate
    Range("A2").Activate
    Do Until ActiveCell.Value =3D ""
    Sheets("TemporaryList").Activate
    delName =3D ActiveCell.Value
    Sheets("Current").Activate
    Range("A1").Activate
    With Sheets("Current").Range("A:A")
    Set c =3D .Find(delName, lookin:=3DxlValues,
    SearchOrder:=3DxlByColumns, SearchDirection:=3DxlNext)
    If c Is Nothing Then
    MsgBox "Search Value was not found"
    Else
    c.EntireRow.Delete
    End If
    End With
    Sheets("TemporaryList").Activate
    ActiveCell.Offset(1, 0).Activate
    Loop
    MsgBox ("finished")
    Sheets("TemporaryList").Activate
    Range("A2:D2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.ClearContents
    'Reset to auto
    Application.Calculation =3D xlManual
    End Sub=20


    Regards=20


    Michael beckinsale


  2. #2
    Arif Ali
    Guest

    RE: Repost:Excel not active / loses focus.Pls help!

    Michael,

    You might want to try changing "Application" to XLApp by doing the following:

    Dim XLApp as Excel.Application
    Dim MyWorkbook as Excel.workbook
    Dim Removed as Excel.worksheet


    Sub ListUnsubscribed()
    'Variables for the Outlook Object Library
    Dim myOlApp As Outlook.Application
    Dim mpfInbox As Outlook.MAPIFolder
    Dim obj As Outlook.MailItem
    'Other variables
    Dim i As Integer
    Dim r As Long
    Dim r1 As Long
    'Define the variables
    Set myOlApp = CreateObject("Outlook.Application")

    Set XLApp = GetObject(,"Excel.Application")
    Set MyWorkbook = XLapp.Workbooks("MyWorkBookName")
    Set Removed = MyWorkBook.Sheets("Removed")

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1