?
Solved

Find and Replace text in Microsoft Word via MS Visual Studio

Posted on 2010-08-18
5
Medium Priority
?
324 Views
Last Modified: 2013-11-27
Does anyone know if it is possible to find and replace text in Microsoft Word via MS Visual Studio? This would be useful for engineering changes.

I originally wanted to do a find and replace text with PDF files, but wasn't sure if it would be possible to; hence converted them to word. If anyone knows how to do a find and replace text with PDF files that would be great.

Thanks!
0
Comment
Question by:jeremyll
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 33473304
This works in VB 2008 express
        Dim wdApp As New Microsoft.Office.Interop.Word.Application
        Dim wdDoc As Microsoft.Office.Interop.Word.Document
        'Dim wdRange As Microsoft.Office.Interop.Word.Range
        Try
            wdApp.Visible = True
            wdDoc = wdApp.Documents.Open("c:\myfolder\test.doc")

            With wdDoc.Range.Find
                .Text = "old text"
                .Replacement.Text = "new text"
                .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
                .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
            End With
            MsgBox("Saving File")
            wdApp.Documents.Item(1).Save()
            wdApp.Documents.Item(1).Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Open in new window

0
 

Author Comment

by:jeremyll
ID: 33481569
Sorry guys, I realised I didn't make myself entirely clear.

I would like a way to do a find and replace text for multiple files. I've got about 500 word documents!

0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 33483767
If you put them all in one folder, you could run this code
        Dim wdApp As New Microsoft.Office.Interop.Word.Application
        Dim wdDoc As Microsoft.Office.Interop.Word.Document
        Dim wdRange As Microsoft.Office.Interop.Word.Range
        Dim strFiles() As String
        Dim f As Integer
        Dim ex As Exception
        Try
            strFiles = IO.Directory.GetFiles("c:\myfolder", "*.doc*")
            For f = 0 To UBound(strFiles)
                wdApp.Visible = True
                wdDoc = wdApp.Documents.Open(strFiles(f))
                wdRange = wdDoc.Range
                With wdRange.Find
                    .Text = "old text"
                    .Replacement.Text = "new text"
                    .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
                    If .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll) Then
                        MsgBox("Saving File" & strFiles(f))
                        wdApp.Documents.Item(1).Save()
                    Else
                        MsgBox("No changes to " & strFiles(f))
                    End If
                End With
                wdApp.Documents.Item(1).Close()
            Next f
        Catch ex 'As Exception
            MsgBox(ex.Message)
        End Try

Open in new window

0
 

Author Closing Comment

by:jeremyll
ID: 33483830
Thanks!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question