Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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\"
    '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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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\"
    '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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

721 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