不能运行宏xx。此工作簿中可能不可用或所有宏都已禁用
Can not run the macro xx. may not be available in this workbook or all macros are disabled
我正在尝试执行以下代码
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();
我总是得到以下错误
"Can not run the macro xx. may not be available in this workbook or all
macros are disabled."
如何解决错误?有什么建议吗?
我只是稍微修改了你的代码,它工作得很好
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();
}
}
}
我正在尝试执行以下代码
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();
我总是得到以下错误
"Can not run the macro xx. may not be available in this workbook or all macros are disabled."
如何解决错误?有什么建议吗?
我只是稍微修改了你的代码,它工作得很好
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();
}
}
}