Word.Application

  • Thread starter Thread starter jokobe
  • Start date Start date
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?
 
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 <--
 
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
 
Back
Top