Solved

VB6 Merge with Word

Posted on 2006-06-19
5
274 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
  • 3
  • 2
5 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That's good news. Thanks
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

744 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

17 Experts available now in Live!

Get 1:1 Help Now