Document is locked for editing by user.

P

PBI

I am attempting to insert data in a Word document from an Access
database using VB6 code. The code compiles without errors; however, when
executed the locked for editing error pops up. I know that the document is
not in use and the error message is triggered by:

Set MSWordDoc = wd.Documents.Open("H:\Test2.doc", , False, , , , , , , , ,
False)

Sub code follows:

Public Sub EdDoc(strField1 As String)
Const Find = "[OCCASION]"
Dim MSWordDoc As Document
Dim wd As New Word.Application
Dim rng As Range
Dim hf As HeaderFooter
Dim s As Section
Set MSWordDoc = wd.Documents.Open("H:\Test2.doc", , False, , , , , , , , ,
False)
Set rng = hf.Range
With rng.Find
.Text = Find
.Replacement.Text = strField1
.Execute Replace:=wdReplaceAll
End With
DoCmd.Save ("H:\Notice.doc")
DoCmd.Close
wd.Quit
End Sub

Please help.
 
T

Tom van Stiphout

On Tue, 14 Oct 2008 10:38:08 -0700, PBI

Change that to:
Dim MSWordDoc As Word.Document
or you may get a Type Mismatch.

Also, be sure to run Task Manager (alt_ctrl_del > Task Manager >
Processes and be sure that MsWord is not running. It could be running
invisibly (especially if you set that argument to False).

-Tom.
Microsoft Access MVP
 
P

PBI

Thanks Tom. Your change in addition to changing the ReadOnly parameter
to True got me past that error. However, the program exits the subroutine at
the next set statement. Am I approaching the correctly? Iam a programmer ;
but, I don't speak VB. The text I am trying to replace is in the body of the
document, not the header. I would like the Replace command to search the
entire document for the string provided.
--
Paul


Tom van Stiphout said:
On Tue, 14 Oct 2008 10:38:08 -0700, PBI

Change that to:
Dim MSWordDoc As Word.Document
or you may get a Type Mismatch.

Also, be sure to run Task Manager (alt_ctrl_del > Task Manager >
Processes and be sure that MsWord is not running. It could be running
invisibly (especially if you set that argument to False).

-Tom.
Microsoft Access MVP

I am attempting to insert data in a Word document from an Access
database using VB6 code. The code compiles without errors; however, when
executed the locked for editing error pops up. I know that the document is
not in use and the error message is triggered by:

Set MSWordDoc = wd.Documents.Open("H:\Test2.doc", , False, , , , , , , , ,
False)

Sub code follows:

Public Sub EdDoc(strField1 As String)
Const Find = "[OCCASION]"
Dim MSWordDoc As Document
Dim wd As New Word.Application
Dim rng As Range
Dim hf As HeaderFooter
Dim s As Section
Set MSWordDoc = wd.Documents.Open("H:\Test2.doc", , False, , , , , , , , ,
False)
Set rng = hf.Range
With rng.Find
.Text = Find
.Replacement.Text = strField1
.Execute Replace:=wdReplaceAll
End With
DoCmd.Save ("H:\Notice.doc")
DoCmd.Close
wd.Quit
End Sub

Please help.
 
T

Tom van Stiphout

On Wed, 15 Oct 2008 03:13:01 -0700, PBI

I don't often program in Word. Typically I first create a macro in
Word that I record the action in that I want to program. Then I look
at the VBA that was generated, and it gives me the basis for my code.

In your case I think you're missing "Selection.WholeStory" to select
all text.

-Tom.
 

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