Results 1 to 9 of 9

Thread: Find Replace text inWord from Vb.net

  1. #1

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Question 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:
    1. Imports Word = Microsoft.Office.Interop.Word
    2.  
    3. Public Function DoFindReplace()
    4.         Dim oWord As New Word.Application
    5.         Dim oDoc As Word.Document
    6.         Dim sFind As String
    7.         Dim sReplace As String
    8.  
    9.         oDoc = oWord.ActiveDocument.Content
    10.         Dim rngRange As Word.Range = ActiveDocument.Range 'Problem: Not Declared
    11.  
    12.         With rngRange.Find
    13.             .ClearFormatting()
    14.             .Replacement.ClearFormatting()
    15.             .Text = sFind
    16.             .Replacement.Text = sReplace
    17.             .Forward = True
    18.             .Wrap = Word.WdFindWrap.wdFindContinue
    19.             .Format = False
    20.             .MatchCase = True
    21.             .MatchWholeWord = True
    22.             .MatchWildcards = False
    23.             .MatchSoundsLike = False
    24.             .MatchAllWordForms = False
    25.             .Execute(Replace:=Word.WdReplace.wdReplaceAll)
    26.         End With
    27.     End Function
    28.  
    29.     Private Sub subsubTran_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles subsubTran.Click
    30.         Dim oWord As New Word.Application
    31.         Dim oDoc As Word.Document
    32.         Dim path As String = Me.TextBox1.Text
    33.         oWord.Visible = False
    34.         oDoc = oWord.Documents.Open(path)
    35.  
    36.         Call DoFindReplace("System", "Computer", ActiveDocument.Range)'Problem: Not Declared
    37.         Call DoFindReplace("test", "tempo", ActiveDocument.Range)'Problem: Not Declared
    38.         Call DoFindReplace("car", "bus", ActiveDocument.Range)'Problem: Not Declared
    39.  
    40.         'etc. etc.
    41.  
    42.         oDoc.SaveAs(path)
    43.         oDoc.Close()
    44.         oDoc = Nothing
    45.         oWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
    46.         oWord.Quit()
    47.         oWord = Nothing
    48.     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

  2. #2
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    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




    If you find any of my posts of good help, please rate it

  3. #3

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Find Replace text inWord from Vb.net

    yes, I tried that before. That solves first Error
    VB Code:
    1. ' old line
    2. ' Dim rngRange As Word.Range = ActiveDocument.Range
    3. ' new line        
    4. Dim rngRange As Word.Range = oWord.ActiveDocument.Range
    But then problem transfers to:
    VB Code:
    1. .Text = sFind 'now Null, before ok
    2. .Replacement.Text = sReplace 'now Null, before ok
    end like before:
    VB Code:
    1. Call DoFindReplace("System", "Computer", ActiveDocument.Range)
    2. Call DoFindReplace("test", "tempo", ActiveDocument.Range)
    3. Call DoFindReplace("car", "bus", ActiveDocument.Range)

    I'm working on that almost ten days and still nothing.
    More sugestions?

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  5. #5

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    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

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Find Replace text inWord from Vb.net

    #1
    VB Code:
    1. Dim rngRange As Word.Range = oWord.Documents("Document1").Range
    #2 and #3
    VB Code:
    1. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Angry 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:
    1. '[B]VBA Word[/B]
    2. 'SUB for all replacement in Document
    3. Sub DoFindReplace(FindText As String, ReplaceText As String)
    4. With Selection.Find
    5.     .ClearFormatting
    6.     .Replacement.ClearFormatting
    7.     .Text = FindText
    8.     .Replacement.Text = ReplaceText
    9.     .Forward = True
    10.     .Wrap = wdFindContinue
    11.     .Format = False
    12.     .MatchCase = True
    13.     .MatchWholeWord = True
    14.     .MatchWildcards = False
    15.     .MatchSoundsLike = False
    16.     .MatchAllWordForms = False
    17.     .Execute Replace:=wdReplaceAll
    18. End With
    19. End Sub

    VB Code:
    1. '[B]VBA Word[/B]
    2. 'SUB with words and calls
    3. Sub Translate_Words()
    4. Application.ScreenUpdating = False
    5. Selection.WholeStory
    6.   Call DoFindReplace("^t^t", "^t")
    7.   Call DoFindReplace("^p^p", "^p")
    8.   Call DoFindReplace("bus", "car")
    9.   'etc...
    10. 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:
    1. '[B]VB.NET[/B]
    2. 'FUNCTION for all replacement in Document
    3. Public Function DoFindReplace()
    4.         Dim oWord As New Word.Application
    5.         Dim oDoc As Word.Document
    6.         Dim path As String
    7.         Dim sFind As String
    8.         Dim sReplace As String
    9.  
    10.         oDoc = oWord.Documents(path)
    11.  
    12.         Dim rngRange As Word.Range = oWord.Documents(path).Range
    13.  
    14.         With rngRange.Find
    15.             .ClearFormatting()
    16.             .Replacement.ClearFormatting()
    17.             .Text = sFind
    18.             .Replacement.Text = sReplace
    19.             .Forward = True
    20.             .Wrap = Word.WdFindWrap.wdFindContinue
    21.             .Format = False
    22.             .MatchCase = True
    23.             .MatchWholeWord = True
    24.             .MatchWildcards = False
    25.             .MatchSoundsLike = False
    26.             .MatchAllWordForms = False
    27.             .Execute(Replace:=Word.WdReplace.wdReplaceAll)
    28.         End With
    29. End Function

    VB Code:
    1. '[B]VB.NET[/B]
    2. 'SUB with words and calls
    3. Private Sub subsubTran_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles subsubTran.Click
    4.         Dim oWord As New Word.Application
    5.         Dim oDoc As Word.Document
    6.         Dim path As String = Me.TextBox1.Text
    7.  
    8.         oWord.Visible = False
    9.         oDoc = oWord.Documents.Open(path)
    10.  
    11.         Call DoFindReplace("System", "Computer", rngRange)
    12.         Call DoFindReplace("test", "tempo", rngRange)
    13.         Call DoFindReplace("car", "bus", rngRange)
    14.         'etc. etc.
    15.  
    16.         oDoc.SaveAs(path)
    17.         oDoc.Close()
    18.         oDoc = Nothing
    19.         oWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
    20.         oWord.Quit()
    21.         oWord = Nothing
    22. 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?

  8. #8

    Thread Starter
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Find Replace text inWord from Vb.net

    Someone! Anyone! Please, help!

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width