eanda9000

Hi,

I am very new to vsto, less then one day. Basically all I need to do is find and replace text in the word document. While my code compiles, nothing is getting replaced The select works but the Replaces execute but noting appears to happen.

Please let me know if you have any ideas as to what the issue could be.

Thanks!

Protected Sub btnWordSR_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnWordSR.Click

Dim wrdApp As Word.Application

Dim wrdDoc As Word.Document

Dim oMissing As Object = System.Reflection.Missing.Value

Dim oFalse As Object = False

Dim oTrue As Object = True

''Dim wrdMailMerge As Microsoft.Office.Interop.Word.MailMerge

' Create an instance of Word

wrdApp = New Word.Application()

wrdApp.Visible = False

' Open the template document.

''Dim TemplateFile As String = TemplateFilePath + selectedTemplate

Dim oFile As Object = Server.MapPath("/ABCpdfDemo/WordTemplate/Boiler Plate Scope of Work.doc")

wrdDoc = wrdApp.Documents.Open(oFile, oMissing, oFalse, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing)

' Search and replace text

wrdDoc.Content.Select()

wrdDoc.Content.Text.Replace("customer", "ARES Corporation")

wrdDoc.Content.Text.Replace("xx/xx/xxxx", Today.Date.ToString)

wrdDoc.Content.Text.Replace("x.x", "2.1")

wrdDoc.Content.Text.Replace("Salesman", "Shelly.Coben")

wrdDoc.Content.Text.Replace("#xx-xxx", "07-101")

' Save changes to a new file

Dim oNewFile As Object = Server.MapPath("/ABCpdfDemo/WordDoc/Boiler Plate Scope of Work.doc")

wrdDoc.SaveAs(oNewFile, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing)

' Release References.

''wrdMailMerge = Nothing

wrdDoc = Nothing

wrdApp = Nothing

End Sub



Re: Visual Studio Tools for Office Simple find and replace in MS Word is not working, new to vsto

SaurabhB

Try this

Dim txt As String
txt = wrdDoc.Content.Text
txt = txt.Replace("Customer", "ARES Corporation")
txt = txt.Replace("xx/xx/xxxx", Today.Date.ToString)
txt = txt.Replace("x.x", "2.1")
txt = txt.Replace("Salesman", "Shelly.Coben")
txt = txt.Replace("#xx-xxx", "07-101")
wrdDoc.Content.Delete()
wrdDoc.Content.InsertAfter(txt)





Re: Visual Studio Tools for Office Simple find and replace in MS Word is not working, new to vsto

Cindy Meister

Yes, you must be very new to VSTO, as your code has absolutely nothing to do with VSTO. It's straight interop. This is a Winform application Which version of Word are you targeting.

Look up the Help information on Word's Range.Find / Selection.Find property. That's what you want to use to Find and Replace within a Word document.






Re: Visual Studio Tools for Office Simple find and replace in MS Word is not working, new to vsto

eanda9000

What are the top resources for learning vsto



Re: Visual Studio Tools for Office Simple find and replace in MS Word is not working, new to vsto

Cindy Meister

Well, there's the msdn VSTO pages, starting here.

Then there are a couple of books. And some blogs. You should find a fairly complete set of resources in the "Please Read Next" posting at the top of the forum.

But if you want to do Office interop (which your question appears to indicate), please note that VSTO is not Office interop and these resources will be of limited help. For Office interop, a good place to start might be Andrew Whitechapel's book ".NET development for Office".






Re: Visual Studio Tools for Office Simple find and replace in MS Word is not working, new to vsto

eanda9000

Saurabh and Cindy thank you for your help, both solutions work. I guess I am a bit confused to the difference between VSTO and interopp, is there somewhere that explains what the differences are

Thanks!





Re: Visual Studio Tools for Office Simple find and replace in MS Word is not working, new to vsto

Cindy Meister

Saurabh's suggestion will not retain any formatting the original text might contain. Using Word's internal Find/Replace should retain formatting.

Interop is manipulating the Word (or Office) application, generally. VSTO is Visual Studio Tools for Office - it provides special tools for the Visual Studio Developer (VB.NET or C#) for working with Office. Details of what it provides can be found in the msdn pages I pointed you at. Very briefly:

- "document-level" customizations for Word and Excel: a managed code alternative to VBA code embedded in an Excel workbook or Word document (or templates).

- Capability to create Shared Add-ins (COM Add-ins) optimized to work with Office applications (saves you a lot of work).

The coding in a VSTO application will often be the same, or very similar, to code you'd use in a Winform or Console app, as far as manipulating the Office application goes. But some things are different, and any special capabilities your read about in those msdn pages are special to VSTO and won't be available otherwise. It's those special capabilities and any problems involved with installing VSTO that should be discussed in this forum.