Solved

VB6 Merge with Word

Posted on 2006-06-19
5
284 Views
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.

Thanks.
 

0
Comment
Question by:markevans100
[X]
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
5 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 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.
0
 

Author Comment

by:markevans100
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?

Thanks

0
 
LVL 76

Expert Comment

by:GrahamSkan
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:

http://www.officearticles.com/word/steps_to_troubleshooting_microsoft_word.htm
0
 

Author Comment

by:markevans100
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.

Thanks
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 16957767
That's good news. Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
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…
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…
Suggested Courses
Course of the Month7 days, 16 hours left to enroll

617 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