How do I find text in a MS Word doc using VB2005 express
Can someone point me to some sample code perhaps


Re: Visual Studio Tools for Office How do I find text in a MS Word doc using VB2005 express?

Cindy Meister

Hi Kim

The best place to get help with automating an Office application using a .NET language is the office.developer.automation newsgroup. Doing a Google search on that group might even turn up some examples you can use. If not, ask there and you should get some help with sample code.

Note that you'll find a lot of information about using Find, including VBA examples that can easily be ported to VB.NET, on the site.

Re: Visual Studio Tools for Office How do I find text in a MS Word doc using VB2005 express?


I did a bit of research for one of my own projects and came up with the following function to make it easier to perform a search and replace within a word document... hope it helps

Dim oWord As Microsoft.Office.Interop.Word.Application
Dim oDoc As Microsoft.Office.Interop.Word.Document

'Start Word and open the document template.
oWord = CreateObject("Word.Application")
oDoc = oWord.Documents.Open("c:\" & strNameOfWordDoc)

'Example Useage
"DATE_TODAY", FormatDateTime(Now(), DateFormat.ShortDate))

Friend Function FindAndReplace(ByVal objWord As Microsoft.Office.Interop.Word.Application, ByVal WordToFind As String, ByVal ReplaceWith As String) As Boolean
FindAndReplace = False
With objWord.Selection.Find
.Text = WordToFind
With .Replacement
.Text = ReplaceWith
End With

FindAndReplace =
End With

Catch ex As Exception
MsgBox(ex.Message & vbCrLf & Err.Erl, MsgBoxStyle.Critical,
"Unexpected Error")
End Try
End Function

Re: Visual Studio Tools for Office How do I find text in a MS Word doc using VB2005 express?


I am very new to VBExpress and am trying to add text from a form to an existing word doc. I can open the doc but need to locate the spot where I enter the text. Your function looks like it would do a lot but I get an error saying that




are not defined.

How can I get them defined

Re: Visual Studio Tools for Office How do I find text in a MS Word doc using VB2005 express?


(NOTE: After impementing code utilzing the interop,etc... Your clients will need to do this extra features install too or they will receive an error about something not being in the GAC (Global Assembly Cache) when they run your application setup. If you get this as an error... you must follow these steps on the users machine.

First, you need to insert your MS Office disk

If it doesn't fire up from the CD Drive, you'll need to click setup.exe

Next, and (I'm not certain of the exact wording of the options [I think it's the 2nd option]) select the add features to MS Office... click next.

Now, you'll see a list of each of the Office products, Word, Excel, etc

At the bottom of the list, is an additional checkbox for Advanced or custom features. Tick that box and click next.

Let's talk about MS Word... but it applies to the others too.

Click the (gray) down arrow on MS Word and you'll see "Add .Net Programmability Support"... and there's a second option below that you should select about "Smart Tags".

Do this for Excel, Powerpoint, Outlook, etc.

Look under every Gray option, the bottom one has one to tick as well.

Once you've done that with each product you want to program against/require, click next and let the wizard do the work.

Now, Open your project.

Dbl click on "My Project"

Select "References"...."Add"

Click on the "COM" Tab

Select "Microsoft Office 11.0 Library" and click OK

Review your references.

You should now have the following references in your project;

Microsoft Office 11.0 Library [ver]

Microsoft Visual Basic for Applications Extensibility 5.3

Microsoft Office 11.0 Library [ver]

My configuration is different from yours, but you'll need at least one of the Office 11.0 libraries and Extensibility 5.3

If you don't have them, click add again and add them accordingly.

If you don't see them, then search for the keyword "VSTO" and you'll find a "Support for VSTO" download. Install it.

After doing this, you're now in position to program against the interp, etc. But remember, for your client must also perform these steps BEFORE attempting to install your MS Word upgrade edtion or they'll get the GAC error.

I'm doing this from memory so, I apologize if I've missed anything.

Should that be the case, do not hesitate to contact me and I'll try to point you in the right direction.

Re: Visual Studio Tools for Office How do I find text in a MS Word doc using VB2005 express?


Your memory is excellent!! Works great. I would never have found the additions. Thanks for your help.

Re: Visual Studio Tools for Office How do I find text in a MS Word doc using VB2005 express?


Lost the magic. But in the process, I was looking around MSDN and found something that looked really basic to get a better handle on the process. I found a "Word Document Project Template" (one for Excel too) writeup which said that the actual template contained all of the startup code for generating a VB/ Word project in VBE. From the words in the writeup [], it sounded like there would be a real code block to download. There may be but I have been searching for over an hour and have yet to find that download button. I have also looked in Downloads and in Support.

If you were wondering, the lost magic is that Microsoft.Office.Interop.Word.Application and ...Word.Document are no longer defined. Everything looks the same. Saved and closed the Project. Came back to the machine (there were some Microsoft updates in between) and defined turned to not defined. Thought looking at the templates would give me a more basic concept of what had to be defined and where.

If you could suggest where the template downloads could be found, would appreciate it. I will continue to look for a difference in my code and setup.

Re: Visual Studio Tools for Office How do I find text in a MS Word doc using VB2005 express?


What exactly are you attempting to do Dynamically generate letters or what

Re: Visual Studio Tools for Office How do I find text in a MS Word doc using VB2005 express?


