-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal version 5
95 lines (79 loc) · 3.23 KB
/
final version 5
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
Sub CheckSTEWordsStructuredTechnicalEnglish()
Dim doc As Document
Dim incorrectWord As Variant
Dim replacementWord As String
Dim correctionsDict As Object
Dim wordRange As Range
Dim excelApp As Object, workbook As Object, sheet As Object
Dim row As Long, lastRow As Long
Dim excelFilePath As String
Dim selectedRange As Range
Dim updateCount As Long
' Ensure there is a selection
If Selection.Type = wdNoSelection Then
MsgBox "Please select the text to be checked.", vbExclamation
Exit Sub
End If
' Set the active document and selected range
Set doc = ActiveDocument
Set selectedRange = Selection.Range
' Specify the Excel file path containing corrections
excelFilePath = "C:\path\to\your\correction_file.xlsx" ' Update this path
' Initialize the dictionary
Set correctionsDict = CreateObject("Scripting.Dictionary")
' Open Excel
On Error Resume Next
Set excelApp = CreateObject("Excel.Application")
If excelApp Is Nothing Then
MsgBox "Excel could not be started. Make sure Excel is installed.", vbCritical
Exit Sub
End If
excelApp.Visible = False
Set workbook = excelApp.Workbooks.Open(excelFilePath)
On Error GoTo 0
If workbook Is Nothing Then
MsgBox "The specified Excel file could not be opened. Please check the file path.", vbCritical
excelApp.Quit
Set excelApp = Nothing
Exit Sub
End If
Set sheet = workbook.Sheets(1)
lastRow = sheet.Cells(sheet.Rows.Count, 1).End(-4162).Row ' -4162 is xlUp
' Load corrections into the dictionary
For row = 2 To lastRow
incorrectWord = Trim(LCase(sheet.Cells(row, 1).Value))
replacementWord = Trim(sheet.Cells(row, 2).Value)
If Not correctionsDict.Exists(incorrectWord) Then
correctionsDict.Add incorrectWord, replacementWord
End If
Next row
' Close Excel
workbook.Close False
excelApp.Quit
Set excelApp = Nothing
' Initialize counters
updateCount = 0
' Main loop for grammar check
For Each wordRange In selectedRange.Words
' Process only words that are not underlined
If wordRange.Font.Underline = wdUnderlineNone Then
Dim coreWord As String
coreWord = Trim(LCase(wordRange.Text))
' Check if word is in corrections dictionary and doesn't already have suggestion appended
If correctionsDict.Exists(coreWord) And InStr(wordRange.Text, "(" & correctionsDict(coreWord) & ")") = 0 Then
' Append the replacement in parentheses only if it hasn't already been appended
wordRange.Text = wordRange.Text & " (" & correctionsDict(coreWord) & ")"
' Underline the word to mark it as processed
wordRange.Font.Underline = wdUnderlineSingle
End If
End If
' Increment counter and allow Word to process events
updateCount = updateCount + 1
If updateCount Mod 10 = 0 Then DoEvents
Next wordRange
' Final cleanup
Application.ScreenUpdating = True
Application.StatusBar = "Grammar check completed."
' Display completion message
MsgBox "Grammar check completed.", vbInformation
End Sub