NVIT
asked on
Erasing whole line via VBScript doesn't work
Hi,
I'm trying to erase a line of text, including the end-of-paragraph mark. When I manually do it in Word, it works fine - in vbscript, it doesn't. It enters the .Execute statement and each line w/o error. But nothing happens to the text.
In the debugger, the find and replace variables look fine.
strFindText = "erase this paragraph^p"
strReplacementText = ""
Any ideas?
I'm running the script in XP SP3.
Chris
I'm trying to erase a line of text, including the end-of-paragraph mark. When I manually do it in Word, it works fine - in vbscript, it doesn't. It enters the .Execute statement and each line w/o error. But nothing happens to the text.
In the debugger, the find and replace variables look fine.
strFindText = "erase this paragraph^p"
strReplacementText = ""
Any ideas?
I'm running the script in XP SP3.
Const TIMEOUT_1_SEC = 1
Const POPUP_TITLE = "Find and Replace"
Dim objShell 'As IWshRuntimeLibrary.WshShell
Dim objDoc 'As Word.Document
Dim objWord 'As Word.Application
Dim objRangeFind 'As Word.Find
Set objShell = CreateObject("WScript.Shell")
Sub FindNRepl
sFilename = "C:\Local\test2.doc"
Set objDoc = objWord.Documents.Open(sFilename)
strFindText = "erase this paragraph^p"
strReplacementText = ""
Do While Not IsEmpty(strFindText)
Set objRangeFind = objDoc.Range.Find
objRangeFind.ClearFormatting
objRangeFind.Replacement.ClearFormatting
With objRangeFind
.Text = strFindText
.Replacement.Text = strReplacementText
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Replacement.Font.Color = vbMagenta
If .Execute Then
objShell.Popup "Replacing text...", TIMEOUT_1_SEC, POPUP_TITLE, vbInformation + vbOKOnly
objRangeFind.Execute, , , , , , , , , ,wdReplaceAll
objShell.Popup "Replacement done.", TIMEOUT_1_SEC, POPUP_TITLE, vbInformation + vbOKOnly
If objDoc.Saved = False Then
objDoc.Save
End If
Else
objShell.Popup "Not found...", TIMEOUT_1_SEC, POPUP_TITLE, vbInformation + vbOKOnly
End If
End With
' Get next find and replace strings
'nRecFind = nRecFind + 1
'strFindText = aStrs(nRecFind,0)
'strReplacementText = aStrs(nRecFind,1)
Loop 'While Not IsEmpty(strFindText)
End Sub
Chris
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Not too sure where you are stuck, but here is an (untested) suggestion.
After a successful find, the input Range object is set to the the found range, so here you can simply delete the first paragraph. Note that I use the VBA editor to create this, so I might have left some VBA-only syntax.
After a successful find, the input Range object is set to the the found range, so here you can simply delete the first paragraph. Note that I use the VBA editor to create this, so I might have left some VBA-only syntax.
'Sub FR()
Const TIMEOUT_1_SEC = 1
Const POPUP_TITLE = "Find and Replace"
Dim objShell 'As IWshRuntimeLibrary.WshShell
Dim objDoc 'As Word.Document
Dim objWord 'As Word.Application
Dim objRangeFind 'As Word.Find
Dim rng ' As Range
Set objShell = CreateObject("WScript.Shell")
FindNRepl
Sub FindNRepl()
sFilename = "C:\Local\test2.doc"
Set objWord = CreateObject("Word.Application")
objWord.Visible = True 'make sure that you can see instances of Application
Set objDoc = objWord.Documents.Open(sFilename)
strFindText = "erase this paragraph^p"
strReplacementText = ""
Do While Not IsEmpty(strFindText)
Set rng = objDoc.Range
Set objRangeFind = rng.Find
With objRangeFind
.Text = strFindText
.Replacement.Text = strReplacementText
If .Execute Then
rng.Paragraphs.First.Delete
objShell.Popup "Replacing text...", TIMEOUT_1_SEC, POPUP_TITLE, vbInformation + vbOKOnly
objRangeFind.Execute , , , , , , , , , , wdReplaceAll
objShell.Popup "Replacement done.", TIMEOUT_1_SEC, POPUP_TITLE, vbInformation + vbOKOnly
If objDoc.Saved = False Then
objDoc.Save
End If
Else
objShell.Popup "Not found...", TIMEOUT_1_SEC, POPUP_TITLE, vbInformation + vbOKOnly
End If
End With
strFindText = emptyvar
Loop 'While Not IsEmpty(strFindText)
CloseWordAppAndQuit
'End Sub
ASKER
Hi Graham,
I haven't tried your solution yet.
Mine is basically a find and replace tool which works fine until now I have to erase the entire line, i.e. paragraph. Some documents have single-line text such as titles. If I just use "erase this paragraph", it leaves the blank line. So, I added the ^p at the end. It works fine manually in Word but not in this macro.
Again, I haven't had a chance to try your solution yet. At face value, it seems to ALWAYS delete the text. Would it still work with general find and replace?
I haven't tried your solution yet.
Mine is basically a find and replace tool which works fine until now I have to erase the entire line, i.e. paragraph. Some documents have single-line text such as titles. If I just use "erase this paragraph", it leaves the blank line. So, I added the ^p at the end. It works fine manually in Word but not in this macro.
Again, I haven't had a chance to try your solution yet. At face value, it seems to ALWAYS delete the text. Would it still work with general find and replace?
ASKER
In the Find dialog, I enter: erase this paragraph^p
In the Replace dialog, I enter: ^p
This results in a blank line, as expected.
Rewind and try this...
In the Find dialog, I enter: erase this paragraph^p
In the Replace dialog, I don't enter any text. Just pick OK.
It doesn't erase the text and ^p mark, as I'd expect. As I said in my OP, it enters the statement but nothing happens.
Rewind and try this...
In the Find dialog, I enter: erase this paragraph^p
In the Replace dialog, I enter a single SPACE character.
This results erasing the line and ^p mark. But, the next line is now prefixed with a space character. Although it's not obvious to the eye, this is not the proper way to do it.
Any suggestions?
In the Replace dialog, I enter: ^p
This results in a blank line, as expected.
Rewind and try this...
In the Find dialog, I enter: erase this paragraph^p
In the Replace dialog, I don't enter any text. Just pick OK.
It doesn't erase the text and ^p mark, as I'd expect. As I said in my OP, it enters the statement but nothing happens.
Rewind and try this...
In the Find dialog, I enter: erase this paragraph^p
In the Replace dialog, I enter a single SPACE character.
This results erasing the line and ^p mark. But, the next line is now prefixed with a space character. Although it's not obvious to the eye, this is not the proper way to do it.
Any suggestions?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Here's the final code. It now lets me replace or delete text. In the end, I didn't use Graham's "rng.Paragraphs.First.Rang e.Delete". Instead, it was a matter of knowing more how the Find object works and revising accordingly.
ASKER
BTW, how can I see the result of the "objRangeFind.Execute, , , , , , , , , ,wdReplaceAll" line?
Open in new window