Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I open a word document in Read Only? 2

Status
Not open for further replies.

SiberBob

Programmer
Aug 28, 2002
107
US
Can someone tell me how to Open a Word file in Read Only mode? I can open a word file (I'm using the fHandleFile function from elsewhere on this site) in it's default state, but would like to be able to open one in read only mode as though I opened Word, then selected File / Open / and clicked the small dropdown arrow by the Open button on the Open File Dialog...

Any ideas?
 
set the attributes to read only

Hope this helps
Hymn
 
I assume you are talking about the file properties?

I was hoping to find a way to do it via VBA... But that's an option that I hadn't thought of and may be a good alternative if I can't get it done in VBA... Thanks!
 
these might help you
thread222-546625
thread222-5726
thread329-931247

Hope this helps
Hymn
 
I may not have been clear enough in my original post...

I'm using MS Office 2000

MS Word's "Read Only" mode is normally selected using the drop down arrow on the Open button of the Open Dialog box in Word.

I have some Word Documents that I don't want to password protect. Word allows documents to be opened in "Read Only" mode regardless of the file attributes. I would like to be able to have a command button on my Access form to open the Word Document in MS Word's "Read Only" mode without having to set actual file attributes or secure the file with a password.



 
SiberBob,
Try setting a reference to MS Word.
In the VBE...Tools - References

This is an excel example.

See my 1st and Last Post...for a correction!

I'm sure something like this work for you, once you figure out the proper code useage. There are several excel and pdf examples here in the forums. They may help... Search for a few. I'd start with the MS Office Forum! You may need to add it to your list.


I'll try coding it tonight. Don't have time at the moment.
Look around, I'm sure you'll get pretty close to what you need.

Post back any code attempts you may have tried!

Carl

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
SiberBob,

Here you go...

Add the reference to MS Word...

Paste this code into a new module.

Code:
Public Sub OpenDocReadOnly(strDoc As Variant)

On Error GoTo ERR_OpenDocReadOnly
'Use the Object property in Visual Basic to return a reference to the ActiveX object
Dim MyObject As Object
Set MyObject = GetObject(strDoc)    'Returns the reference to the object
'Opens the specified document and adds it to the Documents collection.
Documents.Open strDoc, , True
'The Nothing keyword is used to disassociate an object variable from an actual object
Set MyObject = Nothing
ExitingSub:
Exit Sub

ERR_OpenDocReadOnly:
    MsgBox Err.Number & vbCr & Err.Description, vbExclamation, "Error Opening Word"
    Err.Clear
    GoTo ExitingSub
       
End Sub

Place this code behind the click event of your buttons...

Code:
Private Sub Command0_Click()

Call OpenDocReadOnly("c:\test.doc")

End Sub

Change the path and document name to match yours.


To see more options when opening....

In the module...
Highlight... Open

Hit F1 - For complete useage....


Hope this is what your looking for...
Carl

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
Carl,

That's EXACTLY what I needed.

But I'm having a problem with it's implementation...

I copy/pasted your code to a new module named OpenWordDocuments

I then placed the following code on a button:
Code:
Private Sub CmdOpenDriverExaminersScheule_Click()
    Call OpenDocReadOnly("Y:\Databases\DriverExaminationOfficeLocations.doc")
End Sub

and this code on another button:
Code:
Private Sub VersionInfo_Click()
    Call OpenDocReadOnly("Y:\Databases\CommDVersionInfo.doc")
End Sub

After opening the mdb file containing this code, the first time I click on one of these buttons it works fine. The second and all subsequent times I click on either of these buttons I get the following error:

Error Opening Word
462
The remote server machine does not exist or is unavailable

[blue]any idea what I may have done?[/blue]

Thanks!

SiberBob

 

Hi SiberBob,

I'll look into it.

Something to do with trying to create an instance of word through the network. I'm quessing...

I'll do some testing...


Carl

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
The reason is probably that you're using an unqualified reference to one of the Word objects/properties (Documents.Open) which creates some headaches. Here's a (Excel) link on the topic Excel automation fails second time code runs. Try something like this (Here I'm using late binding, so there's no need for any reference to word):

[tt]dim wapp as object
dim wdoc as object
on error resume next
set wapp = getobject(,"word.application")
if err.number<>0 then
' no instance of Word, create
err.clear
set wapp = createobject("word.application")
if err.number<>0 then
' Word installed???
exit sub
end if
end if
on error goto <your errorhandler>
set wdoc=wapp.documents.open(strDoc,,true)
wapp.visible=true
set wdoc=nothing
set wapp=nothing[/tt]

- typed not tested;-)

Roy-Vidar
 
SiberBob,

Seems the error your getting is a result of the long file names.
You can rename them to a dos format(8 char max) or use

RoyVidar's Code, it seems to work like a charm...

Replace the beginning section of my module code with his!

Change this line:
on error goto <your errorhandler>

With this: On Error GoTo ERR_OpenDocReadOnly

Save it, and you should be set to go.

Carl




AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
Thanks Carl and RoyVidar! That's exactly what I needed!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top