OPEN
Option Explicit
Public Function GetAllFilesInDir(ByVal strDirPath As String) As Variant
' Loop through the directory specified in strDirPath and save each
' file name in an array, then return that array to the calling
' procedure.
' Return False if strDirPath is not a valid directory.
Dim strTempName As String
Dim varFiles() As Variant
Dim lngFileCount As Long
On Error GoTo GetAllFiles_Err
' Make sure that strDirPath ends with a "\" character.
If Right$(strDirPath, 1) <> "\" Then
strDirPath = strDirPath & "\"
End If
' Make sure strDirPath is a directory.
If GetAttr(strDirPath) = vbDirectory Then
strTempName = Dir(strDirPath, vbDirectory)
Do Until Len(strTempName) = 0
' Exclude ".", "..".
If (strTempName <> ".") And (strTempName <> "..") Then
' Make sure we do not have a sub-directory name.
If (GetAttr(strDirPath & strTempName) _
And vbDirectory) <> vbDirectory Then
' Increase the size of the array
' to accommodate the found filename
' and add the filename to the array.
'Control the file type(optional) for entering the array
If Right(strTempName, 4) = ".out" Then
ReDim Preserve varFiles(lngFileCount)
varFiles(lngFileCount) = strTempName
lngFileCount = lngFileCount + 1
End If
End If
End If
' Use the Dir function to find the next filename.
strTempName = Dir()
Loop
' Return the array of found files.
GetAllFilesInDir = varFiles
End If
GetAllFiles_End:
Exit Function
GetAllFiles_Err:
GetAllFilesInDir = False
Resume GetAllFiles_End
End Function
Public Function GetAllFiles(ByVal folderPath As String) As Long
Dim varFileArray As Variant
Dim lngI As Long
Dim strDirName As String
Const NO_FILES_IN_DIR As Long = 9
Const INVALID_DIR As Long = 13
' On Error GoTo Test_Err
strDirName = folderPath
varFileArray = GetAllFilesInDir(strDirName)
GetAllFiles = UBound(varFileArray)
For lngI = 0 To UBound(varFileArray)
Debug.Print "Inside the Array: " & varFileArray(lngI)
Documents.Open strDirName & "\" & varFileArray(lngI)
'ActiveDocument.Close , varFileArray(lngI)
Next lngI
End Function
Public Function openSingleFile(ByVal fn As String) As Boolean
Workbooks.OpenText Filename:= _
fn, Origin:= _
950, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), _
Array(12, 1), Array(94, 1), Array(122, 1), Array(150, 1)), TrailingMinusNumbers:= _
True
Cells.Select
Selection.Columns.AutoFit
openSingleFile_End:
Exit Function
openSingleFile_Err:
openSingleFile = False
Resume openSingleFile_End
End Function
Save
Option Explicit
Public Function toSave(ByVal fileNamePrefix As String, ByVal filesExtensionAfterCon As String) As Boolean
Dim Filename
Dim callSaveVar As Boolean
'Avoid if the user new a document (i.e. Document1 )
If (InStr(ActiveDocument.Name, ".") <> 0) Then
Filename = ActiveDocument.path & fileNamePrefix & Left(ActiveDocument.Name, InStrRev(ActiveDocument.Name, ".") - 1)
callSaveVar = checkIfDocExistNDetermineIfSave(Filename, filesExtensionAfterCon)
'To save the document b4 quit
ActiveDocument.Close savechanges:=True
toSave = callSaveVar
Else
Filename = ActiveDocument.path & fileNamePrefix & ActiveDocument.Name
Select Case VBA.MsgBox("Do you want to save the file as " & Filename, vbYesNo, "Confirm to save the file")
Case VBA.vbYes
callSaveVar = checkIfDocExistNDetermineIfSave(Filename, filesExtensionAfterCon)
'To save the document b4 quit
ActiveDocument.Close savechanges:=True
toSave = callSaveVar
Case VBA.vbNo
MsgBox "No file reformatted", vbInformation, "Action aborted"
toSave = False
End Select
End If
End Function
Public Function checkIfDocExistNDetermineIfSave(ByVal strPath As String, ByVal filesExtensionAfterCon As String) As Boolean
Dim checkFileExistsWorker As StartReformat
Set checkFileExistsWorker = New StartReformat
Dim isFileExists As Boolean
Dim isSaveOK As Boolean
isFileExists = checkFileExistsWorker.IsFileOrDirExists(strPath & "." & filesExtensionAfterCon)
Dim isSaveAnyway As Boolean
If (isFileExists) Then
'isSaveAnyway = MsgBox("The file " & strPath & ".doc is existed already, do you still want to save??", vbYesNoCancel, "Files Exists")
Select Case VBA.MsgBox("The file " & strPath & "." & filesExtensionAfterCon & " is existed already, do you still want to save??", vbYesNoCancel, "Files Exists")
Case VBA.vbYes
isSaveOK = Save(strPath, filesExtensionAfterCon)
checkIfDocExistNDetermineIfSave = isSaveOK
Case VBA.vbNo
MsgBox "File not saved due to duplication", vbExclamation, "Duplicated File not been sasved"
checkIfDocExistNDetermineIfSave = False
Case VBA.vbCancel
MsgBox "File not saved as you stop the process", vbExclamation, "File abort by user"
checkIfDocExistNDetermineIfSave = False
End Select
Else
isSaveOK = Save(strPath, filesExtensionAfterCon)
checkIfDocExistNDetermineIfSave = isSaveOK
End If
End Function
Public Function Save(ByVal strPath As String, ByVal filesExtensionAfterCon As String) As Boolean
ActiveDocument.SaveAs Filename:=strPath & "." & filesExtensionAfterCon, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
Select Case Err.Number
Case Is = 0
Save = True
Case Else
Save = False
MsgBox Err.Number & " " & Err.Description, vbMsgBoxHelpButton
End Select
End Function
沒有留言:
張貼留言