【发布时间】:2019-01-10 08:15:30
【问题描述】:
使用与Run an Excel Macro from SSIS类似的逻辑
我的Script任务逻辑在SSIS中如下,
public void Main()
{
// TODO: Add your code here
string filename;//= Dts.Variables["User::filename"].Value.ToString();
filename = @"D:\VersionWithDummyData\finbalReviewTest.xlsm";
string S_Directory = @"D:\VersionWithDummyData\";
if (S_Directory.Substring(S_Directory.Length - 1, 1) != @"\")
{
S_Directory = S_Directory + @"\";
}
DirectoryInfo finfo = new DirectoryInfo(S_Directory);
if (filename.ToString().Substring(1, 2) != "~$")
{
try
{
xls.Application ExcelObj = new xls.Application();
ExcelObj.DisplayAlerts = true;
ExcelObj.Visible = true;
ExcelObj.DefaultFilePath = S_Directory;
xls.Workbook eBook = ExcelObj.Workbooks.Open(filename.ToString(), false, false,
Type.Missing, "", "", true, xls.XlPlatform.xlWindows, "",
false, false, 0, false, true, 0);
foreach (xls.WorkbookConnection wc in eBook.Connections)
{
if (wc.Type.ToString() == "xlConnectionTypeODBC")
{
wc.ODBCConnection.BackgroundQuery = false;
}
else
{
wc.OLEDBConnection.BackgroundQuery = false;
}
}
eBook.RefreshAll();
eBook.Save();
ExcelObj.Run("Module1",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);
eBook.Save();
ExcelObj.Workbooks.Close();
ExcelObj.Quit();
}
catch (COMException e)
{
MessageBox.Show(filename.ToString() + " has an issue with error " + e.Message);
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
excel(.xlsm)中可用的宏如下
Sub fileSave()
'
Dim newFileName As String, originalFileName As String, fileSaveName As String, fileNamePathSaved As String, fileNameSaved As String
Dim response As VbMsgBoxResult, currentRoute As String
'
ThisWorkbook.RefreshAll
ActiveWorkbook.Save ' save the current workbook before messing with it
Application.DisplayAlerts = False ' turns off alerts and messages
' Save file name and path into a variable
originalFileName = ActiveWorkbook.FullName ' gets the fullname with path
' originalFilePath = ActiveWorkbook.Path ' grabs the current path
Dim usingReplace As String
usingReplace = Replace(originalFileName, ".xlsm", ".xlsx")
ActiveWorkbook.SaveAs Filename:=usingReplace, FileFormat:=xlOpenXMLWorkbook
fileNameSaved = ActiveWorkbook.Name ' grabs the name of the saved file
Workbooks.Open Filename:=originalFileName 'reopens the original workbook file
Application.DisplayAlerts = True ' turns the alerts and messages back on
'provide an opportinity to clear the incident report flag
' If incidentFiled = True Then response = MsgBox("Do you want to clear the Incident Report?", vbInformation + vbOKCancel, "Incident Report Form")
If response = vbOK Then incidentFiled = False
'close the newly made file
' Workbooks(fileNameSaved).Close True ' sub terminates at this point
'
End Sub
上面的宏保存为Module1,当我尝试运行包时,数据正在刷新但宏没有被执行
注意:尝试了类似帖子中建议的所有解决方案
仍然是错误发生了错误
重要:
*尝试从 excel-->developer-->visual***-->Module1-->执行时的宏,然后按预期工作。*
【问题讨论】:
-
不是真正的解决方案,而是一种解决方法:如果将宏重命名为
Sub Auto_open(),它将在文件打开时执行。 -
您可以将其与最后的
ThisWorkbook.Saved = True和Application.Quit配对以使其也关闭excel -
如果您将
Auto_open包装在一个检查Application.Visible = False的If 中,您可以在文件被用户打开时阻止宏运行 -
逻辑上一切正常,但是当我将宏合并到包中时,问题就会出现......我会尝试将宏写为 Auto_open() 并尽快发布 cmets
-
点赞this