SaveAs in Excel 2007

Hi all

Creating a new workbook from the active sheet is not so easy anymore now we
have Excel 2007 with so many different file formats.

Below is a code example to copy the active sheet to a new workbook and save it.
This example is working in 97/2007.

If you run the code in Excel 2007 it will look at the FileFormat of the parent workbook and save the new file in that format.
Only if the parent workbook is an xlsm file and if there is no code in the new workbook it will save the new file as xlsx,
If the parent workbook is not an xlsx, xlsm, or xls then it will be saved as xlsb.

This are the main formats in Excel 2007 :

51 = xlOpenXMLWorkbook (without macro’s in 2007, xlsx)
52 = xlOpenXMLWorkbookMacroEnabled (with or without macro’s in 2007, xlsm)
50 = xlExcel12 (Excel Binary Workbook in 2007 with or without macro’s, xlsb)
56 = xlExcel8 (97-2003 format in Excel 2007, xls)

If you always want to save in a certain format you can replace this part of the macro

Select Case Sourcewb.FileFormat
    Case 51: FileExtStr = “.xlsx”: FileFormatNum = 51
    Case 52:
        If .HasVBProject Then
            FileExtStr = “.xlsm”: FileFormatNum = 52
        Else
            FileExtStr = “.xlsx”: FileFormatNum = 51
        End If
    Case 56: FileExtStr = “.xls”: FileFormatNum = 56
    Case Else: FileExtStr = “.xlsb”: FileFormatNum = 50
End Select

With one of the one liners from this list

FileExtStr = “.xlsb”: FileFormatNum = 50
FileExtStr = “.xlsx”: FileFormatNum = 51
FileExtStr = “.xlsm”: FileFormatNum = 52
FileExtStr = “.xls”: FileFormatNum = 56

Or maye you want to save the one sheet workbook to csv, txt ot prn.
(you can use this also if you run it in 97-2003)

FileExtStr = “.csv”: FileFormatNum = 6
FileExtStr = “.txt”: FileFormatNum = -4158
FileExtStr = “.prn”: FileFormatNum = 36

Sub Copy_ActiveSheet()
‘Working in Excel 97-2007
   Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
 
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
 
    Set Sourcewb = ActiveWorkbook
 
    ‘Copy the sheet to a new workbook
   ActiveSheet.Copy
    Set Destwb = ActiveWorkbook
 
    ‘Determine the Excel version and file extension/format
   With Destwb
        If Val(Application.Version) < 12 Then
            ‘You use Excel 97-2003
           FileExtStr = “.xls”: FileFormatNum = -4143
        Else
            ‘You use Excel 2007
           ‘We exit the sub when your answer is NO in the security dialog that you only
           ‘see  when you copy a sheet from a xlsm file with macro’s disabled.
           If Sourcewb.Name = .Name Then
                With Application
                    .ScreenUpdating = True
                    .EnableEvents = True
                End With
                MsgBox “Your answer is NO in the security dialog”
                Exit Sub
            Else
                Select Case Sourcewb.FileFormat
                Case 51: FileExtStr = “.xlsx”: FileFormatNum = 51
                Case 52:
                    If .HasVBProject Then
                        FileExtStr = “.xlsm”: FileFormatNum = 52
                    Else
                        FileExtStr = “.xlsx”: FileFormatNum = 51
                    End If
                Case 56: FileExtStr = “.xls”: FileFormatNum = 56
                Case Else: FileExtStr = “.xlsb”: FileFormatNum = 50
                End Select
            End If
        End If
    End With
 
    ‘    ‘Change all cells in the worksheet to values if you want
   ‘    With Destwb.Sheets(1).UsedRange
   ‘        .Cells.Copy
   ‘        .Cells.PasteSpecial xlPasteValues
   ‘        .Cells(1).Select
   ‘    End With
   ‘    Application.CutCopyMode = False

    ‘Save the new workbook and close it
   TempFilePath = Application.DefaultFilePath & “”
    TempFileName = “Part of “ & Sourcewb.Name & ” “ & Format(Now, “dd-mmm-yy h-mm-ss”)
 
    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        .Close SaveChanges:=False
    End With
 
    MsgBox “You can find the new file in “ & Application.DefaultFilePath
 
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

