简体   繁体   English

无法运行宏xx。 可能在此工作簿中不可用或所有宏都被禁用

[英]Can not run the macro xx. may not be available in this workbook or all macros are disabled

I am trying to execute the following code 我正在尝试执行以下代码

const string excelFile = @"C:\test.xls";

                var excelApplication = new ExcelInterop.Application { Visible = true };
                var targetExcelFile = excelApplication.Workbooks.Open(excelFile,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
                var newStandardModule = targetExcelFile.VBProject.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule);
                var codeModule = newStandardModule.CodeModule;

                // add vba code to module
                var lineNum = codeModule.CountOfLines + 1;
                var macroName = "test";
                var codeText = " Sub " + macroName + "()" + "\r\n";
                codeText += " Dim xsheet As Worksheet" + "\r\n";
                codeText += "For Each xsheet In ThisWorkbook.Worksheets" + "\r\n";
                codeText += " xsheet.Select" + "\r\n";
                codeText += "With xsheet.UsedRange" + "\r\n";
                codeText += "    .Value = .Value" + "\r\n";
                codeText += "  End With" + "\r\n";
                codeText += "Next xsheet    " + "\r\n";
                codeText += "End Sub";

                codeModule.InsertLines(lineNum, codeText);
                targetExcelFile.Save();

                // run the macro
                var macro = string.Format("{0}!{1}.{2}", targetExcelFile.Name, newStandardModule.Name, macroName);
                excelApplication.Run(macro,
Type.Missing, Type.Missing, Type.Missing,  
Type.Missing, Type.Missing, Type.Missing, 
Type.Missing, Type.Missing, Type.Missing,  
Type.Missing, Type.Missing, Type.Missing, 
Type.Missing, Type.Missing, Type.Missing,  
Type.Missing, Type.Missing, Type.Missing, 
Type.Missing, Type.Missing, Type.Missing,  
Type.Missing, Type.Missing, Type.Missing, 
Type.Missing, Type.Missing, Type.Missing,  
Type.Missing, Type.Missing, Type.Missing
);
                excelApplication.Quit();

I always got the following error 我总是遇到以下错误

"Can not run the macro xx. may not be available in this workbook or all macros are disabled." “无法运行宏xx。在此工作簿中可能不可用,或者所有宏都被禁用。”

How to resolve the error? 如何解决错误? Any suggestions ? 有什么建议么 ?

I just modified your code little bit and it works just fine 我刚刚修改了一点代码,就可以了

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using VBIDE = Microsoft.Vbe.Interop;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlexcel;
            Excel.Workbook xlWorkBook;
            VBIDE.VBComponent newStandardModule;
            VBIDE.CodeModule codeModule;

            object misValue = System.Reflection.Missing.Value;

            const string excelFile = @"C:\Users\Siddharth\Desktop\Sid.xlsm";

            xlexcel = new Excel.Application();
            xlexcel.Visible = true;

            // Open a File
            xlWorkBook = xlexcel.Workbooks.Open(excelFile, misValue, 
                         misValue, misValue, misValue, misValue, misValue,
                         misValue, misValue, misValue, misValue, misValue, 
                         misValue, misValue, misValue);

            newStandardModule = xlWorkBook.VBProject.VBComponents.Add(
                                VBIDE.vbext_ComponentType.vbext_ct_StdModule);

            codeModule = newStandardModule.CodeModule;

            // add vba code to module
            var lineNum = codeModule.CountOfLines + 1;

            var macroName = "test";

            var codeText = "Sub " + macroName + "()" + "\r\n";
            codeText +=    "    Dim xsheet As Worksheet" + "\r\n";
            codeText +=    "    For Each xsheet In ThisWorkbook.Worksheets" + "\r\n";
            codeText +=    "        xsheet.UsedRange.Value = xsheet.UsedRange.Value" + "\r\n";
            codeText +=    "    Next xsheet" + "\r\n";
            codeText +=    "End Sub";

            codeModule.InsertLines(lineNum, codeText);
            xlWorkBook.Save();

            // run the macro
            var macro = string.Format("{0}!{1}.{2}", xlWorkBook.Name, newStandardModule.Name, macroName);
            xlexcel.Run(macro,misValue, misValue, misValue,misValue, misValue, misValue,
            misValue, misValue, misValue, misValue, misValue, misValue,   misValue, misValue, misValue,
            misValue, misValue, misValue, misValue, misValue, misValue,  misValue, misValue, misValue,
            misValue, misValue, misValue, misValue, misValue, misValue);
            xlexcel.Quit();
        }
    }
}

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

相关问题 使用Application调用自定义函数时出现BERT错误。运行VBA“此工作簿中的宏可能不可用,或者所有宏都可能被禁用。” - BERT error while calling a custom function with Application.Run VBA “The macro may not be available in this workbook or all macros may be disabled.” NHAPI 2.5.1 xx。(0)。组件值,如何使用 - NHAPI 2.5.1 xx.(0).Components values, how to use 通过C#运行Excel宏:从另一个工作簿运行宏? - Running an Excel Macro via C#: Run a macro from one workbook on another? 由于类型xx不可用,因此未加载关系xx - The relationship xx was not loaded because the type xx is not available 将数据和带有宏的按钮复制到另一个工作簿 - Copy data and a button with macros to another workbook 在 c# 中创建了 Excel 工作簿,但图表被禁用? - Created an Excel workbook in c# but charting is disabled? 在代码生成的Excel应用程序和工作簿中看不到Personal Workbook中的Excel宏 - Excel macro in Personal workbook not visible in code generated Excel application and workbook 从工作簿宏调用Excel UDF - Invoke Excel UDF from Workbook Macro C#:工作簿另存为,其中包含宏 - C#: Workbook SaveAs saving with macro in it 使用C#检查Excel工作簿是否具有宏 - Check whether an Excel workbook has macros using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM