|
-
Oct 21st, 2006, 01:27 PM
#1
Thread Starter
Hyperactive Member
Find Replace text inWord from Vb.net
Searching this group i found interesting solutions and couple of problems.
I need to search from VB.net, text in Word and replace that text with replacement text. I need to replace 500+ words!
-VB.net 2005 express
-word 2003
-PIA reference added
VB Code:
Imports Word = Microsoft.Office.Interop.Word
Public Function DoFindReplace()
Dim oWord As New Word.Application
Dim oDoc As Word.Document
Dim sFind As String
Dim sReplace As String
oDoc = oWord.ActiveDocument.Content
Dim rngRange As Word.Range = ActiveDocument.Range 'Problem: Not Declared
With rngRange.Find
.ClearFormatting()
.Replacement.ClearFormatting()
.Text = sFind
.Replacement.Text = sReplace
.Forward = True
.Wrap = Word.WdFindWrap.wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
End Function
Private Sub subsubTran_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles subsubTran.Click
Dim oWord As New Word.Application
Dim oDoc As Word.Document
Dim path As String = Me.TextBox1.Text
oWord.Visible = False
oDoc = oWord.Documents.Open(path)
Call DoFindReplace("System", "Computer", ActiveDocument.Range)'Problem: Not Declared
Call DoFindReplace("test", "tempo", ActiveDocument.Range)'Problem: Not Declared
Call DoFindReplace("car", "bus", ActiveDocument.Range)'Problem: Not Declared
'etc. etc.
oDoc.SaveAs(path)
oDoc.Close()
oDoc = Nothing
oWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
oWord.Quit()
oWord = Nothing
End Sub
Till now the only problem is "ActiveDocument" not declared, but when I play with it, I gort more errors...
Any help appriciated. And yes, this is my first encounter with VB.net or similar. Sorry for bad english.
Zeljko
-
Oct 21st, 2006, 01:43 PM
#2
Addicted Member
Re: Find Replace text inWord from Vb.net
Hey Zeljko,
I'm not familiar with VB.net either...but I believe you could just use:
oWord.ActiveDocument.Range
Hope this helps
-
Oct 21st, 2006, 01:56 PM
#3
Thread Starter
Hyperactive Member
Re: Find Replace text inWord from Vb.net
yes, I tried that before. That solves first Error
VB Code:
' old line
' Dim rngRange As Word.Range = ActiveDocument.Range
' new line
Dim rngRange As Word.Range = oWord.ActiveDocument.Range
But then problem transfers to:
VB Code:
.Text = sFind 'now Null, before ok
.Replacement.Text = sReplace 'now Null, before ok
end like before:
VB Code:
Call DoFindReplace("System", "Computer", ActiveDocument.Range)
Call DoFindReplace("test", "tempo", ActiveDocument.Range)
Call DoFindReplace("car", "bus", ActiveDocument.Range)
I'm working on that almost ten days and still nothing.
More sugestions?
-
Oct 21st, 2006, 02:12 PM
#4
Re: Find Replace text inWord from Vb.net
I would recommend not using the "Active" property of the WOM as it can produce undesired effects if a user accidentally clicks or activates a different document. 
oDoc = oWord.ActiveDocument.Content
'To
oDoc = oWord.Documents("Document1").Content ' Or whatever your document name is 
What are the current errors left?
Have you looked at my Office FAQ?
http://vbforums.com/showthread.php?t=358585
How to automate an office app with vb.net
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 21st, 2006, 02:37 PM
#5
Thread Starter
Hyperactive Member
Re: Find Replace text inWord from Vb.net
"Active" to "Documenth(path). I agree with accidental click. Done.
Problems are still there 
1. Dim rngRange As Word.Range = ActiveDocument.Range 'function
---
1. Call DoFindReplace("System", "Computer", ActiveDocument.Range) 'sub
2. Call DoFindReplace("System", "Computer", ActiveDocument.Range) 'sub
3. etc etc
Notice that if I use "oDoc.Range" (instead of ActiveDocument.Range)then error becomes:
.Text = sFind 'error: null value
.Replacement.Text = sReplace 'error: null value
-
Oct 21st, 2006, 04:05 PM
#6
Re: Find Replace text inWord from Vb.net
#1
VB Code:
Dim rngRange As Word.Range = oWord.Documents("Document1").Range
#2 and #3
VB Code:
Call DoFindReplace("System", "Computer", rngRange) 'sub
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 22nd, 2006, 01:48 AM
#7
Thread Starter
Hyperactive Member
Re: Find Replace text inWord from Vb.net
I'm still having errors like before. Maybe its my bad english and i cant explain it to you very well, but I'l try:
First, thanks all for trying to help me.
Second, as somebody on group sugested i tried with record macro in word (year ago) and that worked with help on google , so here is copy of that macro (with 2 subs: one with calls, one with conditions) that I'm trying to translate to VB.NET:
VB Code:
'[B]VBA Word[/B]
'SUB for all replacement in Document
Sub DoFindReplace(FindText As String, ReplaceText As String)
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = FindText
.Replacement.Text = ReplaceText
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
VB Code:
'[B]VBA Word[/B]
'SUB with words and calls
Sub Translate_Words()
Application.ScreenUpdating = False
Selection.WholeStory
Call DoFindReplace("^t^t", "^t")
Call DoFindReplace("^p^p", "^p")
Call DoFindReplace("bus", "car")
'etc...
End Sub
VBA code works perfect in Word!
------
For now I have this 2 subs in VB.NET 2005 express (function with conditions, sub with calls):
VB Code:
'[B]VB.NET[/B]
'FUNCTION for all replacement in Document
Public Function DoFindReplace()
Dim oWord As New Word.Application
Dim oDoc As Word.Document
Dim path As String
Dim sFind As String
Dim sReplace As String
oDoc = oWord.Documents(path)
Dim rngRange As Word.Range = oWord.Documents(path).Range
With rngRange.Find
.ClearFormatting()
.Replacement.ClearFormatting()
.Text = sFind
.Replacement.Text = sReplace
.Forward = True
.Wrap = Word.WdFindWrap.wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
End Function
VB Code:
'[B]VB.NET[/B]
'SUB with words and calls
Private Sub subsubTran_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles subsubTran.Click
Dim oWord As New Word.Application
Dim oDoc As Word.Document
Dim path As String = Me.TextBox1.Text
oWord.Visible = False
oDoc = oWord.Documents.Open(path)
Call DoFindReplace("System", "Computer", rngRange)
Call DoFindReplace("test", "tempo", rngRange)
Call DoFindReplace("car", "bus", rngRange)
'etc. etc.
oDoc.SaveAs(path)
oDoc.Close()
oDoc = Nothing
oWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
oWord.Quit()
oWord = Nothing
End Sub
On My Form I have Text Box "TextBox1" with path to Word document. Button "subsubTran" to open document and run this sub (and call this function).
Opening document and similar worked.
Problems:
In Function:
Null reference on ...oWord.Documents(path) & ...sFind & ...sReplace
In Sub:
Call DoFind...., rngRange) not declared
If I declare rngRange in Sub then I get error in call DoFindReplace("here is error") line:
To many arguments to Public Function DoFindReplace()as object
Anyone?
-
Oct 25th, 2006, 01:00 PM
#8
Thread Starter
Hyperactive Member
Re: Find Replace text inWord from Vb.net
Someone! Anyone! Please, help!
-
Oct 25th, 2006, 02:40 PM
#9
Re: Find Replace text inWord from Vb.net
Couple of things...
Your creating two instances of the word application when you can do with just one and one instance of the open document.
Your calling your sub and passing parameters but the sub does not have any parameters defined in the subs signature.
Call DoFindReplace("System", "Computer", rngRange)
Public Function DoFindReplace() 'No arguments defined in the parenthesis.
Define rngRange.
You can pass the word application and open document object variables to the DoFindReplace function if you add these two arguments. Then there will be no need to open a second instance of word.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
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
|
Click Here to Expand Forum to Full Width
|