Interacting with MS Word

PeterW
09-03-2000, 11:47 AM
Help! I have spent hours now, trying to create a function that produces new Word documents. That means I have to work with the Word objects, and I do not seem to get the hang of it! Whenever I call the following code from a loop it hangs (doesn't let me create an object), and it seems to hang sometimes even if I do not call it from a loop. Anyone care to put me on the right track, please?

Peter

---------------------

'References to MSWord 9.0 and MSOffice 9.0



Public Function llr_ProcDocument(rstrName As String, rstrContents As String) As Boolean

Dim wrdApp As New Word.Application
Dim myDoc As New Word.Document
Dim objSel As Word.Selection

Set myDoc = Documents.Add

'Work with the new document

ActiveDocument.SaveAs FileName:=rstrName & ".doc"

myDoc.Close SaveChanges:=wdSaveChanges

Set myDoc = Nothing
Set wrdApp = Nothing
Set objSel = Nothing

End Function

TheOnly
09-03-2000, 05:48 PM
Hi Peter !!

One Reason why it's hang, is that you create for each function call a unique hidden Word Object. If you running NT you can check this with the TaskManager.

The other maybe (I'm not sure) is the use of "ActiveDocument" reference, afaik it should only be used in Word VBA.

So if you need to create the Word Object every time when you call the function, place after mydoc.close ...
"wrdAPP.quit" then you avoid the hangs.

A better solution would be to create/destroy the Word Object outside the function.

Martin

------------------
example:
Sub Main()

Dim bStatus As Boolean
Dim iCounter As Integer
Dim WordObject As New Word.Application


For iCounter = 1 To 10
bStatus = llr_ProcDocument(rstrName:="hugo" & CStr(iCounter), _
rstrContents:="Hugo", _
objWord:=WordObject)
Next iCounter

WordObject.Quit
Set WordObject = Nothing

End Sub

Public Function llr_ProcDocument(rstrName As String, rstrContents As String, _
objWord As Word.Application) As Boolean


Dim myDoc As Word.Document
Dim objSel As Word.Selection

Set myDoc = objWord.Documents.Add

'Work with the new document

myDoc.SaveAs FileName:=rstrName & ".doc"
myDoc.Close SaveChanges:=wdSaveChanges

Set myDoc = Nothing
Set objSel = Nothing

End Function

PeterW
09-04-2000, 02:32 AM
Hello Martin, and thank you for your reply! I am not asking this out of idle curiosity, I need to get that code to work...

What you write makes sense - stupid of me to create the Word object inside the loop, rather than outside - and I have implemented your advice. Sadly, it still doesn't work! I get the error message: "Run-time error 429, ActiveX component can't create object". I even tried to run your code on my machine, and got the same error... :-(
So if you have any other ideas I will be most grateful!

Peter

NoahBody
09-05-2000, 11:49 AM
Peter,
You need to initialize the word object:

'Declare it
Dim mwobNewApp As Word.Application

'Init it
Set mwobNewApp = CreateObject("Word.Application")

'Now create the doc
mwobNewApp.Documents.Add

'You can reference it with Selection:
mwobNewApp.Selection.TypeText "Bullwinkle is a dope..."

Hope This Helps,
->Noah

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum