Originally Posted by
TonyJollans
There is a significant overhead in using this when you do not want to interact with the user.
By 'overhead' do you mean that this will run slowly, as this is what I am finding. If I'm trying to replace multiple pieces of text, it takes about 1 second per replacement, so if I have 18 keywords to replace, it takes 18 seconds.
The code here:
Dim iCount As Integer
For iCount = 1 To 18
wrdApp.Selection.Find.Text = findData(iCount)
wrdApp.Selection.Find.Replacement.Text = replData(iCount)
wrdApp.Selection.Find.Forward = True
wrdApp.Selection.Find.Wrap = wdFindContinue
wrdApp.Selection.Find.Format = False
wrdApp.Selection.Find.MatchCase = True
wrdApp.Selection.Find.MatchWholeWord = False
wrdApp.Selection.Find.MatchWildcards = False
wrdApp.Selection.Find.MatchSoundsLike = False
wrdApp.Selection.Find.MatchAllWordForms = False
wrdApp.Selection.Find.Execute Replace:=wdReplaceAll
Next iCount
Looks in the array findData{} for the search terms and replData{} for the text to replace it with.
This code works fine, but is rather slow.
I tried changing the code to be:
wrdDoc.Select
wrdDoc.Range = Selection.Range
Dim iCount As Integer
For iCount = 1 To 18
wrdDoc.Range.Find.Text = findData(iCount)
wrdDoc.Range.Find.Replacement.Text = replData(iCount)
wrdDoc.Range.Find.Forward = True
wrdDoc.Range.Find.Wrap = wdFindContinue
wrdDoc.Range.Find.Format = False
wrdDoc.Range.Find.MatchCase = True
wrdDoc.Range.Find.MatchWholeWord = False
wrdDoc.Range.Find.MatchWildcards = False
wrdDoc.Range.Find.MatchSoundsLike = False
wrdDoc.Range.Find.MatchAllWordForms = False
wrdDoc.Range.Find.Execute Replace:=wdReplaceAll
Next iCount
However, this doesn't actually change anything. At first I thought that my range wasn't set as the whole document, so I added "wrdDoc.Select
wrdDoc.Range = Selection.Range", hoping that this worked.
In essence, it is slow to replace text and I can't seem to implementwrdDoc.Range.Find correctly.
The interesting thing is that when using the Selection.Find method, when the text is being replaced slowly, if I take focus off the Word application, even by clicking the windows Start button, the text is suddenly replaced quickly.
Rather odd.
EDIT: In relation to the original question, why do I have two instances of Word open? I don't. I only tell my VBA code to open an instance so that I have a handle to the document and application I want. However, say the user is writing a letter to a friend (completely unrelated to the database), and then executes this script; previously, it would edit the first document open, in this case the letter, which I don't want it to, I want it to edit the document I've just opened.
Hopefully that clears up why I have two 'instances' of word open.