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($fil e + '.xlsx')
$filedest='C:\Temp\'
foreach ($ws in $wb.Worksheets)
{
$ws.SaveAs($filedest + '_' + $ws.Name, [Microsoft.Office.Interop. Excel.xlFi leFormat]: :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 ?
This is code.
$file = 'C:\temp\RP\test'
$xlApp = New-Object -Com Excel.Application
$xlApp.Visible = $false
$wb = $xlApp.Workbooks.Open($fil
$filedest='C:\Temp\'
foreach ($ws in $wb.Worksheets)
{
$ws.SaveAs($filedest + '_' + $ws.Name, [Microsoft.Office.Interop.
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 ?
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($fil e + '.xlsx')
$filedest='C:\Temp\'
foreach ($ws in $wb.Worksheets)
{
$ws.SaveAs($filedest + '_' + $ws.Name, [Microsoft.Office.Interop. Excel.xlFi leFormat]: :xlExcel12 )
break
}
$wb.Close(0)
$xlApp.Quit()
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($fil
$filedest='C:\Temp\'
foreach ($ws in $wb.Worksheets)
{
$ws.SaveAs($filedest + '_' + $ws.Name, [Microsoft.Office.Interop.
break
}
$wb.Close(0)
$xlApp.Quit()
Change this line:
$wb = $xlApp.Workbooks.Open($fil e + '.xlsx')
to:
$wb = $xlApp.Workbooks.Open($fil e)
$wb = $xlApp.Workbooks.Open($fil
to:
$wb = $xlApp.Workbooks.Open($fil
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:
Convert-XLSX-to-XLSB.xlsm
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
In attached, just click the button and check.Convert-XLSX-to-XLSB.xlsm
there is a best free tool available in Experts Exchange Article https://www.experts-exchange.com/articles/8269/Convert-between-Excel-file-formats-XLS-XLSX-XLSM-with-without-macro-option.html
it is working.
it is working.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
You already specifying FileFormat as xlExcel12 then you don't need .xlsx