+ Reply to Thread
Results 1 to 15 of 15

How to remove code from ThisWorkbook before emailing as attachment

  1. #1
    Registered User
    Join Date
    03-27-2017
    Location
    Dubai
    MS-Off Ver
    Excel 2013
    Posts
    18

    How to remove code from ThisWorkbook before emailing as attachment

    Hi,

    I have two modules in my workbook, which I call automatically within ThisWorkbook, with a "Private Sub Workbook_Open" command:

    Please Login or Register  to view this content.
    These modules refresh my workbook and then email the refreshed workbook to a colleague.

    The issue is that when my colleague opens the workbook, the ThisWorkbook command tries to run, and causes an error as he does not have the API required.

    My question therefore is how can I add one more step to my VBA, so that the commands from ThisWorkbook are removed before the file is emailed?

    Hopefully this makes sense, would be great to get it working!

    Many thanks,
    Antony

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,331

    Re: How to remove code from ThisWorkbook before emailing as attachment

    Hi ahazeldene

    Could this work
    Please Login or Register  to view this content.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Registered User
    Join Date
    03-27-2017
    Location
    Dubai
    MS-Off Ver
    Excel 2013
    Posts
    18

    Re: How to remove code from ThisWorkbook before emailing as attachment

    Hi,

    Thank you for the suggestion, but the above solution involves manual intervention, meaning that I have to click "YES" for the workbook to update and send.

    The workbook is scheduled in Task Scheduler to open at 08:15 every morning. The idea is that the workbook, once opened by Task Scheduler, will automatically refresh and email as an attachment, with no manual intervention by me. This allows the whole process to run even if I am not at my desk.

    The current problem is that when my colleague receives the email and opens the attachment, the code in ThisWorkbook tries to run again, and he gets errors as he does not have API tool.

    Is there a way to delete/deactivate the code from ThisWorkbook before the email is sent? This way when my colleague receives the email and opens the attached workbook, the VBA will not try and run on his machine.

    Thanks,
    Antony

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,331

    Re: How to remove code from ThisWorkbook before emailing as attachment

    Why don't you save the workbook as a normal xls. and not a macro-enabled document before emailing. Or...does your colleague have to get the document to make use of the macros.

  5. #5
    Registered User
    Join Date
    03-27-2017
    Location
    Dubai
    MS-Off Ver
    Excel 2013
    Posts
    18

    Re: How to remove code from ThisWorkbook before emailing as attachment

    Hi,

    I do save the workbook as a .xls file. This causes the below warning when the user opens the workbook:

    2017-04-10 08_05_03-Excel.png

    He then gets another warning saying that the workbook had has macros disabled, does he want to enable?

    I would like this workbook at run completely autonomously on my side via Task Sceduler and then VBA in ThisWorkbook. This part is working. What I need is to somehow disable the code from ThisWorkbook before the workbook is sent as an attachment.

    Here is my code in full:

    ThisWorkbook:

    Please Login or Register  to view this content.
    Module RefreshData:

    Please Login or Register  to view this content.
    Module SendEmail:

    Please Login or Register  to view this content.
    Thanks!

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: How to remove code from ThisWorkbook before emailing as attachment

    Why not copy the sheet(s) to a new workbook and send that as attachment ?
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  7. #7
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,946

    Re: How to remove code from ThisWorkbook before emailing as attachment

    Quote Originally Posted by sintek View Post
    Why don't you save the workbook as a normal xls. and not a macro-enabled document before emailing. Or...does your colleague have to get the document to make use of the macros.
    That was my thoughts too, except to save As ,.xlsX that will make sure the code is stripped out
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  8. #8
    Registered User
    Join Date
    03-27-2017
    Location
    Dubai
    MS-Off Ver
    Excel 2013
    Posts
    18

    Re: How to remove code from ThisWorkbook before emailing as attachment

    Hi,

    Thanks for the suggestions. In regards to the saving the file as an .xlsx file, I have changed my SendEmail module, but get the below error:

    2017-04-10 10_33_25-Excel.png

    My VBA reads:

    Please Login or Register  to view this content.
    Any ideas why it is not working?

    Thanks!

  9. #9
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: How to remove code from ThisWorkbook before emailing as attachment

    Because you are using SaveCopyAs which saves a copy of the workbook, but the code remains and you are changing the extension to XLSX.

    When the file is opened, Excel detects the code but 'sees' an XLSX (no code) extension and assumes the worst. You need to change the FileFormat parameter of the SAVE command to save the workbook without code
    Please Login or Register  to view this content.
    OK, so I haven't looked too closely at the file name and whether that code is trying to save a file with the same name as an existing file - so you need to make sure. It would be a simple matter to save the file to the TEMP directory and attach to the email from there.
    Last edited by cytop; 04-10-2017 at 02:47 AM.

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: How to remove code from ThisWorkbook before emailing as attachment

    You cannot set or change FileFormat using SaveCopyAs.
    Like I said earlier export your Sheet or Sheets to a new workbook and use SaveAs. Then you can set fileformat as required.

    PS no need to use FileExtStr when you set fileformat. XL will add proper File extension according to FileFormat used.
    Last edited by bakerman2; 04-10-2017 at 02:50 AM.

  11. #11
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,946

    Re: How to remove code from ThisWorkbook before emailing as attachment

    If they are using an older version of excel 2003 or earlier, they wont be able to open .xlsx files

  12. #12
    Registered User
    Join Date
    03-27-2017
    Location
    Dubai
    MS-Off Ver
    Excel 2013
    Posts
    18

    Re: How to remove code from ThisWorkbook before emailing as attachment

    Hi,

    Thanks for the input. I substituted the code suggested above, and still receive the warning message:

    2017-04-10 08_05_03-Excel.png

    Also, now once the workbook has been emailed, the codes behind the refreshed data revert back to codes (rather than refreshed data), and the workbook is unfortunately unusable.

    My current code reads:

    Please Login or Register  to view this content.
    Perhaps I now need a new argument that copies the whole sheet and then Paste:=xlPasteValuesAndNumberFormats?

    If someone could help adjust the above code so that it can save the workbook in a agreeable format and email the data without unwinding the API refresh, that would be really appreciated.

    Thanks.

  13. #13
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: How to remove code from ThisWorkbook before emailing as attachment

    Try this.

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    03-27-2017
    Location
    Dubai
    MS-Off Ver
    Excel 2013
    Posts
    18

    Re: How to remove code from ThisWorkbook before emailing as attachment

    Hi,

    The above suggestion solves the issue of "corrupt or unsafe" file extension, but I still get the API refresh issue:

    Data after refresh:

    2017-04-12 08_14_27-Abdur Morning Update - Excel.png

    Data once emailed and opened:

    2017-04-12 08_14_45-Morning Update 12-Apr-17 [Read-Only] - Excel.png

    Is it possible to add an argument to the code above that copies the whole sheet and then pastes as Paste:=xlPasteValuesAndNumberFormats? I think that this would solve the second and only outstanding issue I have.

    Appreciate any suggestions,
    Thanks.

  15. #15
    Registered User
    Join Date
    03-27-2017
    Location
    Dubai
    MS-Off Ver
    Excel 2013
    Posts
    18

    Re: How to remove code from ThisWorkbook before emailing as attachment

    Hi,

    Does anyone have any ideas on this? I am still unable to send the file with the refreshed data, it shows as #Calc as above.

    If anyone has any ideas how to modify the below code I would appreciate the input. I would like the data, once refreshed, to be saved as values and formats (no underlying codes):

    Please Login or Register  to view this content.
    Thanks,

+ 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. Emailing PDF, another attachment, and with range in email body
    By supcussy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-24-2012, 10:03 PM
  2. Remove all code from workbook except "Thisworkbook"
    By realniceguy5000 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-13-2010, 07:34 PM
  3. Problems Emailing Attachment
    By lee2k60 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-11-2010, 04:22 AM
  4. Macro for emailing attachment & body
    By BigRed333 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-17-2009, 12:15 PM
  5. Emailing an attachment where the subject = data in cells
    By morris5984 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-23-2008, 12:40 PM
  6. Emailing as attachment
    By ameretsu in forum Excel General
    Replies: 1
    Last Post: 09-02-2008, 06:09 PM
  7. Remove ThisWorkbook code via VBA
    By PCLIVE in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-08-2005, 05:05 PM
  8. [SOLVED] why does a workwook increase in size when emailing as attachment
    By paula in forum Excel General
    Replies: 1
    Last Post: 07-20-2005, 01:05 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