Document doc = Application.DocumentManager.MdiActiveDocument; using (doc.LockDocument()) { 需要锁定文件,将代码写入到这里; }