+ Reply to Thread
Results 1 to 9 of 9

How do I force saving as *.xlsm without using .SaveAs

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    How do I force saving as *.xlsm without using .SaveAs

    I've come up against a problem I can't think my way around so was hoping you guys could help.

    We use Excel 2007 at work and I created a macro that validates data. Once the data has been validated with no errors, the macro makes available a button which when clicked calls a macro to copy the validated data to a new workbook.

    My organisation uses a document management system called TRIM. TRIM has a COM add-in for MS Office that takes care of the saving so when the save button is clicked or the save/save-as menu item is selected, the add-in launches TRIM's save dialog and not the usual windows/office dialog. This save dialog does not give you the option to save the file as a particular type so will just automatically save the file into TRIM as whatever type the document was created as.

    After help I've previously received from Norie, the way the "copy data" macro works is as follows: A new workbook is created; a hidden sheet with code already in the code module for that sheet is copied over to the new workbook; the data that has just been successfully validated is then copied and pasted into that new sheet in the new workbook. The result is a new workbook containing a sheet of validated data and a macro in the code module for that sheet. Which has been working well for me since I created it last year.

    The time is coming now to roll this out to my colleagues to use but it has occurred to me that because I write macros all day long, I have changed the Save options in Excel Options to automatically create new documents as macro enabled.

    Save Options.png

    This means that the TRIM add-in automatically saves the new document as an Excel Macro-Enabled Workbook (*.xlsm) document. My colleagues however will have theirs setup as the standard Excel Workbook (*.xlsx) which means when they try to save, the TRIM add-in will automatically save it as those file types which will throw up warnings and prevent the save because that extension cannot include macros (or more accurately, it just saves it without the warnings and the result is the code is stripped out)

    I had considered working out some kind of Workbook_BeforeSave code to capture the user's save event but the problem with that approach is the same as the problem that gets mentioned in the post I linked to above in that I'll need to insert code into the new workbook. So I considered forcing a save in the same macro that copies the data as soon as the data is copied with something like this:
    TheFileName = "Example.xlsm"
    wBook.SaveAs TheFileName, xlOpenXMLWorkbookMacroEnabled
    But it turns out that the TRIM add-in only intercepts UI initiated save commands and so a VBA initiated save command actually launches the usual Windows/Office save dialog which is no use as you can't save to TRIM from there.

    Can anybody think of a way around this? Like can I use VBA to tell Excel to save a file but make Excel "think" the command came from a UI selection? Or is there some way for me to view the code of a COM add-in so I can find out what procedure I'd need to call within that add-in?

    My posts always seem to be very long so apologies, but I'm a great believer in explaining a situation properly. That said, if there's more I can add, please let me know.

    Thanks for any help you can give me.
    Please click *Add Reputation if I've helped

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: How do I force saving as *.xlsm without using .SaveAs

    This will call up the SaveAs dialog. I'm not sure if that's what you're looking for.

    TheFileName = "Example.xlsm"
    Application.Dialogs(xlDialogSaveAs).Show TheFileName, xlOpenXMLWorkbookMacroEnabled
    Last edited by AlphaFrog; 10-14-2015 at 09:58 AM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: How do I force saving as *.xlsm without using .SaveAs

    Quote Originally Posted by AlphaFrog View Post
    This will call up the SaveAs dialog. I'm not sure if that's what you're looking for.

    TheFileName = "Example.xlsm"
    Application.Dialogs(xlDialogSaveAs).Show TheFileName, xlOpenXMLWorkbookMacroEnabled
    Thanks AlphaFrog. I tested that and it just bring up the Windows/Office SaveAs dialog too.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: How do I force saving as *.xlsm without using .SaveAs

    Then I'm not quite sure what you're asking for.

    This will change Excel's default SaveAs file format to macro enabled.

    Application.DefaultSaveFormat = xlOpenXMLWorkbookMacroEnabled

  5. #5
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: How do I force saving as *.xlsm without using .SaveAs

    Quote Originally Posted by AlphaFrog View Post
    Then I'm not quite sure what you're asking for.

    This will change Excel's default SaveAs file format to macro enabled.

    Application.DefaultSaveFormat = xlOpenXMLWorkbookMacroEnabled
    Now that is perfect AlphaFrog, that'll suit my needs brilliantly!

    I was literally just about to post a separate thread there asking how to determine what a user's default file format is set to so I could popup some convoluted instructions to the user to change it if it wasn't set up as xlsm already. I didn't think I'd just be able to go ahead and change it with VBA myself. You read my mind entirely, and you said you weren't quite sure what I was asking for! lol

    It doesn't matter either way for my purposes, but just so I know, does that create a permanent change or a change that's only valid for that session?

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: How do I force saving as *.xlsm without using .SaveAs

    I think that's a permanent (beyond the current session) change. You could store their current setting, change to macroenabled, then restore the original when closing the workbook.

    'Standard code module declare a public variable
    Public OrigSaveFormat As Long

    'Before they save
    OrigSaveFormat = Application.DefaultSaveFormat
    Application.DefaultSaveFormat = xlOpenXMLWorkbookMacroEnabled


    'Before workbook close
    Application.DefaultSaveFormat = OrigSaveFormat

  7. #7
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: How do I force saving as *.xlsm without using .SaveAs

    Quote Originally Posted by AlphaFrog View Post
    I think that's a permanent (beyond the current session) change. You could store their current setting, change to macroenabled, then restore the original when closing the workbook.

    'Standard code module declare a public variable
    Public OrigSaveFormat As Long

    'Before they save
    OrigSaveFormat = Application.DefaultSaveFormat
    Application.DefaultSaveFormat = xlOpenXMLWorkbookMacroEnabled


    'Before workbook close
    Application.DefaultSaveFormat = OrigSaveFormat
    Having just tested it with our set up here, it seems that after the setting has been changed the new workbook still "wants to be" a .xlsx file. However, if I shut down Excel and relaunch it, then run the macro again without instructing another change to the default save as file type, then the new workbook wants to be a .xlsm file.

    It seems as though the setting is only read by Excel at Excel startup time. Would that be right? I suppose this behaviour could also be something to do with that TRIM add-in.

    Like I say though, it doesn't matter for my purposes as a permanent change to this setting is fine so thanks to you I can now test for this and display a short message asking the user to shutdown and relaunch Excel if the setting isn't correct when the workbook is opened which should naturally only appear once (or if the user changes the setting manually from .xlsm at any point)

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: How do I force saving as *.xlsm without using .SaveAs

    Quote Originally Posted by kadeo View Post
    It seems as though the setting is only read by Excel at Excel startup time. Would that be right? I suppose this behaviour could also be something to do with that TRIM add-in.
    I really don't know. I'll take your word for it if you've tested it.

    What if you just saved the workbook as a temporary .xlsm file? Then later delete the temp file after the workbook is closed or SavedAs another file.

  9. #9
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: How do I force saving as *.xlsm without using .SaveAs

    Quote Originally Posted by AlphaFrog View Post
    ...What if you just saved the workbook as a temporary .xlsm file? Then later delete the temp file after the workbook is closed or SavedAs another file.
    Yeah, I had thought of that. In fact that's how I get around another issue with the TRIM add-on that doesn't allow me to specify a name for saving a new file as. The only way you can name a new file being saved to TRIM is to do it in the TRIM save dialog so my macros save to a temp file so that when I then save that to TRIM its no longer a new file and TRIM just uses the name the file already has. Thats OK for me cos I know what's happening and know to hit SaveAs to get it into TRIM. Others though are used to just hitting Save on a new document and as per normal Excel operation, if the file hasn't been previously saved, the Save function operates as SaveAs. So doing it this way risks the user just saving to the temporary file location instead of into TRIM. Using Workbook_BeforeSave to alter this brings me right back to original issue of inserting code into a new workbook. It would be so much easier if I could just get my hands on whatever API the TRIM add-in uses but it has eluded my efforts thus far.

+ 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. Replies: 0
    Last Post: 05-20-2014, 12:01 PM
  2. [SOLVED] Force .xlsm to save as .xlsx or .xls in VBA
    By JayRab in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-17-2014, 03:13 PM
  3. SaveAs alert when change xlsm to xlsx
    By Altair in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-10-2013, 05:50 AM
  4. [SOLVED] Dialog saveas xlsm filter
    By penfold1992 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-08-2013, 01:27 PM
  5. [SOLVED] Force Save as .xlsm filetype
    By DJvdW in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-10-2012, 09:22 AM
  6. [SOLVED] ActiveWorkbook.SaveAs function needs to save as .xlsb instead of .xlsm
    By jonvanwyk in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-21-2012, 10:38 AM
  7. Force overwrite when using SaveAs
    By shawnpgorman in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-13-2007, 11:17 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