# Opening a word doc in VB...

**URL:** <https://boards.straightdope.com/t/opening-a-word-doc-in-vb/308859>\
**Category:** Factual Questions\
**Created:** [June 19, 2005, 12:42am UTC](https://boards.straightdope.com/t/opening-a-word-doc-in-vb/308859 "2005-06-19T00:42:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![legion](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@legion](https://boards.straightdope.com/u/legion)\
**Post date:** [June 19, 2005, 12:42am UTC](https://boards.straightdope.com/t/opening-a-word-doc-in-vb/308859/1 "2005-06-19T00:42:16Z")

</div>

…to allow VB programmed string/text manipulation. Is there a way to do this without involving expensive OCX’S?

---

<div class="post-metadata">

**Author:** ![Shalmanese](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Shalmanese](https://boards.straightdope.com/u/Shalmanese)\
**Post date:** [June 19, 2005, 3:23am UTC](https://boards.straightdope.com/t/opening-a-word-doc-in-vb/308859/2 "2005-06-19T03:23:37Z")

</div>

If your using [VB.NET](http://VB.NET), check out the Microsoft Office interop. It allows .NET programs to programatically call Office for certain functions. If you just want to extract the text from a word doc, then you can just open up the file in the background, select all the text and copy into VB.

However, Office is required to be installed on all machines running that code. That may be a problem.

---

<div class="post-metadata">

**Author:** ![OttoDaFe](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ottodafe/32/356_2.png) [@OttoDaFe](https://boards.straightdope.com/u/OttoDaFe)\
**Post date:** [June 19, 2005, 3:28am UTC](https://boards.straightdope.com/t/opening-a-word-doc-in-vb/308859/3 "2005-06-19T03:28:05Z")

</div>

You don’t say which versions of VB or Word you’re using, but generally speaking the first step would be to add the Word object library (if it’s available) to your project:[ul]  
[li]Open the **Project** menu[/li][li]Click **References** [/li][li]Scroll down to the Word library (in VS6 it’s “Midrosoft Word 8.0 Object Library”)[/li][li]Check the box and click **OK** [/li][/ul]This exposes the Word programming interface to your project. I’m not too familiar with Word, though I’ve done quite a bit with Excel; but googling “word visual basic automation” should point you to something with more detail. (I’ve done a quick looksee at MSDN, but they’re concentrating on .NET at the expense of the legacy products. Understandable, I guess. . . .)

If you can’t add the library, I would suggest using a macro within the document. If you’re going to be working with multiple documents, you could create a template—paste the text in, manipulate it, and copy it back out.

Have fun!

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [June 19, 2005, 3:30am UTC](https://boards.straightdope.com/t/opening-a-word-doc-in-vb/308859/4 "2005-06-19T03:30:09Z")

</div>

I’m not a VB programmer, but if nobody else answers, here are two things you could try:

1. Use VB for Applications (or whatever it’s called) – Word’s built in macro language – instead of standalone VB.

2. Use Word or the free Word Viewer to open the files (in the background or visible to the user), then either save it as another document type or use the clipboard to copy and change text. You might have to fake some keyboard or mouse movements for this and I don’t think it’s a very good way…

3. Maybe this page will help a bit: [Loading...](http://www.vbdotnetheaven.com/Code/Jul2003/2123.asp)

4. The best solution: Wait for somebody who actually knows what they’re talking about 🙂

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [June 19, 2005, 3:31am UTC](https://boards.straightdope.com/t/opening-a-word-doc-in-vb/308859/5 "2005-06-19T03:31:51Z")

</div>

(Oops, that was obviously a late simulpost.)

---

<div class="post-metadata">

**Author:** ![Armilla](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Armilla](https://boards.straightdope.com/u/Armilla)\
**Post date:** [June 19, 2005, 6:11am UTC](https://boards.straightdope.com/t/opening-a-word-doc-in-vb/308859/6 "2005-06-19T06:11:55Z")

</div>

As others have mentioned probably the best way to approach this is to write the code directly in Word using VBA.

If the machine on which the code needs to run does not have Word installed then you’re into a more complex situation. You really don’t want to get into the whole arena of parsing the native Word file format.

Options I would suggest in this case are either saving the Word documents out as RTF (Rich Text) format or, if you have Word 2003, in XML format.

In the first case you should be able to efficiently process the simpler RTF file using regular expressions (reference the Microsoft VBScript Regular Expressions 5.5 object). If you use XML then the MSXML parser should be handy, though I’d recommend a SAX approach if you’re dealing with large documents.

---

<div class="post-metadata">

**Author:** ![legion](https://avatars.discourse-cdn.com/v4/letter/l/838e76/32.png) [@legion](https://boards.straightdope.com/u/legion)\
**Post date:** [June 22, 2005, 10:57pm UTC](https://boards.straightdope.com/t/opening-a-word-doc-in-vb/308859/7 "2005-06-22T22:57:13Z")

</div>

Thanks all.

Problem solved using VBA. 🙂
