Learn how to a build a cloud-first strategyRegister Now


How can I pull a large Word doc into variables in VB6?

Posted on 2008-11-14
Medium Priority
Last Modified: 2012-05-05
I need to read a Word doc, replace some of the information (they are placeholders) and then write the file out to another filename.  The file is over 800k

I've done this many times with HTML files and smaller RTF files . . .but never any so large.  I just open the file in binary mode and read the entire file into a string variable.  I'm pretty sure this won't work with such a large file (am I missing something easy?)

I could really use some direction!  This project is due by Monday and this is my last hurdle!


Question by:lthames
  • 4
  • 3
LVL 76

Expert Comment

ID: 22966353
Have you considered using the OLE Automation to drive the Word application? That way you can use the methods of the Word Object model to change the data at bookmarks or found text.

The snippet demonstrates the code if your placeholders have been implemented as bookmarks. It was written using early binding, but I have commented out the Word object types in the Dims and the Word-defined constant to make it a bit easier to try.
Sub AddTextToBookmark()
    Dim wdApp 'As Word.Application
    Dim wdDoc 'As Word.Document
    Dim bmk 'As Word.Bookmark
    Dim rng 'As Word.Range
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = True
    Set wdDoc = wdApp.Documents.Open("C:\MyFolder\MyFile.rtf")
    Set bmk = wdDoc.Bookmarks("MyBookmark")
    Set rng = bmk.Range
    rng.Text = "New Text"
    wdDoc.Bookmarks.Add "MyBookmark", rng 'ensure bookmark contains the new text
    wdDoc.Close -1 'wdSaveChanges
End Sub

Open in new window


Author Comment

ID: 22967309
No, I hadn't thought of that.  I will try it.

The placeholders look like this:   [client], [type_text], and [options], etc.  But some of these get replaced with entire paragraphs of formatted text.  Would this be a problem?

LVL 76

Accepted Solution

GrahamSkan earned 2000 total points
ID: 22967446
That code won't work. It's only for bookmarks. You would best be using Find to locate the position.

In what way is the incoming text formatted? Simply font - name, colour, size etc,  or is there layout - indenting,  spacing, etc to consider.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 22967591
Could I 'bookmark' the placeholders in WORD and then use that code?  IE . . . . bookmark [client] as a bookmard and then replace it?  Or would the Find still be better?

Font, Lists, Indenting . . . but they already have the WORD formatting appled to them; done.  The function as it is now works (opening binary file and replacing the placeholders with the code generated data).  BUT it only works in small pieces, not this entire file!

Thanks for your help!

Author Comment

ID: 22967651
OH, and does each client machine need WORD installed for this to work?
LVL 76

Expert Comment

ID: 22967668
I don't know your full scenario. If you want to set up a system to do this many times in the future then bookmarks instead of/or as well as text placeholders would be a good idea.

If you have document(s) already with the placeholders then you would need to find them to replace them with bookmarks, so it wouldn't be a lot easier.

You would need to have Word installed to use the Word object library, yes.

Author Closing Comment

ID: 31517043
Absolutely amazing!!!!!!   I turned pages of code into about 1/2 a page using the Word Object and Find/Replace!  Thank you!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month20 days, 21 hours left to enroll

810 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