如何从 Excel VBA 打开只读 Word 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17025592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open a Read-only Word Document from Excel VBA
提问by engineer Mike
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).
每次我尝试在 VBA excel 中打开 Word 文档时,我都会在后台弹出一个窗口,询问我如何打开它,因为它被标记为只读。我查看了文件的属性,它不是只读的,但它处于修订控制 (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
回答by Ripster
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
回答by David Zemens
I'm not familiar with SVN but you might try either:
我不熟悉 SVN,但您可以尝试:
.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 (i.e., ReadOnly=True
to force read only) or just allow the dialog to display, using your original code.
这些会抑制两个常见的对话框并强制默认行为。如果您需要覆盖,则必须在上面的代码中明确说明(即ReadOnly=True
强制只读),或者只允许使用原始代码显示对话框。
回答by user2602261
I suppose it depends on the version of Excel. Solutions often work for one version but not another.
我想这取决于Excel的版本。解决方案通常适用于一个版本而不适用于另一个版本。
http://smallbusiness.chron.com/open-word-document-excel-using-vba-40430.html
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"
回答by engineer Mike
hmmm not familiar with SVN but you might try
嗯不熟悉 SVN 但你可以试试
.Open("Spec.docx", ReadOnly=False) or Open("Spec.docx", ConfirmConversions=False, ReadOnly=False)