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

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!


Who is Participating?
GrahamSkanConnect With a Mentor RetiredCommented:
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.
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

lthamesAuthor Commented:
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?

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

lthamesAuthor Commented:
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!
lthamesAuthor Commented:
OH, and does each client machine need WORD installed for this to work?
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.
lthamesAuthor Commented:
Absolutely amazing!!!!!!   I turned pages of code into about 1/2 a page using the Word Object and Find/Replace!  Thank you!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.