+ Reply to Thread
Results 1 to 10 of 10

Code to create and email PDF getting interupted

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

    Code to create and email PDF getting interupted

    I am using the following code I found online to create and email a PDF of one of my sheets:

    Please Login or Register  to view this content.
    The code runs from a command button in a userform, and it works fine. However after clicking the command button if the user clicks anywhere in the workbook before the code completes then it stops the code from completing. The user is then left with a duplicate copy of the workbook open titled 'Workbook', and the main workbook is closed (which easily causes confusion).
    Is there away I can prevent this from happening by allowing the user to click on the workbook without it interfering, or if this is not possible then somehow temporarily disable the mouse pointer and keyboard so the user is not able to accidentally interrupt it?
    Last edited by Nitefox; 01-12-2014 at 09:57 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,751

    Re: Code to create and email PDF getting interupted

    Is UserForm1 the form containing this button?

    What is the "ShowModal" attribute of the form containing the button?
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

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

    Re: Code to create and email PDF getting interupted

    That is correct the button is located on UserForm1.
    Im not sure what ShowModal is. How do I check that?

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,751

    Re: Code to create and email PDF getting interupted

    In the code development window, click on the line in the object browser for the form, then click F4. A Properties window will come up. However, the default is True, so if you are not aware of this then it is certainly still True. Which is what you want. If it were False, then the user could still do things in other windows while the form is showing.

    You hide the form immediately upon pressing the button. I would suggest waiting until the end of the Sub to hide the form. That will prevent the user from action in any other >Excel< window.
    Last edited by 6StringJazzer; 01-12-2014 at 10:29 PM. Reason: blue text

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Code to create and email PDF getting interupted

    I believe in "DoEvents" in code that should wait for other windows things to happen. Without the code waiting (DoEvents) it will keep firing instructions that are lost on the other program.

    http://msdn.microsoft.com/en-us/libr.../gg264522.aspx
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

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

    Re: Code to create and email PDF getting interupted

    6StringJazzer, when I move the userform hide line to the end I get run time error '1004', Method 'Close' of object '_Workbook' failed, on the following line:
    ActiveWorkbook.Close SaveChanges:=False

    MarvinP, am I correct in understanding that 'DoEvents' allows the computer to run the task hidden in the background, allowing the user to still perform actions on the workbook without interfering with what its doing, or something of that nature?
    If so how would I go about coding that?

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,751

    Re: Code to create and email PDF getting interupted

    I think that the OP's issue is the reverse. Nitefox wants to prevent the user from doing anything in a worksheet while the code is running, which then stops the code from running. DoEvents allows the OS to take control to execute any pending interrupts. DoEvents is frequently used in long-running loops to allow things like the display to be updated or respond to keyboard input.

    I may be misunderstanding how you are suggesting to use DoEvents but I am not seeing how it will work in this case.

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

    Re: Code to create and email PDF getting interupted

    That is correct 6String, unless there is an easy way to allow the code to still work simultaneously to the user working on the workbook, then I feel the best way to get around this is to prevent the user from doing anything until the code has finished.

  9. #9
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Code to create and email PDF getting interupted

    A few tests is how I'd go. Throw in some DoEvents in the code and see what happens.

    My experience has been, I've needed them after opening a file that is large and takes a while to load. Just after the Open command I'd put in DoEvents which (I believe) would allow the Open command to complete before trying to toss in more VBA commands. As soon as another app (like a PDF maker) is working I need to tell VBA to wait until it is done before trying to do more commands.

    I haven't spent much time looking at he code above and admire 6SJ and his experience. I'd simply throw in some DoEvents after Opens and see what happens.

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

    Re: Code to create and email PDF getting interupted

    I think I have a work around, I simply put the hide userform line right before the line that was causing the error before, and it seems to be working just fine now. The code after that line must work quick enough that the user doesn't have the chance to interrupt it (like they were if the hide userform line was first).
    Thank you both for your help.

+ 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] Help me fix attached code (create Outlook email then create calendar item)
    By Rerock in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-19-2013, 08:15 PM
  2. Code to create email in Lotus Notes
    By lastat in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-11-2011, 09:45 AM
  3. Change this code to create one email with all the addresses found
    By dcgrove in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-15-2008, 08:52 PM
  4. Macro 'Code execution has been interupted' error after print?
    By Bluehair the Pirate in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-09-2006, 02:25 PM
  5. [SOLVED] Code Execution has been interupted
    By Rich in forum Excel General
    Replies: 2
    Last Post: 05-10-2005, 08:06 AM

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