0% found this document useful (0 votes)
11 views2 pages

Select Word Files in Excel VBA

The document is a VBA module for selecting Word certificate files using a file dialog in Excel. It initializes variables, allows multiple file selection, checks for duplicates, and updates a worksheet with the selected files while providing user feedback. The module also manages error handling and displays the number of unique and duplicate files selected.

Uploaded by

Majdi Belguith
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Select Word Files in Excel VBA

The document is a VBA module for selecting Word certificate files using a file dialog in Excel. It initializes variables, allows multiple file selection, checks for duplicates, and updates a worksheet with the selected files while providing user feedback. The module also manages error handling and displays the number of unique and duplicate files selected.

Uploaded by

Majdi Belguith
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Attribute VB_Name = "Module_SelectFiles"

'Attribute VB_Name = "Module_SelectFiles"


Option Explicit
Public Sub SelectFiles_Step1()
Dim fd As FileDialog
Dim ws As Worksheet: Set ws = [Link](SHEET_DETECT)
Dim i As Long, path As String
Dim startRow As Long, lastRow As Long
Dim newUnique As Long, duplicates As Long
Dim oneDriveRoot As String

' Initialisation des variables globales


If gFileIndex Is Nothing Then Set gFileIndex =
CreateObject("[Link]")
If gStartRowFiles = 0 Then gStartRowFiles = ROW_FILES_START

' Bo�te de s�lection


Set fd = [Link](msoFileDialogFilePicker)
With fd
.AllowMultiSelect = True
.title = "Select one or more Word certificate files"
.[Link]
.[Link] "Word Documents", "*.doc;*.docx"

On Error Resume Next


oneDriveRoot = Environ$("OneDrive")
If Len(oneDriveRoot) > 0 Then .InitialFileName = oneDriveRoot &
[Link]
On Error GoTo 0

' ---- CAS ANNULATION ----


If .show <> -1 Then
[Link]("B" & ROW_ERR1).value = "No file selected."
[Link]("B" & ROW_ERR2).value = "Existing files will remain active."

' Garder Analyze actif si compteur > 0


If [Link] > 0 Then
UI_ShowAnalyze True
End If
Exit Sub
End If

' S�lection r�elle


Set gSelectedFiles = .SelectedItems
End With

' ---- V�rification s�lection vide ----


If gSelectedFiles Is Nothing Or [Link] = 0 Then
[Link]("B" & ROW_ERR1).value = "No file selected."
[Link]("B" & ROW_ERR2).value = "Please select at least one document."

If [Link] > 0 Then


UI_ShowAnalyze True
End If
Exit Sub
End If

' ---- Ajout des fichiers s�lectionn�s ----


lastRow = [Link]([Link], "B").End(xlUp).Row
startRow = IIf(lastRow < gStartRowFiles, gStartRowFiles, lastRow + 1)

newUnique = 0
duplicates = 0

For i = 1 To [Link]
path = CStr(gSelectedFiles(i))

If Len(path) > 0 Then


If Not [Link](path) Then
[Link] path, True
[Link](startRow, "B").value = path
[Link](startRow, "A").ClearContents ' Reset count
startRow = startRow + 1
newUnique = newUnique + 1
Else
duplicates = duplicates + 1
End If
End If
Next i

' ---- Mise � jour compteur ----


[Link]("B" & ROW_COUNT).value = [Link] & " file(s) selected."
[Link]

[Link]("B" & ROW_ERR1 & ":B" & ROW_ERR2).ClearContents


If duplicates > 0 Then
[Link]("B" & ROW_ERR1).value = duplicates & " duplicate file(s) ignored."
[Link]("B" & ROW_ERR2).value = "Each file is processed only once."
End If

' ---- Afficher Analyze ----


If [Link] > 0 Then
UI_ShowAnalyze True
End If
End Sub

You might also like