我们在应用程序中运行了这几行代码已经有几年了(在多个版本的Office,2003、2007、2010等中)。目的是在Word文档中执行一种邮件合并,用数据库中的姓名、地址等替换字段占位符:
Dim w As Word.Application
Dim d As Microsoft.Office.Interop.Word.Document = Nothing
...
Dim f As Microsoft.Office.Interop.Word.Field
For Each f In d.Fields
f.Select()
If fieldName = w.Selection.Text Then
f.Result.Text = value
End If
Next
然而,使用Office 2013的用户报告称,在f.Result.Text = value
这一行出现了以下错误:
System.Runtime.InteropServices.COMException (0x800A17EC): You are not allowed to edit this selection because it is protected.
因此,只有在用户使用Office 2013时才会发生这种情况,而该错误的在线帮助非常有限。
文档的任何部分都没有受到保护,用户可以直接在Word中编辑文档而没有任何问题。
ReadingLayout
,我猜它是在后来的版本中引入的,所以我使用了w.ActiveWindow.View.Type = wdPageView
。 - Alex