Option Explicit
Public referenceDocument As Document
Public lookupDocument As Document
Public activeDictionary As Object
Option Explicit
Sub createAcronymTableFile()
Dim newDocumentName As String, existingDocumentName As String, docNameWithoutExtension As String
Set referenceDocument = ActiveDocument
newDocumentName = "reference_" & Left(ActiveDocument.Name, InStr(1, ActiveDocument.Name, ".") - 1)
If Right(newDocumentName, 1) = Chr(46) Then newDocumentName = Left(newDocumentName, Len(newDocumentName) - 1)
existingDocumentName = ""
On Error Resume Next
existingDocumentName = Dir(Rep_Documents() & "" & newDocumentName & ".docx")
Set lookupDocument = Documents.Open(Rep_Documents() & "" & newDocumentName & ".docx")
On Error GoTo 0
If existingDocumentName = "" Then
Call copyTableToNewDoc
On Error GoTo ErrHandler
lookupDocument.SaveAs (Rep_Documents() & "" & newDocumentName & ".docx")
End If
referenceDocument.Activate
ErrHandler:
End Sub
Sub copyTableToNewDoc()
Dim rng As Range, lookupRng As Range
Dim currentTableIndex As Index
Dim tbl As Table
Dim endOfDocument As Integer
If Not Selection.Information(wdWithInTable) Then
Err.Raise 5, , "You need to create the reference table first!" & vbLf _
& "Please put cursor inside the table of acronymns or abbreviations" & vbLf _
& "and run this macro again!"
Exit Sub
End If
Set lookupDocument = Documents.Add
referenceDocument.Activate
Selection.Tables(1).Range.Select
Selection.Copy
Set rng = lookupDocument.Range
rng.Collapse Direction:=wdCollapseEnd
rng.PasteSpecial DataType:=wdPasteRTF
lookupDocument.Tables(1).Rows(1).Delete
rng.Collapse Direction:=wdCollapseEnd
rng.Text = "== Missing Definitions =="
End Sub
Function acronymDescription(txt As String, tbl As Table) As String
Dim i As Integer, totalTableRows As Integer
Dim key As String
Dim value As String
Dim dict As Object
Dim rng As Range
If activeDictionary Is Nothing Then
totalTableRows = tbl.Rows.Count
Set dict = CreateObject("Scripting.Dictionary")
For i = 1 To totalTableRows
key = fcnGetCellText(tbl.Cell(i, 1))
value = fcnGetCellText(tbl.Cell(i, 2))
If Not dict.Exists(key) Then
dict.Add key, value
End If
If key = txt Then GoTo StopBuildingDictionary
Next i
StopBuildingDictionary:
If Not dict.Exists(txt) Then
acronymDescription = "Value not found"
Set rng = lookupDocument.Range
rng.Collapse Direction:=wdCollapseEnd
rng.Text = vbCr & txt
Else
acronymDescription = dict(txt)
End If
Set dict = activeDictionary
Else
If Not dict.Exists(txt) Then
acronymDescription = "Value not found"
Set rng = lookupDocument.Range
rng.Collapse Direction:=wdCollapseEnd
rng.Text = vbCr & txt
Else
acronymDescription = activeDictionary(txt)
End If
End If
End Function
Function fcnGetCellText(ByRef oCell As Word.Cell) As String
fcnGetCellText = Replace(oCell.Range.Text, ChrW(13) & ChrW(7), vbNullString)
End Function
Sub createNote()
Dim timeStamp As String, bookmarkName As String, screentipText As String, key As String
Dim rng As Range
Dim answer As Integer
Dim tbl As Table
Application.ScreenUpdating = False
timeStamp = Format(Now(), "yyyyMMddHHmmss")
Set lookupDocument = Nothing
Call createAcronymTableFile
Select Case Right(fcnGetCellText(lookupDocument.Tables(1).Cell(1, 1)), 1)
Case Is = Chr(32)
If Right(Selection.Text, 1) = Chr(32) Then
key = Selection.Text
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
Else
key = Selection.Text & Chr(32)
End If
Case Else
If Right(Selection.Text, 1) = Chr(32) Then
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
key = Selection.Text
Else
key = Selection.Text
End If
End Select
Set rng = Selection.Range
If Selection.Information(wdWithInTable) Then
If Selection.Cells.Count > 1 Then Exit Sub
End If
screentipText = acronymDescription(key, lookupDocument.Tables(1))
If screentipText = "Value not found" Then
MsgBox screentipText
Exit Sub
End If
bookmarkName = rng.Text & "_" & timeStamp
If Not InStr(1, rng.Text, "-", vbTextCompare) = 0 Then
bookmarkName = Replace(rng.Text, "-", "", vbTextCompare) & "_" & timeStamp
End If
If Not InStr(1, Selection.Text, "&", vbTextCompare) = 0 Then
bookmarkName = Replace(rng.Text, "&", "", vbTextCompare) & "_" & timeStamp
End If
With ActiveDocument.Bookmarks
.Add Range:=rng, Name:=bookmarkName
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
rng.Select
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
SubAddress:=bookmarkName, ScreenTip:=screentipText, TextToDisplay:= _
Selection.Text
rng.Select
With Selection.Font
.ColorIndex = wdAuto
.Underline = wdUnderlineNone
End With
MsgBox screentipText
Set lookupDocument = Nothing
Application.ScreenUpdating = True
CreateNoteErrHandler:
End Sub
Sub addScreenTip(doc As Document, rng As Range, bmark As String, tip As String)
With doc.Bookmarks
.Add Range:=Selection.Range, Name:=bmark
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
End Sub
Function ReplaceInTable(textToFind As String) As String
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:=textToFind)
If oRng.Information(wdWithInTable) Then
oRng.Select
Selection.Cells(1).Next.Select
Set oRng = Selection.Range
ReplaceInTable = CleanTrim(oRng.Text, True)
GoTo lbl_Exit
End If
Loop
End With
lbl_Exit:
Exit Function
End Function
Function CleanTrim(ByVal S As String, Optional ConvertNonBreakingSpace As Boolean = True) As String
Dim X As Long, CodesToClean As Variant
CodesToClean = Array(0, 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, 127, 129, 141, 143, 144, 157)
If ConvertNonBreakingSpace Then S = Replace(S, Chr(160), " ")
For X = LBound(CodesToClean) To UBound(CodesToClean)
If InStr(S, Chr(CodesToClean(X))) Then S = Replace(S, Chr(CodesToClean(X)), "")
Next
CleanTrim = S
End Function
Private Type SHITEMID
cb As Long
abID As Byte
End Type
Private Type ITEMIDLIST
mkid As SHITEMID
End Type
Private Const CSIDL_PERSONAL As Long = &H5
Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _
(ByVal hwndOwner As Long, ByVal nFolder As Long, _
pidl As ITEMIDLIST) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, ByVal pszPath As String) As Long
Public Function Rep_Documents() As String
Dim lRet As Long, IDL As ITEMIDLIST, sPath As String
lRet = SHGetSpecialFolderLocation(100&, CSIDL_PERSONAL, IDL)
If lRet = 0 Then
sPath = String$(512, Chr$(0))
lRet = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath)
Rep_Documents = Left$(sPath, InStr(sPath, Chr$(0)) - 1)
Else
Rep_Documents = vbNullString
End If
End Function
|