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

Posted on 2008-11-14
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
    LVL 76

    Expert Comment

    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

    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

    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.

    Author Comment

    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

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

    Expert Comment

    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

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    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…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now