PDA

View Full Version : Solved: Word document locks after being read from programatically



muttleee
12-06-2005, 04:33 AM
Hiyas,

I am trying to read information from a pre-existing Word document and populate a Lotus Notes form with it. This works ok so far in testing but the problem I have is that the Word document seems to lock after I read data from it. The Notes agent works when I run it but if I then try to open the Word document (on my desktop), Word briefly starts to open and then I get a message asking if I want to revert to the saved version of the document. It doesn't matter if I choose yes or no - it still refuses to open any further and I have to resort to Ctrl+Alt+Del to get rid of the instance of Word.

Here's the code in the Notes doc that is reading from the Word doc:

Sub Postopen(Source As Notesuidocument)

Dim session As New notessession

If Source.IsNewDoc Then

'open the appropriate Word doc from the desktop
Set wdapp = CreateObject("word.application")
wdapp.documents.Open ("C:\Documents and Settings\smithj\Desktop\PSA and Dept'l Targets.doc")
wdapp.Visible = False

Set wddoc=wdapp.activedocument
wddoc.tables(1).rows(2).cells(5).select 'select a particular cell

Dim strTemp As String
strTemp = wdapp.selection.Text 'read text from the selected cell

If Source.EditMode Then
Call Source.FieldSetText ( "DeptTarget", strTemp) 'set text in field if Notes doc is editable
Else
Msgbox "'Postopen set text didn't work" 'else display error message
End If
End If

Set wdApp = Nothing 'close Word doc
Set wddoc = Nothing

End Sub
I wouldn't have thought anything should stop me opening the Word doc even after reading from it programatically. If I try to run the agent again immediately after I have run it the first time, I get a message saying the doc is "locked for editing by ' ' " but it still works ok as long as I click 'Read Only'.

Is there any way I can get round this problem..? Thanks in advance for any suggestions. http://vbaexpress.com/forum/images/smilies/001.gif

TonyJollans
12-06-2005, 05:19 AM
Hi muttleee,

Your problem is that you are not closing the Word document and you are not quitting and destroying the Word application. All you are doing by setting the pointers to Nothing is disconnecting from Word and leaving it running. As you create the Word object conditionally you should also destroy it subject to the same condition - and, although it does no harm, unconditionally setting your object variables to Nothing isn't needed. Try this Sub Postopen(Source As Notesuidocument)

Dim session As New notessession

If Source.IsNewDoc Then

'open the appropriate Word doc from the desktop
Set wdApp = CreateObject("word.application")
wdApp.Documents.Open ("C:\Documents and Settings\smithj\Desktop\PSA and Dept'l Targets.doc")
wdApp.Visible = False

Set wddoc = wdApp.ActiveDocument
wddoc.Tables(1).Rows(2).Cells(5).Select 'select a particular cell

Dim strTemp As String
strTemp = wdApp.Selection.Text 'read text from the selected cell

If Source.EditMode Then
Call Source.FieldSetText("DeptTarget", strTemp) 'set text in field if Notes doc is editable
Else
MsgBox "'Postopen set text didn't work" 'else display error message
End If

wdDoc.Close
wdApp.Quit

Set wdDoc = Nothing
Set wdApp = Nothing

End If

End Sub

muttleee
12-06-2005, 07:37 AM
Thanks Tony - that did the trick. :) Ya live and learn....

TonyJollans
12-06-2005, 08:50 AM
My pleasure!

Still living. Still learning.