如何从Excel VBA打开只读Word文档

时间:2013-06-10 13:55:13

标签: excel vba ms-word

每当我尝试在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

4 个答案:

答案 0 :(得分:1)

该文件可能正由另一个应用程序使用,这就是为什么您被告知它是只读的。除非您要写入文件,否则这不是问题。如果您只是想从中读取,我的建议是添加

Application.DisplayAlerts = False

看看它是否摆脱了你的提示。另请注意,通常做一些更好的做法

Sub YourMethod
    Dim PrevDispAlerts as Boolean

    PrevDispAlerts = Application.DisplayAlerts
    Application.DisplayAlerts = False

    'code that does something goes here

    Application.DisplayAlerts = PrevDispAlerts
End Sub

答案 1 :(得分:1)

我不熟悉SVN,但您可以尝试:

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

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

这些会禁止两个常用对话框并强制执行默认行为。如果您需要覆盖,则必须在上面的代码中明确表示(即ReadOnly=True强制只读)或者只允许使用原始代码显示对话框。

答案 2 :(得分:0)

我认为这取决于Excel的版本。解决方案通常适用于一个版本,但不适用于另一个版本。

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

我发现此代码有效。

'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"

答案 3 :(得分:-1)

嗯,不熟悉SVN,但你可以试试

 .Open("Spec.docx", ReadOnly=False) or Open("Spec.docx", ConfirmConversions=False, ReadOnly=False)
相关问题