Go Premium for a chance to win a PS4. Enter to Win


VB6 Merge with Word

Posted on 2006-06-19
Medium Priority
Last Modified: 2010-04-07
I have a Word template that I want to send data to from a VB6 app (it is not write protected and the final document gets saved as something else to preserve the formatting in the template). I have tried using Bookmarks, and it's fine on my PC with Word 2003 using the following:

    psDoc = "C:\Temp\Master.dot"
    'Create global word object
    Set gobjWordDoc = gobjWordApp.Documents.Open(psDoc)
    With gobjWordApp
        .ActiveDocument.Bookmarks("AgentData").Range.Text = txtAgentText

but when I distribute the app it gives an error:
compiled VB application has encountered a problem and needs to close.

Is there another (better) way to insert text into a pre-existing Word doc (maybe for older versions of Word) other than using bookmarks that might be version non-specific? Or the right way to use bookmarks if I'm not using them correctly.


Question by:markevans100
  • 3
  • 2
LVL 76

Accepted Solution

GrahamSkan earned 1500 total points
ID: 16932844
Basically, you seem to be doing it right. If you weren't, it wouldn't work on your machine. That type of error indicates some sort of bug or corruption.

I would make a few of changes, though not specifically addressing your problem.

First, create a new document from the template, secondly use the document object that you have just created instead of going to the application's ActiveDocument  and thirdly replace the bookmark, in case it gets overwritten.

Dim rng As Word.Range

psDoc = "C:\Temp\Master.dot"
    'Create global word object
    Set gobjWordDoc = Documents.Add(psDoc)
    With gobjWordDoc
        Set rng = .Bookmarks("AgentData").Range
        rng.Text = txtAgentText
        .Bookmarks.Add "AgentData", rng

Does it fail on every other system?

I suppose that you are using late binding, which is generally more reliable than early, but which ever binding type it is, it might be worth experimenting with the other.

Author Comment

ID: 16939201
I have just found out that I developed the app using the Word 2003 (Word 11.0 olb) object library, but the target machine is running Word 2000 - presumably this might be what is causing the problem?? Do I need to ship the MSWORD.OLB as part of the installation, or will this screw up the target machine? If I don't ship it, how  can I use the Word 2003 object library to reference Word?


LVL 76

Expert Comment

ID: 16940035
As a rule, you should develop, or at least do the in-depth testing, in the earliest environment that you have to support.

You would have to install the later Word version. I doubt if simply copying the library would work, even if it were legal. However, the code snippets that we are talking about are perfectly valid in Word 2000, so I doesn't look like a simple version problem.

If you only have the problem with one machine, there might be something wrong with that installation. Here are some steps to fix that sort of problem:


Author Comment

ID: 16956496
It was a late binding issue, since by using early binding I had to have a reference to the msword.olb and different versions of Word use different versions of the .olb file (eg. msword8.olb), so it couldn't find the reference. Using late binding doesn't require the reference in the project so problem solved.

LVL 76

Expert Comment

ID: 16957767
That's good news. Thanks

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

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 …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

885 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