Hello, I am attempting to run a command over many files in a directory. I have a function that successfully iterates over the files in a directory and opens them, and then closes them again. However when I try to run a command on each file, my DocumentLock throws an eNoDocument exception. Maybe it has something to do with the fact that when my documents are opened, they have dialog boxes come up asking whether or not to show proxy images? Here is my code:
delegate void DoSomething(Document doc);
[CommandMethod("MASSINSERTGP4")] public void MassInsertGP4() { DoSomethingEnMasse(delegate(Document doc) { doc.SendStringToExecute("IEMBED all\n\n", true, false, false); }, true); } private void DoSomethingEnMasse(DoSomething something, bool savechanges) { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; try { PromptResult pr1 = ed.GetString("Directory path"); if (pr1.Status != PromptStatus.OK) { ed.WriteMessage("incorrect usage"); return; } ed.WriteMessage("Starting at " + DateTime.Now + "\n"); IEnumerable<string> files = new DirectoryInfo(pr1.StringResult).GetFiles().Where(x => x.Extension.ToLower() == ".dwg").Select(x => x.FullName); int i = 0; int count = files.Count(); foreach (string file in files) { try { i++; ed.WriteMessage("Processing " + i + " of " + count + ": " + file + "\n"); Document doc = Application.DocumentManager.Open(file, false); // DO THE THING THAT GETS DONE EN MASSE using (doc.LockDocument()) { something(doc); } if (savechanges) doc.CloseAndSave(file); else doc.CloseAndDiscard(); } catch (System.Exception ex) { ed.WriteMessage("Error occured: " + ex.Message + "\n" + ex.StackTrace); } } ed.WriteMessage("Done at " + DateTime.Now + "\n"); } catch(System.Exception ex) { ed.WriteMessage("Error occured: " + ex.Message + "\n" + ex.StackTrace); } }
Solved! Go to Solution.
Solved by schwabg. Go to Solution.