Link to home
Start Free TrialLog in
Avatar of Darshan Shah
Darshan Shah

asked on

XLSX to XLSB conversion.

I want to process multiple files from the folder.

This is code.
$file = 'C:\temp\RP\test'
$xlApp = New-Object -Com Excel.Application
$xlApp.Visible = $false
$wb = $xlApp.Workbooks.Open($file + '.xlsx')
$filedest='C:\Temp\'
foreach ($ws in $wb.Worksheets)
{
  $ws.SaveAs($filedest + '_' + $ws.Name, [Microsoft.Office.Interop.Excel.xlFileFormat]::xlExcel12)
  break
}
$wb.Close(0)
$xlApp.Quit()

I tried using below  but  failed.
$file = Get-ChildItem "C:\temp\RP\" -Filter *.xlsx .

Could any one please hemp in this ?
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Hi Darshan,

You already specifying FileFormat as xlExcel12 then you don't need .xlsx
Avatar of Darshan Shah
Darshan Shah

ASKER

Hi Shums,

That is for to open only .xlsx files. the code is working for single file. But I want to use this code to work on multiple file exists in folder. so need one loop. I did R&D for it. And found below statement to work on multiple files. but it is not working.

$file = Get-ChildItem "C:\temp\RP\" -Filter *.xlsx
$xlApp = New-Object -Com Excel.Application
$xlApp.Visible = $false
$wb = $xlApp.Workbooks.Open($file + '.xlsx')
$filedest='C:\Temp\'
foreach ($ws in $wb.Worksheets)
{
  $ws.SaveAs($filedest + '_' + $ws.Name, [Microsoft.Office.Interop.Excel.xlFileFormat]::xlExcel12)
  break
}
$wb.Close(0)
$xlApp.Quit()
Change this line:
$wb = $xlApp.Workbooks.Open($file + '.xlsx')
to:
$wb = $xlApp.Workbooks.Open($file)
Below is a VBA version. It will allow you to select the folder which has XLSX files and will create another folder Converted XLSB folder and all your xlsx files will be as xlsb in that folder:
Sub Convert_XLSX_To_XLSB()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    Dim dialog As FileDialog: dialog = Application.FileDialog(msoFileDialogFolderPicker)
    With dialog
        .Title = "Select Folder"
        .AllowMultiSelect = False
        .Show
    End With

    If dialog.SelectedItems.Count = 0 Then End
    Dim curFile As String, wb As Workbook, convDir As String: convDir = dialog.SelectedItems(1)

    MkDir (convDir & "\Converted XLSB")
    curFile = Dir(convDir & "\*.xlsx")
    While Len(curFile) > 0
        Set wb = Workbooks.Open(convDir & "\" & curFile)
        wb.SaveAs convDir & "\Converted XLSB\" & Left(curFile, InStrRev(curFile, ".")) & "xlsb", 50
       wb.Close
        curFile = Dir
    Wend
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
End Sub

Open in new window

In attached, just click the button and check.
Convert-XLSX-to-XLSB.xlsm
ASKER CERTIFIED SOLUTION
Avatar of Darshan Shah
Darshan Shah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This script work on multiple files exist in one folder. and it is as per my requirement. I can schedule this  to convert file from .xlsx to xlsb.