-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod_exc_LinksFiles.bas
123 lines (94 loc) · 3.68 KB
/
mod_exc_LinksFiles.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
Attribute VB_Name = "mod_exc_LinksFiles"
Option Explicit
' error handling tag
Const cStrModuleName As String = "mod_exc_LinksFiles"
' generic functions for manipulating filesystem objects
' and web and sharepoint sites and URLs
'
' 150309.AMG also get URL sub-address
' 150219.AMG added GetURL for hyperlinks
'
' This module may require the following references (paths and GUIDs might vary)
' Tools / References / Microsoft Scripting Runtime
' Scripting (C:\WINDOWS\system32\scrrun.dll) 1.0 - {420B2830-E718-11CF-893D-00A0C9054228}
' (or C:\Windows\SysWOW64\scrrun.dll)
' MSXML2 (C:\WINDOWS\system32\msxml6.dll) 6.0 - {F5078F18-C551-11D3-89B9-0000F81FE221}
Const cStrExcFileFilter As String = "Excel Workbooks, *.xls; *.xlsx"
' Case "xls": strFilter = "Excel Workbooks (*.xls), *.xls"
' Case "txt": strFilter = "Text Files (*.txt), *.txt"
' Case Else: strFilter = "All Files (*.*), *.*"
' *********** HYPERLINKS *********************************************
Function GetURL(rngCell As Range) As String
If rngCell.Hyperlinks.Count > 0 Then
GetURL = Replace _
(rngCell.Hyperlinks(1).Address, "mailto:", "")
If rngCell.Hyperlinks(1).SubAddress <> "" Then
' credit http://excel.tips.net/T003281_Extracting_URLs_from_Hyperlinks.html
GetURL = GetURL & "#" & rngCell.Hyperlinks(1).SubAddress
End If
End If
End Function
' *********** FILE AND PATH NAMES *********************************************
' Make this use generic arrFilteredPathnamesInUserTree in mod_off_FilesFoldersSitesLinks
' re cast as array of full file paths
Function EnumerateExcelFiles()
' This module is used to enumerate all XLS files in a
' folder, chosen by the user
' 071030.AMG created
Dim SourceFilename As String
Dim SourceFolderName As String
Dim wbk As Workbook
' Ask user to identify a file in source folder
'
SourceFilename = CStr(Application.GetOpenFilename( _
FileFilter:=cStrExcFileFilter, _
Title:="Choose one of the files in the source folder", _
ButtonText:="Select"))
SourceFolderName = GetFolderFromFileName(SourceFilename)
' enumerate all XLS files in the folder
' I wanted to use FileSearch VBA object,
' but it looks like SearchScopes were getting in the way
'
'With Application.FileSearch
' .NewSearch
' .LookIn = SourceFolder
' .SearchSubFolders = False
' '.FileType = msoFileTypeExcelWorkbooks
' .FileName = "*.xls"
' If .Execute > 0 Then
' MsgBox "There were " & .FoundFiles.Count & _
' " file(s) found."
' Dim FileCount As Integer
' For FileCount = 1 To .FoundFiles.Count
' MsgBox .FoundFiles(FileCount)
'
'
' Next FileCount
' End If
'End With
' so I went back to good ol' FileSystemObject from Shell Scripting
'
Dim fso As Scripting.FileSystemObject
Dim SourceFolder As Scripting.folder
Dim SourceFile As Scripting.file
Set fso = New Scripting.FileSystemObject
Set SourceFolder = fso.GetFolder(SourceFolderName)
'Application.ScreenUpdating = False
For Each SourceFile In SourceFolder.Files
If LCase(Right(SourceFile.Name, 5)) = ".xlsx" _
Or LCase(Right(SourceFile.Name, 4)) = ".xls" Then
' Open each workbook and put the name in the status bar
Application.StatusBar = "reading from [" & SourceFile.Name & " ]..."
Set wbk = Workbooks.Open( _
FileName:=SourceFolderName & Application.PathSeparator & SourceFile.Name, _
UpdateLinks:=0, _
ReadOnly:=True, _
IgnoreReadOnlyRecommended:=True _
)
' ******************
' Do your stuff here
' ******************
wbk.Close SaveChanges:=False
End If
Next SourceFile
End Function