?
Solved

VB and Word

Posted on 2003-03-10
24
Medium Priority
?
283 Views
Last Modified: 2010-05-01
I'va a Visual Basic Application which opens a WordApp, adds a new doc and then fill the doc with many information provided by a db and the user can choose what will be inserted.

That all works perfect, and makes no problems. After the inserting-process word stays open to give the user a way to change the doc by himself and then save and close the doc. My vb App only opens up word, fills in the information and then does nothing else (but stays running)

When the user changes the doc (or close it unchanged, it makes no difference in the error) then i got an unhandled exception from word and the word instance stays open..

i does following to open a word doc:

dim WordApp as new Word.Application
dim MyDoc as Word.Document

set MyDoc = wordapp.add

'...
' inserts the data
'...

wordapp.visible


nothing else.. if i do the above code without filling in data, or only fill data like MyDoc.Range.Text = "blablabla" there's also no error

the exception cames from word and not from vb

can anyone help me with this error?

And is there any way to wait in the vb-app until word is closed again? (like modal?)

Thx for your help

!!!WOW!!! new Info: I've found out, that this exception only occurs, when the user doesn't save the doc. When he saves the doc, no error happens, but when i answers with "no" (word asks if it shall save the doc) word crashs, when i save word properly closes..

any ideas?
0
Comment
Question by:Reiti
[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
  • 14
  • 7
  • 3
24 Comments
 
LVL 3

Expert Comment

by:OblivionSY
ID: 8109497
What about bringing up a form as a vbmodal option saying "please wait while Word file is being created... Click ok to continue" then the ok button unloads the form.

Or try getting the VB program to save the doc after is has been completed, would the error still appear?
0
 

Author Comment

by:Reiti
ID: 8109515
In the App there's a button "Preview" and one for Finish. Finish saves automatically, Preview only shows up the result in word without saving...

when the doc is saved no error occurs.

in the doc there are many "Insertfile" in the app to fill data in the word doc, can this cause any problems?
0
 
LVL 3

Expert Comment

by:OblivionSY
ID: 8109594
Why not use a mailmerge technique, or are the format of these word docs changing?

Not sure what an "insertfile" is, so cant answer that part of the question, however, if you suspect it might cause problems then there is a chance it is. Try removing them, leave 1 in and see if it works. (make a copy of your file before stripping it down tho!)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Expert Comment

by:Steiner
ID: 8109608
Maybe

Set WordApp = Nothing
Set MyDoc = Nothing

in your VB-App right after you opened the doc and gave the control over Word to the user can solve your problem. I encountered such exception errors mainly, when VB still had an object pointing to Word while Word was already closed (same goes for the document).

Do you have macros in your Document?

Greets
Steiner
0
 

Author Comment

by:Reiti
ID: 8109635
i allready tried " = nothing" in the app, but the error comes still up..

there are no macros in the word file, but i use a template for the doc.

the last way, to avoid this error will be, to save the preview too (to a temp. dir or anything) but i really want to avoid this way..
0
 

Author Comment

by:Reiti
ID: 8109638
with insertfile i mean

Selection.InsertFile ...

to add the data of another word-file into the doc
0
 
LVL 3

Expert Comment

by:OblivionSY
ID: 8109663
If you use a template, then the format is faily fixed, what about using the mailmerge to merge the data into it? rather than copying and inserting selections?

Else it is possible to copy and paste into applications through the clipboard object and sending keys (like Ctrl V)

However, Maybe saving the file is not a bad answer, it can always be deleted from the VB Project
0
 

Author Comment

by:Reiti
ID: 8109722
bad news.. automatically saving the doc from vb do not help against the error.. the error comes up, when closing the word app.. with this way, the error pops up immidiatly, without automatically saving from vb first a "saveas" dialog comes up, when hitting yes and saving no error occurs and word closes, with no savong (hitting no) the error occurs..
in the error-msg-box there stand's the name of the doc, so i think, the document object makes the error and not the wordapp.

the template is necessary because the template must be able to be changed without recompiling the vb-app.

in the vb-app i fill some formfields, somteimes insert pictures and inserting word-files..

without inserting word-files and without inserting pictures and without filling the formfileds, the error also occurs..

i've no idea why ..
0
 
LVL 3

Expert Comment

by:OblivionSY
ID: 8109757
try putting this code around the VB.

at the start of the sub put "On error goto Hell"

YOU CODE HERE
Then put this at the end

   On Error GoTo 0
Exit Sub

Hell:
   Msgbox "VB CAused an error, it was ... Error " & err.number & " " & err.desc

End Sub



try that and it will see whether it is VB causing the error
0
 

Author Comment

by:Reiti
ID: 8109759
more info:

that's the funktion to do the inserting and so:

Set WordApp = New Word.Application
WordApp.Visible = False
Set Doc = WordApp.Documents.Add(App.Path & "\ta.dot")

-----------------
the following function inserts the data into the doc.
i take an Selection: wordapp.Selection and insert the data step for step, and go forward with sel.collapseAll wdCollapseEnd
-----------------
DoIt
---------------
After the insertion i do the following code to cleanup..
the Word is still open, and when i close word, word throws the error... (something like "command 'written' cannot execute" or anything
---------------

WordApp.Visible = True
Set Doc = Nothing
Set WordApp = Nothing
0
 

Author Comment

by:Reiti
ID: 8109765
vb does not throw any error, the error comes from word.. in the vbapp is already error-handling implemented..
0
 
LVL 11

Expert Comment

by:Steiner
ID: 8110009
Maybe it helps if you set:
MyDoc.Saved = True

That way Word thinks that document is saed (although it isn't) and does not ask again.
Could be worth a try...

Greets
Steiner
0
 
LVL 11

Expert Comment

by:Steiner
ID: 8110015
Oops, forgot that the user closes Word and not your program.

Have you tried first closing your VB-App and then closing Word manually? Does the error then still occur?

Greets
Steiner
0
 

Author Comment

by:Reiti
ID: 8110023
hm.. i'm now tried to use office XP instead of Office 2000

but now nothing is working.. instead of starting word, Windows installer pops up for a moment and then i got the error "dll not found".... i had felt it, that upgrading to XP can never be a good thing..
0
 

Author Comment

by:Reiti
ID: 8110028
before i forgot, i was using a Office 200 without any Service Release on SR-1 the error doesn't happen frequently, but there are still some constelations in which word doesn't close completely
0
 

Author Comment

by:Reiti
ID: 8110327
Hey Guys!

The error only cames up if i use a template ...

but i must use a template ..

any ideas?
0
 
LVL 3

Expert Comment

by:OblivionSY
ID: 8110347
When u say template, its a proper word  template not just a word file saved as .doc which u use as a "template"?
0
 

Author Comment

by:Reiti
ID: 8110379
no it's a self created ".dot" file

in the template is a "Filename" - Field where the aktual Filename will show up (word-specific field) some formfields (text) and a letterhead with an embedded picture, some address-info and nothing more.. could not find out why this problem exists ..

when i open the template normal with word (by double-clicking on the .dot file) the error only comes up if there are still a prozess of word running .. if no prozess runs normaly open a doc with the template causes no errors..

in the vb-app opening a doc file without the template works well, but with the template (in documents.add ..) then the error comes when closing the doc..
0
 

Author Comment

by:Reiti
ID: 8110454
new info: the error occurs only when document.add is called with NewTemplate:=false

if i open the doc as Template (in vb) then no error..
0
 
LVL 3

Expert Comment

by:OblivionSY
ID: 8110547
what is the text of the error message?
0
 

Author Comment

by:Reiti
ID: 8111769
ok i've now created a new template, something in the template had caused the error, but i don't know what it was, i think there was some problem with the formattemplates within the template..

now it works fine, and without any error .. is there a way to close the question?
0
 
LVL 3

Accepted Solution

by:
OblivionSY earned 225 total points
ID: 8111806
Accept an answer ;o) even if its in the spirit of helping
0
 

Author Comment

by:Reiti
ID: 8111878
allright, i think "OblivionSY" has spend enough "blood" ;)
0
 

Author Comment

by:Reiti
ID: 8111887
thank for your help, i'll honor this with the points you get ;)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…
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…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

743 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