C# / VBA

c# execute word macro vba

c# code word

Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wordDocument = wordApp.Documents.Open(@"test.doc");
wordApp.Visible = false;
string[] oRunArgs = { "pdfFactory Pro", "a", "b", "c", "d" };
wordApp.Run("echoTest01", oRunArgs);
wordDocument.Close(false);
wordApp.Quit();

vba code

Public Sub echoTest01(val01)

    With Selection.Find
        .Text = "@name"
        .Replacement.Text = val01(1)
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    WordBasic.FilePrintSetup Printer:=val01(0), DoNotSetAsSysDefault:=1
    PrintOut
    
End Sub

c# code excel

主機要安裝office並加入參考

Dts.TaskResult = (int)ScriptResults.Success;
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

excelApp.Visible = false;
excelApp.DisplayAlerts = false;
Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.Workbooks.Open(@"C:\test\aaa.xls", 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);

excelApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, excelApp, new Object[] { "宏名稱" });
//excelApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, excelApp, new Object[] { "宏名稱", "參數" });

workbook.Close(false, Type.Missing, Type.Missing);
excelApp.Quit();
GC.Collect();

#如果vba自動化過程open出現權限不足,可嘗試增加以下資料夾
曾發生ssis設計介面正常work,但在sql agent發生錯誤
C:\Windows\SysWOW64\config\systemprofile\Desktop

#office 2007另存pdf套件
SaveAsPDFandXPS.exe

#sql agent呼叫方式(asp)

if request.QueryString("fname") = "spc32_sqlAgent_ef_coa" then
  set conn=Server.CreateObject("ADODB.Connection")
  connstr = "Provider=SQLOLEDB.1;Data Source=192.168.123.123;User ID=user;Password=pwd;Initial Catalog=Cylinder"
  conn.open connstr
  sql = " msdb..sp_start_job ef_coa; "
  conn.execute (sql)
  conn.Close()
end if

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。