kad
08-06-2014, 07:17 AM
Okay, so a while back I'd been asked to find a way to pull all of the trademarked words used in a document into another document. The purpose of this is to use that list of trademarked words to write an Appendix for trademarks.
This is the macro we've been using:
Sub FindTrademarks()'
' FindTrademarks Macro
'
'
Dim oDoc_Source As Document
Dim oDoc_Target As Document
Dim oRange As Range
Dim n As Long
Dim strAllFound As String
Application.ScreenUpdating = False
Set oDoc_Source = ActiveDocument
Set oDoc_Target = Documents.Add
With oDoc_Source
Set oRange = .Range
With oRange.Find
.Text = Chr(174)
While .Execute
oRange.MoveStart wdWord, -1
oDoc_Target.Range.InsertAfter oRange & vbCr
oRange.Collapse wdCollapseEnd
Wend
End With
End With
End Sub
And it works really well. The problem though, is that this macro assumes that we've remembered to put a trademark symbol (R) by the relevant trademarks. Which isn't always the case, we all make mistakes.
What I'd like is to be able to compare the document to an excel spreadsheet list of all the trademarked words our company uses to create the new doc with the list of words.
I have a macro that finds and replaces a list of words from an excel spreadsheet:
Sub FindReplace()'
' FindReplace Macro
'
Const strXLFile = "C:\Users\kate.dupuis\Desktop\Templates\Replacements.xls"
Dim xlApp As Object
Dim xlWbk As Object
Dim xlWsh As Object
Dim blnStart As Boolean
Dim r As Long
Dim m As Long
On Error Resume Next
' Get or start Excel
Set xlApp = GetObject(, "Excel.Application")
If xlApp Is Nothing Then
Set xlApp = CreateObject("Excel.Application")
If xlApp Is Nothing Then
MsgBox "Can't start Excel.", vbExclamation
Exit Sub
End If
blnStart = True
End If
On Error GoTo ErrHandler
Application.ScreenUpdating = False
' Open workbook
Set xlWbk = xlApp.Workbooks.Open(strXLFile)
' Reference to first worksheet
Set xlWsh = xlWbk.Worksheets(1)
' Get last used row
m = xlWsh.Cells(xlWsh.Rows.Count, 1).End(-4162).Row
With ActiveDocument.Content.Find
' Initialize find/replace settings
.ClearFormatting
.Replacement.ClearFormatting
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
' Loop through rows
For r = 1 To m
' Get text to find
.Text = xlWsh.Cells(r, 1)
' And replacement
.Replacement.Text = xlWsh.Cells(r, 2)
' Replace all
.Execute Replace:=wdReplaceAll
Next r
End With
ExitHandler:
' Clean up
On Error Resume Next
Set xlWsh = Nothing
xlWbk.Close savechanges:=False
Set xlWbk = Nothing
If blnStart Then
xlApp.Quit
End If
Set xlApp = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
' Inform user
MsgBox Err.Description, vbExclamation
' And go to cleanup section
Resume ExitHandler
End Sub
But I can't make them mush.
Thanks,
kad
This is the macro we've been using:
Sub FindTrademarks()'
' FindTrademarks Macro
'
'
Dim oDoc_Source As Document
Dim oDoc_Target As Document
Dim oRange As Range
Dim n As Long
Dim strAllFound As String
Application.ScreenUpdating = False
Set oDoc_Source = ActiveDocument
Set oDoc_Target = Documents.Add
With oDoc_Source
Set oRange = .Range
With oRange.Find
.Text = Chr(174)
While .Execute
oRange.MoveStart wdWord, -1
oDoc_Target.Range.InsertAfter oRange & vbCr
oRange.Collapse wdCollapseEnd
Wend
End With
End With
End Sub
And it works really well. The problem though, is that this macro assumes that we've remembered to put a trademark symbol (R) by the relevant trademarks. Which isn't always the case, we all make mistakes.
What I'd like is to be able to compare the document to an excel spreadsheet list of all the trademarked words our company uses to create the new doc with the list of words.
I have a macro that finds and replaces a list of words from an excel spreadsheet:
Sub FindReplace()'
' FindReplace Macro
'
Const strXLFile = "C:\Users\kate.dupuis\Desktop\Templates\Replacements.xls"
Dim xlApp As Object
Dim xlWbk As Object
Dim xlWsh As Object
Dim blnStart As Boolean
Dim r As Long
Dim m As Long
On Error Resume Next
' Get or start Excel
Set xlApp = GetObject(, "Excel.Application")
If xlApp Is Nothing Then
Set xlApp = CreateObject("Excel.Application")
If xlApp Is Nothing Then
MsgBox "Can't start Excel.", vbExclamation
Exit Sub
End If
blnStart = True
End If
On Error GoTo ErrHandler
Application.ScreenUpdating = False
' Open workbook
Set xlWbk = xlApp.Workbooks.Open(strXLFile)
' Reference to first worksheet
Set xlWsh = xlWbk.Worksheets(1)
' Get last used row
m = xlWsh.Cells(xlWsh.Rows.Count, 1).End(-4162).Row
With ActiveDocument.Content.Find
' Initialize find/replace settings
.ClearFormatting
.Replacement.ClearFormatting
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
' Loop through rows
For r = 1 To m
' Get text to find
.Text = xlWsh.Cells(r, 1)
' And replacement
.Replacement.Text = xlWsh.Cells(r, 2)
' Replace all
.Execute Replace:=wdReplaceAll
Next r
End With
ExitHandler:
' Clean up
On Error Resume Next
Set xlWsh = Nothing
xlWbk.Close savechanges:=False
Set xlWbk = Nothing
If blnStart Then
xlApp.Quit
End If
Set xlApp = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
' Inform user
MsgBox Err.Description, vbExclamation
' And go to cleanup section
Resume ExitHandler
End Sub
But I can't make them mush.
Thanks,
kad