How do you pass parameters from a VBA script in excel to an external executable (C#) as arguements? -
i have embedded button in excel sheet calls vba script. in script reading current directory, parsing, , using generate values pass string[] args external c# executable. have gone through many iterations of , call executable, appears parameters being passed null(empty) when c# .exe runs. able use c# .exe in other programs , pass arguments, vba script isn't working expected. know passing correct # of parameters don't out of bounds exception c# .exe. converting these arguments strings in attempt troubleshoot this, isn't necessary. vba code attached:
sub buttonsg1b7_click() dim filelocation dim programname dim length filelocation = activeworkbook.fullname length = len(filelocation) - 5 programname = left(filelocation, length) programname = right(programname, 10) length = len(filelocation) - 15 filelocation = left(filelocation, length) length = len(filelocation) - 2 filelocation = right(filelocation, length) msgbox "file location : " & filelocation & " program name: " & programname dim str0 string dim str1 string dim str2 string dim str3 string dim str4 string dim str5 string dim str6 string str0 = "h:\stagegate\administration\scripts\getlatestfileopen.exe " str1 = "h:" str2 = filelocation str3 = "common" str4 = "\market feasibility " str5 = programname str6 = ".xlsx" msgbox str0 & str1 & str2 & str3 & str4 & str5 & str6 shell (str0 & str1 & str2 & str3 & str4 & str5 & str6) end sub edit(adding prelim c# code involving solidworks epdm library takes arguements, turns list, , uses list provide string filepath latest on folder in epdm vault , open updated local copy of file.) :
using system; using system.diagnostics; using system.windows.forms; using system.collections.generic; using system.linq; using system.io; using epdm.interop.epdm; class program { static void main(string[] args) { iedmfolder5 pporetparentfolder; list<string> filepath = new list<string>(args); if (filepath.any()) { filepath.removeat(0); //really need stop having script call passing argument. } if (directory.exists(@"c:\stagegate")) { filepath.insert(0,"c:"); } else if (directory.exists(@"h:\stagegate")) { filepath.insert(0,"h:"); } else { messagebox.show("stagegate not found."); } string newfilepath = string.join("", filepath.toarray()); filepath.removeat(5); filepath.removeat(4); filepath.removeat(3); string folderpathstr = string.join("", filepath.toarray()); //messagebox.show(folderpathstr); //have create vault object work batchget edmvault5 vault = new edmvault5(); //replace my_vault vault name vault.loginauto("stagegate", 0); //set 2 here equal how many folders want (not counting subfolders) edmselitem[] folderarray = new edmselitem[1]; iedmbatchget bg = (iedmbatchget)vault.createutility(edmutility.edmutil_batchget); //create , array element eachf older want get, replace folder locations folders folderarray[0].mldocid = 0; folderarray[0].mlprojid = vault.getfolderfrompath(folderpathstr).id; //fa[1].mldocid = 0; //fa[1].mlprojid = vault.getfolderfrompath("c:\\my_vault\\folderpath").id; bg.addselection(vault, folderarray); bg.createtree(0, (int)edmgetcmdflags.egcf_includeautocachefiles); //egcf_includeautocachefiles latest version of file bg.getfiles(0, null); if (file.exists(newfilepath)) { process process = process.start(newfilepath); //can't open file. need create new windows process have file open in default application(process) //file.open(newpath, filemode.open); } else { messagebox.show("file not found."); }
after reading c# code, see code expects @ least 6 arguments
this vba code escapes spaces might appear in path , passes them command-line arguments:
sub buttonsg1b7_click() dim filelocation dim programname dim length filelocation = activeworkbook.fullname length = len(filelocation) - 5 programname = left(filelocation, length) programname = right(programname, 10) length = len(filelocation) - 15 filelocation = left(filelocation, length) length = len(filelocation) - 2 filelocation = right(filelocation, length) msgbox "file location : " & filelocation & " program name: " & programname dim args(0 6) string dim cmdln string, integer args(0) = "h:\stagegate\administration\scripts\getlatestfileopen.exe" args(1) = "h:" args(2) = filelocation args(3) = "common" args(4) = "\market feasibility " args(5) = programname args(6) = ".xlsx" cmdln=arg(0) = 1 6 cmdln=cmdln & " """ & args(i) & """" next msgbox "vba code writes: " & cmdln shell (cmdln) end sub now c# code should read parameters:
class program { static void main(string[] args) { console.writeline("c# code reads: "+string.join(" ", args)); } } if 2 proccesses return same command-line string, exceptions may encounter in following lines not related vba-c# communications
Comments
Post a Comment