M7cks
XLDnaute Nouveau
Bonjour à tous,
Voilà, chaque mois je reçois une vingtaine de fichiers exportés par un logiciel qui ne peut qu'exporter les fichiers en XLS et j'en ai besoin en XLSX.
Le nombre de fichiers grandissant une automatisation de la tâche s'impose.
Après beaucoup de recherches j'ai trouvé une fonction écrite par un très bon développeur en la matière, la fonction marche super bien pour un fichier, après un court échange avec le développeur en question il m'a partagé un bout de code pour "loop" la fonction afin de convertir tout un dossier.
Le problème c'est que pour lui tout fonctionne très bien alors que de mon coté j'ai que le premier fichier de convertie et après ça me fait une erreur "424 Object Required".
J'essaye de comprendre comment résoudre ça mais là, je bloque, est-ce que quelqu'un aurait une idée ?
Voici la fonction :
	
	
	
	
	
		
Voici la boucle :
	
	
	
	
	
		
	
		
			
		
		
	
				
			Voilà, chaque mois je reçois une vingtaine de fichiers exportés par un logiciel qui ne peut qu'exporter les fichiers en XLS et j'en ai besoin en XLSX.
Le nombre de fichiers grandissant une automatisation de la tâche s'impose.
Après beaucoup de recherches j'ai trouvé une fonction écrite par un très bon développeur en la matière, la fonction marche super bien pour un fichier, après un court échange avec le développeur en question il m'a partagé un bout de code pour "loop" la fonction afin de convertir tout un dossier.
Le problème c'est que pour lui tout fonctionne très bien alors que de mon coté j'ai que le premier fichier de convertie et après ça me fait une erreur "424 Object Required".
J'essaye de comprendre comment résoudre ça mais là, je bloque, est-ce que quelqu'un aurait une idée ?
Voici la fonction :
		VB:
	
	
	'---------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------
' Procedure : XLS_ConvertXLS2XLSX
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Converts an xls (2003-) into an xlsx (2007+)
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sXLSFile  : String - XLS file path, name and extension to be converted
' bDelXLS   : True/False - Should the original XLS file be deleted after the conversion
'
' Usage:
' ~~~~~~
' Call XLS_ConvertXLS2XLSX("C:TempTest.xls")
' Call XLS_ConvertXLS2XLSX("C:TempTest.xls", False)
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2018-02-27              Initial Release
'---------------------------------------------------------------------------------------
Function XLS_ConvertXLS2XLSX(ByVal sXLSFile As String, Optional bDelXLS As Boolean = True)
          '#Const EarlyBind = True 'Use Early Binding, Req. Reference Library
    #Const EarlyBind = False    'Use Late Binding
    #If EarlyBind = True Then
              'Early Binding Declarations
              Dim oExcel            As Excel.Application
              Dim oExcelWrkBk       As Excel.WorkBook
    #Else
              'Late Binding Declaration/Constants
              Dim oExcel            As Object
              Dim oExcelWrkBk       As Object
              Const xlOpenXMLWorkbook = 51
    #End If
          Dim bExcelOpened          As Boolean
          'Start Excel
10        On Error Resume Next
20        Set oExcel = GetObject(, "Excel.Application")      'Bind to existing instance of Excel
30        If Err.Number <> 0 Then      'Could not get instance of Excel, so create a new one
40            Err.Clear
50            On Error GoTo Error_Handler
60            Set oExcel = CreateObject("Excel.Application")
70            bExcelOpened = False
80        Else      'Excel was already running
90            bExcelOpened = True
100       End If
110       On Error GoTo Error_Handler
120       oExcel.ScreenUpdating = False
130       oExcel.Visible = False     'Keep Excel hidden until we are done with our manipulation
140       Set oExcelWrkBk = oExcel.Workbooks.Open(sXLSFile)
150       oExcelWrkBk.SaveAS Replace(sXLSFile, ".xls", ".xlsx"), xlOpenXMLWorkbook, , , , False
160       oExcelWrkBk.Close False
170       If bExcelOpened = True Then oExcel.Quit
180       If bDelXLS = True Then Kill (sXLSFile)
Error_Handler_Exit:
190       On Error Resume Next
200       Set oExcelWrkBk = Nothing
210       Set oExcel = Nothing
220       Exit Function
Error_Handler:
230       MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
                 "Error Number: " & Err.Number & vbCrLf & _
                 "Error Source: XLS_ConvertXLS2XLSX" & vbCrLf & _
                 "Error Description: " & Err.Description & _
                 Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
                 , vbOKOnly + vbCritical, "An Error has Occured!"
240       oExcel.ScreenUpdating = True
250       oExcel.Visible = True     'Make excel visible to the user
260       Resume Error_Handler_Exit
End FunctionVoici la boucle :
		Code:
	
	
	Dim sFile                 As String
    Dim sPath                 As String
    sPath = "C:Temp"    'Folder to iterate over the xls files and convert to xlsx
    If Right(sPath, 1) <> "" Then sPath = sPath & ""
    sFile = Dir(sPath & "*.xls")
    Do While sFile <> vbNullString
        If sFile <> "." And sFile <> ".." Then
            '            sFile 'FileName, does not include the path
            Call XLS_ConvertXLS2XLSX(sPath & sFile, False) 'False, to perform the conversion but retain the original xls files
        End If
        sFile = Dir     'Loop through the next file that was found
    Loop
			
				Dernière édition: 
			
		
	
								
								
									
	
		
			
		
		
	
	
	
		
			
		
		
	
								
							
							 
	 
 
		