[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

VB6 Merge with Word

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
markevans100
Asked:
markevans100
  • 3
  • 2
1 Solution
 
GrahamSkanRetiredCommented:
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
 
markevans100Author Commented:
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
 
GrahamSkanRetiredCommented:
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
 
markevans100Author Commented:
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
 
GrahamSkanRetiredCommented:
That's good news. Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now