Word 2003 Automation

M

Mike Labosh

I have a VBScript student that wants to use Word 2003 for logging. I took
some time to try to work it out. VBScript and VBA are easy. Navigating
Word's bizarre object model is hard. :)

What I have so far is below. Inside the LogItem procedure, look at that
line inside LogItem where it says, "wRng.InsertAfter item"

On the first call to LogItem, if the document does not exist, LogItem works
correctly. The second call to LogItem fails on the InsertAfter method, with
an error that has no description. I assume I need to phrase that
differently, but after several different phrasing attempts, I get the same
result.

' WordLog.vbs ---------------------------------------------
LogItem("This Happened")
LogItem("That Happened")
LogItem("Something Else Happened")
LogItem("Some Other Thing Happened")

Sub LogItem(item)

' These are defined in the Word library, for use with Range.Collapse
Const wdCollapseEnd = 0
Const wdCollapseStart = 1

Dim wApp ' As Word.Application
Dim wDoc ' As Word.Document
Dim wRng ' As Word.Range

item = Now() & ", " & item & vbCrLf

Set wDoc = GetDocument("C:\Log.doc")
Set wApp = wDoc.Parent
Set wRng = wDoc.Range

wRng.Collapse wdCollapseEnd
wRng.InsertAfter item ' <----- Error here on second call to LogItem

wDoc.Save
wApp.Quit False

Set wDoc = Nothing
Set wApp = Nothing

End Sub

Function GetDocument(filename) ' As Word.Document

On Error Resume Next

Dim app ' As Word.Application
Dim doc ' As Word.Document

Set doc = GetObject(filename)

If doc Is Nothing Then
Set app = CreateObject("Word.Application")
Set doc = app.Documents.Add
doc.SaveAs filename
End If

Set GetDocument = doc

End Function

--

Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com

"Escriba coda ergo sum." -- vbSensei
 
J

Jean-Guy Marcil

Mike Labosh was telling us:
Mike Labosh nous racontait que :
I have a VBScript student that wants to use Word 2003 for logging. I
took some time to try to work it out. VBScript and VBA are easy.
Navigating Word's bizarre object model is hard. :)

What I have so far is below. Inside the LogItem procedure, look at
that line inside LogItem where it says, "wRng.InsertAfter item"

On the first call to LogItem, if the document does not exist, LogItem
works correctly. The second call to LogItem fails on the InsertAfter
method, with an error that has no description. I assume I need to
phrase that differently, but after several different phrasing
attempts, I get the same result.

' WordLog.vbs ---------------------------------------------
LogItem("This Happened")
LogItem("That Happened")
LogItem("Something Else Happened")
LogItem("Some Other Thing Happened")

Sub LogItem(item)

' These are defined in the Word library, for use with Range.Collapse
Const wdCollapseEnd = 0
Const wdCollapseStart = 1

Dim wApp ' As Word.Application
Dim wDoc ' As Word.Document
Dim wRng ' As Word.Range

item = Now() & ", " & item & vbCrLf

Set wDoc = GetDocument("C:\Log.doc")
Set wApp = wDoc.Parent
Set wRng = wDoc.Range

wRng.Collapse wdCollapseEnd
wRng.InsertAfter item ' <----- Error here on second call to LogItem

wDoc.Save
wApp.Quit False

Set wDoc = Nothing
Set wApp = Nothing

End Sub

Function GetDocument(filename) ' As Word.Document

On Error Resume Next

Dim app ' As Word.Application
Dim doc ' As Word.Document

Set doc = GetObject(filename)

If doc Is Nothing Then
Set app = CreateObject("Word.Application")
Set doc = app.Documents.Add
doc.SaveAs filename
End If

Set GetDocument = doc

End Function

I do not normally work with VBScript, but I did notice something peculiar
with your code:

When using GetObject the code may latch on an existing Word instance. So,
the code you are using in the main sub:

Set wDoc = GetDocument("C:\Log.doc")
Set wApp = wDoc.Parent
Set wRng = wDoc.Range
..
..
..
Set wDoc = Nothing
Set wApp = Nothing

Because you set wApp based on how wDoc was created, you may actually close
opened documents, without saving them. This may annoy some users...
Normally, we declare the App, then create the doc based on the App, not the
other way around.
We can tell if the App was already running, so at the end of the code, if it
was already running, we do not close the App, but just the document.

Or, we do not use Get Object, but CreateObject, this way we know for sure
that we are creating a new App, so we can close it regardless of the fact
that Word was already running or not.

Besides that, I pasted your code in Excel, an ran it from there as is.
Besides the problem I mention above, whether Word was already running or
not, I never got the error message you got.

I did not try to run it as a VB script though.
I have done it before, but I do not remember how to do it now...

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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