简体   繁体   English

如何使用C#替换excel文件中的代码模块

[英]How do I replace the code module in excel file with c#

I have to replace the code module in a number of existing excel files. 我必须在许多现有的excel文件中替换代码模块。 Instead of opening each file, removing the VB Module and importing a new VB Module, and then saving it manually I tried to write a small program to do the work. 我没有打开每个文件,而是删除了VB模块并导入了新的VB模块,然后手动保存了它,我尝试编写一个小程序来完成这项工作。 It will prove useful in the future. 它将在将来证明有用。 But only if I can make it work! 但是只有在我可以使它起作用的情况下! The new VB Module is a .bas file that I exported from another version of the workbook. 新的VB模块是我从工作簿的另一个版本导出的.bas文件。

Here is the C# code I tried. 这是我尝试过的C#代码。

        foreach (FileInfo file in temp)
        {
            wkbk =app.Workbooks.Open(Filename:file.FullName);
            VBProject proj = wkbk.VBProject;
            foreach (VBComponent comp in proj.VBComponents)
            {
                    if(comp.Name=="VBModule") 
                        proj.VBComponents.Remove(comp);
            }
            proj.VBComponents.Import(newFile);
            wkbk.Close(SaveChanges:true, Filename:file.FullName);
            ReleaseObject(wkbk);
        }

Yes, the name of the module is VBModule. 是的,模块的名称是VBModule。 temp is an ObservableCollection of FileInfo objects. temp是FileInfo对象的ObservableCollection。 While stepping through, this program hits every line without an error. 在逐步执行时,该程序会正确地击中每一行。 I just doesn't do anything. 我什么也没做。 I can examine the variables and they are exactly as intended. 我可以检查变量,它们完全符合预期。 I assume that the Save is not working so that when I examine the file it is the same as it was before running this program. 我认为保存无法正常工作,因此当我检查文件时,该文件与运行该程序之前的文件相同。

Thank you for looking at this. 感谢您的关注。

So the answer is in the code above. 因此答案就在上面的代码中。 I had it right. 我说对了。 The file I was trying to import "VBModule_Hold.bas" was being imported. 我尝试导入的文件“ VBModule_Hold.bas”正在导入。 But in Excel, when you examine the Modules it is only called VBModule. 但是在Excel中,当您检查模块时,它仅称为VBModule。 I named it that originally a couple years ago when I inserted the new module and started writing code. 我最初是几年前命名的,当时我插入了新模块并开始编写代码。 I had to make some updates that I wanted to apply to past files as well so I made the changes, exported the code module with a new name and wrote this little routine to update all the old excel files. 我还必须进行一些更新,也想将这些更新应用于过去的文件,因此我进行了更改,使用新名称导出了代码模块,并编写了这个小例程来更新所有旧的excel文件。 When I examined a file after running the routine, it appeared that the module had not been removed and replaced because it was named VBModule. 当我在运行例程后检查文件时,该模块似乎没有被删除和替换,因为它被命名为VBModule。 I assumed nothing had been changed. 我以为一切都没有改变。 I suppose it has to do with the _ in the file name. 我想这与文件名中的_有关。 Anyway, I edited my "Hold" module with an obvious comment at the top. 无论如何,我编辑了“保持”模块,并在顶部添加了明显的注释。 I ran the code, opened the Excel file and examined the module. 我运行代码,打开Excel文件并检查了模块。 There was my comment! 有我的评论! My thanks to the responses I got. 我感谢收到的答复。 Jazimov made me look at the file in a different way that led me to the answer. Jazimov让我以另一种方式查看文件,这使我找到了答案。 I saved it as "NewFile.xlsm" and there it was. 我将其另存为“ NewFile.xlsm”。 Thank you both, again. 再次谢谢你们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何改进代码(C#)以打开指定的 excel 文件而不仅仅是 excel 应用程序 - How do I improve the code (C#) to open the specified excel file and not only the excel application 如何用代码替换面板上的背景图像? C# - How do I replace a background image on a panel in code? C# 如何在C#中替换CSS文件中的值? - How do I replace values in CSS file in C#? 如何单击按钮并通过C#获取excel文件? - How do I click button and get excel file by C#? 如何将C#excel导出中的复选框替换为1和0? - How do I replace the checkboxes in my C# excel export to ones and zeros? 如何从C#访问Excel VBA中的类模块? - How do I access a class module in Excel VBA from C#? 如何使用 iTextsharp 和 C# 在不破坏文件的情况下删除/替换 PDF 文件中的图像 - How do I delete/replace an image in a PDF file without breaking the file, using iTextsharp and C# 如何在我的C#代码中从HTML网格下载到excel或CVS文件 - How to do download from html grid to excel or CVS file in my c# code 如何在我的 Visual Studio Code c# 项目中设置默认的 c# 文件? - How do I set the default c# file in my Visual Studio Code c# Project? 如何使用 C# 代码在 Excel 中的多个电子表格中拆分数据? - How do I split data across multiple spreadsheets in Excel using C# code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM