-
Using MS Word Find/Replace from VB6
I am having trouble using the find/replace feature of Word from VB6. I set this up as a macro originally but for other reasons I am now compiling this in a little VB app. It worked fine as a macro but I am guessing that was because Word was already open and the correct document was already active and ready for the find/replace to work. Here is what I have right now and I am lost as to what the problem with my code is, Maybe I just need to make the document the top most window somhow but I dont know how, help please....
Private Sub Command1_Click()
'Open MS Word
Dim MSW As Object
Set MSW = CreateObject("Word.Application")
MSW.Visible = True
'Open Predefined Template and make it active
Set Doc1 = MSW.Documents.Open(FileName:=TemplateChange)
Doc1.Activate
'Find / Replace
MSW.Selection.Find.ClearFormatting
MSW.Selection.Find.Replacement.ClearFormatting
With MSW.Selection.Find
.Text = "[TEST]"
.Replacement.Text = " TESTING "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
MSW.Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Any sugestions are welcome
-
Update:
I have no idea why but, for some reason that code is working now, go figure...
-
Option Explicit
Dim spControl As Control
Private Sub Spell_Check(ByRef Cntrl As Control)
Dim R As Word.Range
Dim rWd As Word.Range
Dim Wd As Word.Document
Dim WrdApp As Word.Application
Dim Sglist As Word.SpellingSuggestions
Dim Sugg As Word.SpellingSuggestion
Dim Rs As VbMsgBoxResult
Set WrdApp = New Word.Application
Set Wd = WrdApp.Documents.Add
Set R = Wd.Range
R.InsertAfter Trim(Cntrl.Text)
For Each rWd In WrdApp.ActiveDocument.Words
Set Sglist = rWd.GetSpellingSuggestions
If Sglist.Count > 0 Then
For Each Sugg In Sglist
Rs = MsgBox("Replace " & rWd.Text & " With " & Sugg.Name & "?", vbYesNo + vbQuestion, Sugg.Name)
If Rs = vbYes Then
rWd.Text = Sugg.Name & " "
Exit For
End If
Next Sugg
End If
Next rWd
Cntrl.Text = Trim(Mid(R.Text, 1, Len(R.Text) - 1)) 'Trim Off crlf
Wd.Close False
WrdApp.Quit False
Set WrdApp = Nothing
End Sub
this will spell check then do the replace
Private Sub CmdSpAll_Click()
On Error Resume Next
For Each spControl In Me
spControl.BackColor = vbYellow
Spell_Check spControl
spControl.BackColor = vbWhite
Next
MsgBox "Spell Check Completed"
End Sub
Private Sub Form_Load()
Text1.Text = "Thiss is a spell checker in its most basik form. More could be done as in grammer, and more dictionaries."
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke
|
Bookmarks