Sub Replace()
Dim xl As New Excel.Application
Dim wb As Workbook
xl.Visible = False
Dim ws As Worksheet
Dim SelectedFileItem As String
Dim FDialog As FileDialog
Set FDialog = Application.FileDialog(msoFileDialogOpen)
Dim lastRow
Dim find_text
Dim replace_text
Dim oCell
Dim r As Range
Dim afile
Dim ExcelWasNotRunning As Boolean
Dim num_chk
Dim bmRange As Range
Set bmRange = ActiveDocument.Bookmarks("autounit").Range
With FDialog
.Title = "Select a file"
.AllowMultiSelect = False
.InitialFileName = "D:\OneDrive - CACTUS\Documents"
.Filters.Clear
.Filters.Add "Excel files", "*.xlsx"
If .Show = -1 Then
SelectedFileItem = .SelectedItems(1)
afile = SelectedFileItem
Else
MsgBox "You cancelled the operation"
Exit Sub
End If
End With
On Error Resume Next
If Err Then
ExcelWasNotRunning = True
Set xl = CreateObject("Excel.Application")
End If
On Error GoTo 0
With xl
Set wb = xl.Workbooks.Open(afile)
Set ws = wb.Sheets("sheet1")
ws.Activate
End With
If ExcelWasNotRunning Then
xl.Quit
End If
lastRow = ws.Cells(ws.Rows.count, "A").End(xlUp).Row
num_chk = "([{.,%,/,-}]" + "[0-9] )"
For oCell = 2 To lastRow
find_text = ws.Range("A" & oCell).Value
replace_text = ws.Range("B" & oCell).Value
find_text = "(" + find_text + ")"
Set r = ActiveDocument.Content
r.Find.ClearFormatting
With r.Find
Options.DefaultHighlightColorIndex = wdGray25
.ClearFormatting
.Replacement.ClearFormatting
.MatchCase = True
.Text = find_text
.MatchWholeWord = True
.Replacement.Text = replace_text
.Replacement.Highlight = True
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
Next oCell
MsgBox " Replacement Completed "
End Sub
For ocell = 2 To lastRow
strBookMarkName = "autounit_" & Format(ocell - 1, "00")
replace_text = ws.Range("B" & ocell).Value
Replacebookmarktext ActiveDocument, strBookMarkName, replace_text
Next ocell
ASKER
lastRow = ws.Cells(ws.Rows.count, "A").End(xlUp).Row
num_chk = "([{.,%,/,-}]" + "[0-9] )"
For oCell = 2 To lastRow
find_text = ws.Range("A" & oCell).Value
replace_text = ws.Range("B" & oCell).Value
find_text = "(" + find_text + ")"
Z = 0
Set r = ActiveDocument.Content
With r.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWholeWord = True
.Text = num_chk + find_text
.Replacement.Text = ""
.Wrap = wdFindStop
.Execute = True
Z = Z + 1
bmName = "autounit" + "_" + Z
Selection.Range.Bookmarks.Add bmName
End With
r.Find.ClearFormatting
With r.Find
Options.DefaultHighlightColorIndex = wdGray25
.ClearFormatting
.Replacement.ClearFormatting
.MatchCase = True
.Text = find_text
.MatchWholeWord = True
.Replacement.Text = replace_text
.Replacement.Highlight = True
.Wrap = wdFindStop
Do While .Execute
If Selection.Bookmarks.count > 0 Then
Selection.MoveLeft unit:=wdCharacter, count:=9, Extend:=wdExtend
Loop
End With
Next oCell
End Sub
ASKER
What do you want to happen in the second Find loop?Bookmark name should be different /increment for every "number" found before the "units". eg: [autounit_1], [autounit_2], etc.
As it stands the first Find loop will add a bookmark for the first instance of the text from the WordsToFind column of the spreadsheetI want to replace for all instances not only 1st instance.
ASKER
ASKER
ASKER
I need to create the bookmarks. I have no idea to use bookmarks.If you have no idea how to use bookmarks, why are you trying to create them, manually or programmatically?
ASKER
ASKER
ASKER
ASKER
For oCell = 2 To lastRow
find_text = ws.Range("A" & oCell).Value
replace_text = ws.Range("B" & oCell).Value
Set wrdRange = ActiveDocument.Content
With wrdRange.Find
.MatchCase = True
.Text = find_text
.MatchWholeWord = True
Do While .Execute()
Set wrdRange2 = wrdRange.Duplicate
wrdRange2.Collapse wdCollapseStart
wrdRange2.MoveStart wdWord, -1
If Not IsNumeric(wrdRange2.Text) Then
wrdRange.Text = replace_text
wrdRange.HighlightColorIndex = wdYellow
End If
Loop
End With
Next oCell
MsgBox " Replacement Completed "
ASKER
it converts 'Antimycin A' to 'Antimycin amperes', which I guess you don't wantYes..
ASKER
2-Fluor-2- desoxy-D-glucoseIn this line "D" gets converted to "diopter".
For oCell = 2 To lastRow
find_text = ws.Range("A" & oCell).Value
replace_text = ws.Range("B" & oCell).Value
Set wrdRange = ActiveDocument.Content
wrdRange.Find.ClearFormatting
With wrdRange.Find
.MatchCase = True
.Text = find_text
.MatchWholeWord = True
Do While .Execute()
'get previous word
Set wrdRange2 = wrdRange.Duplicate
wrdRange2.Collapse wdCollapseStart
wrdRange2.MoveStart wdWord, -1
'test previous word
If IsNumeric(wrdRange2.Text) Then
'do nothing
ElseIf IsJoined(wrdRange) Then
'do nothing
Else
wrdRange.Text = replace_text
wrdRange.HighlightColorIndex = wdYellow
End If
Loop
End With
Next oCell
Extra functionFunction IsJoined(wrdRng As Word.Range) As Boolean
Dim rng As Word.Range
Dim Joiners As String
Joiners = "-&°"
'check previous character
Set rng = wrdRng.Duplicate
rng.Collapse wdCollapseStart
rng.MoveStart wdCharacter, -1
IsJoined = InStr(Joiners, rng.Text)
If IsJoined Then
Exit Function
End If
'check following character
Set rng = wrdRng.Duplicate
rng.Collapse wdCollapseEnd
rng.MoveStart wdCharacter, 1
IsJoined = InStr(Joiners, rng.Text)
End Function
ASKER
ASKER
ASKER
ASKER
ASKER
Yes. I told you about that. We could catch that by checking for the word 'Antimycin', but 'A' is especially vulnerable, since it is also a common word.Okay.
Microsoft Word is a commercial document editing program that is part of the Microsoft Office suite. It features numerous text-editing tools for creating richly formatted documents, along with tools for the use of macros in Word documents. Word's native file formats are denoted either by a .doc or .docx file extension. Plugins permitting the Windows versions of Word to read and write formats it does not natively support, such as the OpenDocument format (ODF) are available. Word can import and display images in common bitmap formats such as JPG and GIF. It can also be used to create and display simple line-art.
TRUSTED BY
Open in new window
You can improve the operation by checking that the bookmark is there, and by replacing it after the text has been added. I use a sub-routine like this:
Open in new window
Not quite sure how you need it in your code