After some struggling and with your help, I have managed to get my baseline application running and have built a distribution CD. I used the CD to install my application on my development machine running XP/OFFICE 2003 and it works great!! I then tried to install the application on my laptop which runs Windows 2000 Professional and Office 2003 and I get the "Unable to install or run the application. The application requires that assembly EnvDTE Version be installed in the Global Assembly
Cache (GAC) first." message.

I have installed .Net 2.0, VSTOR, reinstalled Office 2003 per your previous instructions and I continie to get the error message.

Is there a different to be done for W2006 or other OSs My application talkes to Word, Excel, and Outlook.

Thanks for your help.

Re: Visual Studio Tools for Office How do I find text in a MS Word doc using VB2005 express?


Ref your question about what I am trying to do. I am a retired engineer who is working with a citizens group that operates a non-profit wireless internet system in eastern NC. All of our equip came from a state grant. We are trying to improve economic conditions in the area and because the area is very vulnerable to hurricane damage, provide backup emergency communication in the county. We do this with a group of volunteers. The objective of the applications that I am trying to write is to computerize

our database so that we can keep our costs down and the service running. The first form that I have running on my machine but wont install on a Windows 2000 laptop for field use is for installers. It automates getting the new customer info to accounting

and into the customer data base. It also generates a clean contract for the customer and accounting. The failure to get this data to accounting was costing us almost $1000/mo last year. The next form I am working on is a customer service request form that is passed electronically from the person receiving the service call to the tech who makes the repair and back to the database. Docs the problem and collects some data which gives the tech an idea of what the has to solve. Tech reports what was done, time spent, parts used etc again to control costs. Database in Excel. Helps to control our spares inventory and also keep an eye on what our installers are charging our customers. We operate with a monthly budget margin of almost nothing and count on grants to expand service. Been doing this for 5 years. We use Word for customer agreements, Excel for customer databases, and Outlook for communication because we all work out of our houses.

We can doc over 100 new jobs developed and business growth due to the service so we have to keep it going.

Re: Visual Studio Tools for Office How do I find text in a MS Word doc using VB2005 express?


You may need to include the "Microsoft Visual Basic For Applications Extensibility 5.3" object.

You should also have the "Microsoft Office 11.0 Object Library" and "Microsoft Word 11.0 Object Library"

Answer these two questions for me.

1) Do you have these 3 object referenced , and

2) Confirm or clarify the following;

A) XP Computer has Office 2003, and

B) 2000 Pro Computer also has Office 2003 installed.

Re: Visual Studio Tools for Office How do I find text in a MS Word doc using VB2005 express?


While you're in your References Tab, click the "Unused References" button.

I'm wondering if you have ticked a reference that you shouldn't have/don't need.

Also, did you google the error (or the first x number of words of the error) I have noticed a few comments that indicated that an additional - but un-needed reference - when deselected ... removes the error. Not sure which reference they are referring to however...

I have looked into one of my project files that produces word documents (populates word docs with data from DB) and I'm wondering if you have a reference selected (and which is causing the error) that I do not have selected and therefore am unable to assist you given I don't know what you have reference that I do not have referenced. If that makes sense.

As a side note. Microsofts "Virtual Machine" is a really really good way to test your app on different operating systems - from one computer.

I recommend you download it and install it. It works like this.

You install it on your XP comuter. Create a directory called VM.

When the wizard prompts you.. select this Directory as the one to create the Virtual machine in. You'll need a few gigs of space since the Virtual Machine will create a single file within the C:\VM dir - into which it installs the new OS.

Name your VM something lke VM200ProOffice2000

You then take your Windows 2000 pro CD and install that OS into the Virtural Machine. The wizard makes it easy.

When you do this, you'll find that installing the 2000 OS is just like installing it for real on your XP computer - except that it's encapsulated in the VM directory (e.g., it doesn't interfere with your current XP configuration. After installing 2000Pro into the VM, you'll have to let it perform all of the Microsoft Updates - just as you would have had you installed 2000Pro on a computer (as it's primary or only OS).

Install MSOffice 2000Pro within that VM

You can now compile you app on your XP computer and then "Start the Virtual Machine VM200ProOffice2000" and install your application and test it.

Next, create another VM (Place the file in the C:\VM Dir with the other VMOS) and name thsi VM something lke VM200ProOffice2003.

Microsoft also just released the Virtual Machine Consol - which I haven't yet downloaded or installed but likely will in a day or so.

I had always shy'd away or disregarded the Microsoft Virtual Machine - but when I finally had a need - like the one you have now - in desparation I installed it and loaded an OS into it.

I was suprised at how easy it was to do and the benefit of being able to test the code against multiple configuration VM's - one the development computer - really saved a lot of time.

And it was so easy! I should have installed it long ago. So, bottom line - don't even hesitate to install a couple of VM's with different configurations for testing. Highly recommended.

Re: Visual Studio Tools for Office How do I find text in a MS Word doc using VB2005 express?


All three were referenced

Both XP and 2000 machines have Office 2003.

But further, I fould that I could not find the dll for the missing EnvDTE anywher on the W2000 machine

even though I had downloaded .NET 2 and VSTO. I started looking for a way to find and insert it

in the GAC and thru forum postings found that if I downloaded .NET 2 SDK, I would have the tools

to do the install. I did the download and found the required .dll and the needed tools. Before I had a chance to

use the tools, I rebooted my laptop with the OneClick CD in my drive. It autoloaded and the install proceeded

without any problems. I now have the needed app on my (and others) laptops.

Thanks to all.