title: Calling .SaveAs Crashes Excel
body: <p>I have created an <code>xlam</code> (Excel 2007 Add-In) file to handle manipulation of various files. I am trying to write a procedure in that <code>xlam</code> file that removes some worksheets from an opened <code>xlsm</code> file, and saves it as an <code>xlsx</code> (i.e. without macros). </p>
  
  <p>So far the only thing I can do reliably is to crash Excel whenever I reach the <code>.SaveAs</code> call. The crash comes as a Windows Dialog stating:</p>
  
  <blockquote>
    <p>Microsoft Office Excel has stopped working, Windows can try to recover your information and restart the program. [Restart the program] [Debug the Program]</p>
  </blockquote>
  
  <p>In the folder that I am saving to, after every crash I am left with a temp file (ex. filename: <em>7A275000</em> with size: 0) in the folder it tried to save to.</p>
  
  <p>For posterity here some things I have tried, and all have resulted in the same crash:  </p>
  
  <ul>
  <li>Hard coded filename value ("C:\Users\myUserName\Desktop\temp.xlsx")</li>
  <li>Prompted filename from User (shown in code below)</li>
  <li>filename without path ("temp.xlsx")</li>
  <li>filename without extension ("C:\Users\myUserName\Desktop\temp")</li>
  <li>filename as existing filename without extension</li>
  <li>filename as existing filename with .xlsx extension</li>
  <li>instead of using <code>wb.SaveAs</code>, I used <code>wb.Activate</code> followed by <code>ActiveWorkbook.SaveAs</code></li>
  <li>I have tried <code>FileFormat:=xlOpenXMLWorkbook</code> and <code>FileFormat:=xlWorkbookNormal</code></li>
  <li>Saved to several different directories of varying length</li>
  <li>Added an Error trapping statement around the <code>.SaveAs</code> call (it does not trap any errors, and crashes Excel just the same)</li>
  </ul>
  
  <p>The last weird bit is when I try to do a manual Save-As on the file (i.e. navigating to the Save-As menu myself) after the <code>ws.delete</code> calls, Excel crashes the same way. If I manually delete the Worksheets myself, then do a manual Save-As, it saves just fine.</p>
  
  <hr>
  
  <p>Here is the offending code:</p>
  
  <pre><code>Public Sub ConvertToStagingFile(ByRef wb As Workbook)
      Dim reWS As Object, reFILE As Object
      Dim ws As Worksheet
  
      Set reWS = CreateObject("VBScript.regexp")
      reWS.IgnoreCase = True: reWS.Global = False: reWS.MultiLine = False
  
      Set reFILE = CreateObject("VBScript.regexp")
      reFILE.IgnoreCase = True: reFILE.Global = False: reFILE.MultiLine = False
  
      reWS.Pattern = "^(home|location settings|date reference|[\w\s]{1,8} (rating|inquire) data|pkl data - \w{1,8}|verbs - \w{1,8})"
      reFILE.Pattern = "\.xlsm$"
  
      For Each ws In wb.Worksheets
          If (ws.Visible = xlSheetHidden) Or (ws.Visible = xlSheetVeryHidden) Then
              ws.Visible = xlSheetVisible
          End If
  
          Select Case True
              Case reWS.test(ws.name)
                  '// Do Nothing
              Case Else
                  Application.DisplayAlerts = False
                  ws.Delete
                  Application.DisplayAlerts = True
          End Select
      Next ws
  
      ActiveWindow.TabRatio = 0.75
  
      If (reFILE.test(Cached.getAdhocReportFull)) Then
          Dim newName As Variant
  
          newName = Application.GetSaveAsFilename(reFILE.Replace(Cached.getAdhocReportFull, ""), "*.xlsx")
          If newName = False Then Exit Sub
  
          wb.Activate
  
          Application.EnableEvents = False
          '// CODE RELIABLY CRASHES HERE
          wb.SaveAs _
              FileName:=newName, _
              FileFormat:=xlOpenXMLWorkbook, _
              CreateBackup:=False
  
          Application.EnableEvents = True
  
      End If
  End Sub
  </code></pre>
  
  <p>Any help on this issue would be greatly appreciated.</p>