Results 1 to 4 of 4

Thread: Error code 1004 VBA when sent via email

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2014
    Posts
    1

    Error code 1004 VBA when sent via email

    Hi all, apologies if this is in the wrong thread.

    I'm getting a runtime error code 1004 "Activate method of worksheet class failed" when I send my worksheet via an email. The code only happens when you open the file as an attachment but not when i open the original file from my desktop.

    The error derives from

    Private Sub Workbook_Open()
    Worksheets("sheet1").Activate
    frmstudentregister.Show
    End Sub

    Any help is much appreciated

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: Error code 1004 VBA when sent via email

    Yeah, it's in the wrong forum. The VB.NET forum is for VB.NET questions. If your question is on VBA, as you say, then you should put it in the VBA forum. I've asked the mods to move this thread to the correct forum.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Error code 1004 VBA when sent via email

    Welcome to VBForums

    Thread moved from the 'VB.Net' forum to the 'Office Development/VBA' forum.

  4. #4
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Error code 1004 VBA when sent via email

    These types of glitches can be hard to track down and it may be that it is bug in Excel VBA. Therefore I'm going to throw a two different suggestions to try. No guaranties that either will work. Note that I'm using the "ThisWorkBook" qualifier; it shouldn't be needed, but who knows.

    1st: Make sure that the workbook is the active one.
    Code:
    Private Sub Workbook_Open()
       ThisWorkbook.Activate
       ThisWorkbook.Worksheets("Sheet1").Activate
    End Sub
    2nd: It may be a timing issue due to opening it from an attachment. Let's try running that code with a delay.
    Code:
    Private Sub Workbook_Open()
       ' schedule to run in 5 seconds
       Call Application.OnTime(DateAdd("s", 5, now()), "ThisWorkbook.ActiveSheet1")
    End Sub
    
    Private Sub ActiveSheet1()
       ThisWorkbook.Activate
       ThisWorkbook.Worksheets("Sheet1").Activate
    End Sub
    Good luck.

Posting Permissions

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



Click Here to Expand Forum to Full Width