Word.Application

J

jokobe

I'm using this code with ACC2003/Vista:

Set WordApp = GetObject(, "Word.Application")
With WordApp
.Visible = True
.Documents.Add Template:=strDocName
'vorname
.ActiveDocument.Bookmarks("fragestart").Select
.Selection.Text = StrClipboard
strsave = strDirName & "\" & strDocumentsPath & "\" &
StrNewName
.ActiveDocument.SaveAs FileName:=strsave
End With
Set WordApp = Nothing

but: sometimes it is working, sometimes not... is there a better solution
available?
 
S

Stefan Hoffmann

hi,
but: sometimes it is working, sometimes not... is there a better solution
available?
What does this mean? You are getting error messages (which one)? Or it
just does nothing?

btw, I strongly recommend not to use a With block when handling OLE
automation. This may lead to strange behavior, especially when debugging
it. I'm using short variables in these cases, e.g. wp for
Word.Application or ea for Excel.Application and so on.


mfG
--> stefan <--
 
A

Albert D. Kallal

With how you have your code now, if word is NOT running, or is closed by the
user, the your code will fail

I would suggest:

On Error GoTo CreateWordApp
Set wordApp = GetObject(, "Word.Application")
On Error Resume Next

...... you code here...


Exit Function

CreateWordApp:
' If getobject fails, then
' ms-word was NOT running. The below will then
' launch word
Set wordApp = CreateObject("Word.Application")
Resume Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top