简体   繁体   中英

How to open a Read-only Word Document from Excel VBA

Every time I try to open a word document in VBA excel I get a pop up window in the background that asks me how to open it because it is tagged as read-only. I have looked at the properties of the file and it isn't read-only however it is in revision control (tortoise-SVN).

Sub ReadSpec()
'References
'  Microsoft Word 14.0 Object library
'
Dim Paragraphe As Object

Dim WordApp As Word.Application
Set WordApp = CreateObject("Word.Application")

Dim WordDoc As Word.Document
Set WordDoc = WordApp.Documents.Open("Spec.docx")
WordApp.Visible = True

WordDoc.Close
WordApp.Quit


End Sub

The file may be in use by another application which is why you're being told it is read-only. This isn't a problem unless you want to write to the file. If you're only trying to read from it my suggestion is to add

Application.DisplayAlerts = False

to see if it gets rid of the prompt for you. Also note that it is generally good practice to do something more along the lines of

Sub YourMethod
    Dim PrevDispAlerts as Boolean

    PrevDispAlerts = Application.DisplayAlerts
    Application.DisplayAlerts = False

    'code that does something goes here

    Application.DisplayAlerts = PrevDispAlerts
End Sub

I'm not familiar with SVN but you might try either:

.Open("Spec.docx", ReadOnly=False) 

or

.Open("Spec.docx", ConfirmConversions=False, ReadOnly=False)

These suppress two common dialogs and force the default behavior. If you needed to override you would have to either make that explicit in the above code (ie, ReadOnly=True to force read only) or just allow the dialog to display, using your original code.

I suppose it depends on the version of Excel. Solutions often work for one version but not another.

http://smallbusiness.chron.com/open-word-document-excel-using-vba-40430.html

I found this code worked.

'Open an existing Word Document from Excel
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
'Change the directory path and file name to the location
'of the document you want to open from Excel
objWord.Documents.Open "C:\Documents\myfile.doc"

hmmm不熟悉SVN,但您可以尝试

 .Open("Spec.docx", ReadOnly=False) or Open("Spec.docx", ConfirmConversions=False, ReadOnly=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM