+ Reply to Thread
Results 1 to 7 of 7

Code fails when ran within Userform, but works when ran from Macro button on worksheet.

  1. #1
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Code fails when ran within Userform, but works when ran from Macro button on worksheet.

    I am using the following code to create a PDF of some search results and then email them:

    Please Login or Register  to view this content.
    On the Search Page the user clicks Email Search and a UserForm is opened, allowing the user to enter the email settings (Addresses, Subject, Body etc). When the user selects the Send Email button the settings are saved on the EmailSettings sheet, and the code above is then executed. However Im getting the following error:
    Run time error 1004
    Method 'Close' of object '_Workbook' failed.

    If I run the email code by itself, and enter the data into the EmailSettings sheet manually it works fine, it only fails when ran from the UserForm.

    I have attached my workbook.
    Attached Files Attached Files

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Code fails when ran within Userform, but works when ran from Macro button on worksheet

    Maybe:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Code fails when ran within Userform, but works when ran from Macro button on worksheet

    Is this code to replace the following code:

    Please Login or Register  to view this content.
    I tried doing that but again it works fine when I run it in VB, but when I run it from the macro in the UserForm I get the same error as before:

    Run time error 1004
    Method 'Close' of object '_Workbook' failed.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Code fails when ran within Userform, but works when ran from Macro button on worksheet

    Hi Nitefox,

    Have you thought of having your user form call the routine from the VBA module, rather than having the code actually in the userform??

  5. #5
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Code fails when ran within Userform, but works when ran from Macro button on worksheet

    How would that work exactly, I'm not sure I'm following you?

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Code fails when ran within Userform, but works when ran from Macro button on worksheet

    Hi Nitefox,

    The Userform code would just be:
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Code fails when ran within Userform, but works when ran from Macro button on worksheet

    Makes sense, I dont know why I didn't think of that.
    However at first I was getting the same 1004 run time error, and it failed to close the temporary workbook and to send the email.
    So I added a line to close the UserForm before running the email code and it worked fine. How can I get it to work without having to close the Userform?
    I tried just having it re open the user form when the email code completes, but since it takes a few seconds for that code to run its a little impractical.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Userform code fails when the userform is called from a custom ribbon button
    By klonbeck in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-14-2013, 03:00 PM
  2. [SOLVED] Code fails in one file, works in copy?
    By JasonLeisemann in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-14-2013, 12:20 PM
  3. VBA code works in one SUB and fails in a different SUB
    By jcaskey in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-17-2013, 11:54 AM
  4. Code works fine in 2003 excel but fails in 2007
    By tanimytani in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-10-2012, 11:40 AM
  5. Macro run fails, step through works.
    By SteveAlston in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-15-2009, 11:37 PM

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