vba excel打开word文档

每次我尝试在VBA excel中打开一个word文档时,我会在后台popup一个窗口,询问如何打开它,因为它被标记为只读。 我已经看了文件的属性,它不是只读的,但它是在版本控制(龟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 

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

 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 

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

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

要么

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

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

我想这取决于Excel的版本。 解决scheme通常适用于一个版本,但不是另一个

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" 

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

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