|
楼主 |
发表于 2015-11-24 13:45
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 VBA万岁 于 2015-11-24 14:19 编辑
故用“Workbook book = app.Workbooks.Open(f.FullName.Replace("~$", ""));”这一句代码可避免此错误,从而解决这个问题,运行程序可达到预期目的:
- <P>private void mb2_Click(object sender, RibbonControlEventArgs e)
- {
- fn判断工作表是否被保护(Pa.SuperTip.Substring(0, Pa.SuperTip.LastIndexOf("\") + 1));
- }</P>
- <P> private static void fn判断工作表是否被保护(string v_strDir)
- {
- String msg = "";
- int n = 0;
- Application app = new Application();
- try
- {
- foreach (FileInfo f in new DirectoryInfo(v_strDir).GetFiles("*.xls*"))
- {
- {
- Workbook book = app.Workbooks.Open(f.FullName.Replace("~$", ""));
- msg = msg + book.Name + "受保护的工作表有:\n";
- foreach (Worksheet sheet in book.Worksheets)
- {
- if (book.Worksheets[sheet.Name].ProtectContents == true)
- {
- msg = msg + sheet.Name + "\n";
- n++;
- }
- }
- if(n==0)
- {
- msg = msg.Replace(book.Name + "受保护的工作表有:\n", "");
- }
- n = 0;
- app.Application.DisplayAlerts = false;
- //book.Close();
- }
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- app.Quit();
- System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
- app = null;
- GC.Collect();
- }</P>
- <P> System.Windows.Forms.MessageBox.Show(msg);
- }</P>
复制代码
附件如下:
|
|