[英]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.