Also if you use code like GetSaveAsFilename then you must add a lot of code.

See the second example on this page
http://www.rondebruin.nl/saveas.htm


PDF in Excel 2007

Hi all

Excel 2007 B2TR and the final version will not have SaveAs PDF by Default.
You must install a MS ADD-in if you want to have this option in Office.

I am really glad the MS have added this in Excel 2007.
The 2007 version of my SendMail add-in will have this option also.

On this page there are a few vba examples and the link to the add-in so you can try it.
http://www.rondebruin.nl/pdf.htm

Have fun

Ron de Bruin
http://www.rondebruin.nl/tips.htm

Excel/Outlook mail add-in version for Excel 2007 Beta2 TR

Hi all

Here is the first Excel/Outlook mail add-in version for Excel 2007 Beta2 TR.
The versions for 97-2003 you can find here : http://www.rondebruin.nl/mail/add-in.htm

You can use it for xls, xlsx, xlsm, xlsb files.
It always save the new file you create in the same format as the Activeworkbook.
Only if you want to send a sheet from an xlsm file and there is no code then it save it as xlsx.

There is also an option to send as PDF (you must install the MS PDF add-in for that).
It will check if the add-in is installed on the machine when you open the add-in.

There is a limit of 30000 cells now for the Selection/PDF options.
Not sure about the limit but I start with this.

Link to the MS PDF add-in
http://www.microsoft.com/downloads/details.aspx?FamilyID=f1fc413c-6d89-4f15-991b-63b07ba5f2e5&DisplayLang=en

If Winzip is installed you can also use the Zip option (Shift>Go).

Download it here
http://www.rondebruin.nl/files/SendMail2007.zip
There is a option to open the Add-in in the Cell menu.

Ron de Bruin
http://www.rondebruin.nl

Mail with CDO for Win 2000

Hi all

I update the CDO page on http://www.rondebruin.nl/sendmail.htm :

I add a new example to my CDO for Win 2000 page to send the active sheet with pictures in the body of the mail without security warnings.
Note: It doesn’t matter what Mail program you use, it only use the SMTP server.

Read the information good on my site before you try it
http://www.rondebruin.nl/cdo.htm

Have fun

Ron de Bruin
http://www.rondebruin.nl/tips.htm

Copy data from an Access database into Excel

Hi all

Last week I start building a page about Excel-Access.
I want to add more pages soon about Excel-Access but I start with this one
“Copy data from an Access database into Excel”

Most examples on the internet are not so easy to work with for a normal user like me.
I go try to change that on this webpage.

In example workbook 2 I use data validation cells to fill in the criteria and in example
workbook 3 also the Calendar control to fill in the dates.

I add a new workbook where you save 100 or more criteria.
Very easy to get the result you want this way

Suggestions good or bad are welcome

You can find it here
http://www.rondebruin.nl/accessexcel.htm

Ron de Bruin
http://www.rondebruin.nl

Good old DOS

Hi all

When Dave Peterson posted this the first time in the newsgroup to a question about
merging CSV files I say WOW that’s great.

1) Windows Start Button | Run
2) Type cmd and hit enter (“command” in Win 98)
3) Go to the folder with the CSV files (for help enter “help cd”)
4) Type copy *.csv all.txt and hit enter to copy all data in the files into all.txt.
5) Type exit and hit enter to close the DOS window

Very simple and also fast.

I add a VBA example to my site that do the same thing, see
http://www.rondebruin.nl/csv.htm

Question: do you use other things like this in your work

Ron de Bruin
http://www.rondebruin.nl