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
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