-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule1.bas
More file actions
250 lines (214 loc) · 9.25 KB
/
Module1.bas
File metadata and controls
250 lines (214 loc) · 9.25 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
Attribute VB_Name = "Module1"
Option Explicit
#If VBA7 Then
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
ByVal hWndParent As LongPtr, ByVal hWndChildAfter As LongPtr, _
ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
ByVal hWnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
ByVal hWndParent As Long, ByVal hWndChildAfter As Long, _
ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( _
ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
Private Const LOAD_WAIT_SECONDS As Long = 6
Private Const MAX_REV_CHARS As Long = 4
'=============================================================
' MAIN PROCEDURE
'=============================================================
Sub ExtractChromeTabRev()
Dim ws As Worksheet
Dim cell As Range, r As Variant
Dim windowTitle As String, revText As String
Dim rangesToCheck As Variant
Dim shp As Shape, h As Hyperlink
Dim rngBorders As Range, rngSpec As Variant
Dim savePath As String, fileName As String
Dim tempCols As Variant
Dim rCheck As Variant, c As Range
Dim newCell As Range
Dim oldVal As String, newVal As String
Dim i As Long
Set ws = ActiveSheet
rangesToCheck = Array("A3:A32", "G7:G8", "G12:G22")
' --- TEMP COLUMNS (one per range) to avoid overwriting when rows overlap ---
tempCols = Array(ws.Columns("Z").Column, ws.Columns("AA").Column, ws.Columns("AB").Column)
' --- BACKUP EXISTING REV VALUES (one temp column per range) ---
For i = LBound(rangesToCheck) To UBound(rangesToCheck)
For Each c In ws.Range(rangesToCheck(i))
ws.Cells(c.Row, tempCols(i)).Value = c.Offset(0, 1).Value
Next c
Next i
' --- CLEAR PREVIOUS RESULTS (target cells where revs go) ---
For Each r In rangesToCheck
ws.Range(r).Offset(0, 1).ClearContents
Next r
' --- PROCESS LINKS: open and extract revs ---
For Each r In rangesToCheck
For Each cell In ws.Range(r)
If cell.Hyperlinks.Count > 0 Then
cell.Hyperlinks(1).Follow NewWindow:=True
Sleep LOAD_WAIT_SECONDS * 1000
windowTitle = GetChromeWindowTitle()
If Len(windowTitle) > 0 Then
revText = ExtractRevFromTitle(windowTitle)
If Len(revText) > 0 Then
cell.Offset(0, 1).Value = revText
Else
cell.Offset(0, 1).Value = ""
End If
Else
cell.Offset(0, 1).Value = ""
End If
End If
Next cell
Next r
' --- COMPARE OLD AND NEW VALUES; HIGHLIGHT ONLY TRUE CHANGES ---
For i = LBound(rangesToCheck) To UBound(rangesToCheck)
For Each newCell In ws.Range(rangesToCheck(i))
oldVal = CleanForCompare(ws.Cells(newCell.Row, tempCols(i)).Value)
newVal = CleanForCompare(newCell.Offset(0, 1).Value)
' Treat placeholder tokens as empty (in case they show up)
If oldVal = "REVNOTFOUND" Or oldVal = "NOWINDOW" Then oldVal = ""
If newVal = "REVNOTFOUND" Or newVal = "NOWINDOW" Then newVal = ""
' Now compare: highlight only if different and at least one is non-empty
If oldVal <> newVal Then
If Not (oldVal = "" And newVal = "") Then
newCell.Offset(0, 1).Interior.Color = vbYellow
End If
End If
Next newCell
Next i
' --- REMOVE TEMPORARY BACKUPS (clear only the rows we used) ---
For i = LBound(rangesToCheck) To UBound(rangesToCheck)
For Each c In ws.Range(rangesToCheck(i))
ws.Cells(c.Row, tempCols(i)).ClearContents
Next c
Next i
' --- CLEANUP BEFORE SAVE ---
' 1. Remove all hyperlinks but preserve visible text
For Each h In ws.Hyperlinks
h.Range.Value = h.Range.Value
Next h
ws.Hyperlinks.Delete
' 1a. Reapply "All Borders" to the specified ranges
For Each rngSpec In Array("A3:A32", "G7:G8", "G12:G22")
Set rngBorders = ws.Range(rngSpec)
With rngBorders.Borders
.LineStyle = xlContinuous
.Weight = xlThin
End With
Next rngSpec
' 2. Unmerge and clear G30 (unmerge BEFORE clear)
With ws.Range("G30")
If .MergeCells Then .UnMerge
.ClearContents
End With
' 3. Delete form control button(s)
For Each shp In ws.Shapes
If shp.Type = msoFormControl Then
If shp.FormControlType = xlButtonControl Then shp.Delete
End If
Next shp
' --- RECORD DATE AND TIME ---
ws.Range("K34").Value = Date
ws.Range("K35").Value = Time
' --- SAVE MACRO-FREE COPY (.xlsx) ---
fileName = "Quattro Revisions.xlsx"
savePath = ThisWorkbook.Path & "\" & fileName
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs fileName:=savePath, FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
MsgBox "Revisions updated and compared successfully!" & vbCrLf & _
"Changed cells highlighted." & vbCrLf & _
"Macro-free copy saved as: " & fileName, vbInformation
End Sub
'=============================================================
' CleanForCompare:
' - removes invisible chars (NBSP, zero-width, BOM)
' - strips CR/LF/tabs
' - trims & collapses spaces
' - returns ONLY alphanumeric characters in UPPERCASE
'=============================================================
Private Function CleanForCompare(ByVal v As Variant) As String
Dim s As String, outS As String
Dim i As Long, ch As String
On Error GoTo CleanFail
If IsError(v) Or IsNull(v) Then CleanForCompare = "": Exit Function
s = CStr(v)
' Remove common invisible characters
s = Replace(s, Chr(160), " ") ' non-breaking space
s = Replace(s, ChrW(8203), "") ' zero-width space
s = Replace(s, ChrW(65279), "") ' BOM/ZWNBSP
s = Replace(s, vbCr, "")
s = Replace(s, vbLf, "")
s = Replace(s, vbTab, " ")
s = Trim$(s)
On Error Resume Next
s = Application.WorksheetFunction.Trim(s) ' collapse multiple internal spaces
On Error GoTo CleanFail
s = UCase$(s)
' Build output with only A-Z and 0-9
outS = ""
For i = 1 To Len(s)
ch = Mid$(s, i, 1)
If ch Like "[A-Z0-9]" Then outS = outS & ch
Next i
CleanForCompare = outS
Exit Function
CleanFail:
CleanForCompare = ""
End Function
'=============================================================
' Find any Chrome/Edge (or other) window containing "Rev"
'=============================================================
Private Function GetChromeWindowTitle() As String
Dim hWnd As LongPtr
Dim sBuffer As String * 255
Dim lLen As Long
Dim classNames As Variant
Dim cls As Variant
Dim title As String
classNames = Array("Chrome_WidgetWin_1", "Edge_WidgetWin_1", "MozillaWindowClass", "IEFrame")
For Each cls In classNames
hWnd = FindWindow(cls, vbNullString)
Do While hWnd <> 0
lLen = GetWindowText(hWnd, sBuffer, 255)
If lLen > 0 Then
title = Left$(sBuffer, lLen)
If InStr(1, title, "Rev", vbTextCompare) > 0 Then
GetChromeWindowTitle = title
Exit Function
End If
End If
hWnd = FindWindowEx(0, hWnd, cls, vbNullString)
Loop
Next cls
End Function
'=============================================================
' Extract revision letters/numbers following "Rev"
'=============================================================
Private Function ExtractRevFromTitle(ByVal title As String) As String
Dim pos As Long, revPart As String, i As Long
pos = InStr(1, title, "Rev", vbTextCompare)
If pos = 0 Then Exit Function
revPart = Mid$(title, pos + 3)
revPart = Replace(revPart, ":", "")
revPart = Trim$(revPart)
For i = 1 To Len(revPart)
If Mid$(revPart, i, 1) Like "[A-Za-z0-9]" Then
ExtractRevFromTitle = ExtractRevFromTitle & Mid$(revPart, i, 1)
If Len(ExtractRevFromTitle) >= MAX_REV_CHARS Then Exit For
ElseIf Len(ExtractRevFromTitle) > 0 Then
Exit For
End If
Next i
End Function