vb.net - GetActiveObject from The Excel Worbook -


i have datepicker written in vb.net looks this.

enter image description here

i had way because of security settings have. because of that, build in datepicker excel generate error.

the date picker works fine if there only 1 excel application open. problem when there multiple excel applications open.

enter image description here

the code takes first excel application, want excel workbook that's called "test".

i think problem statement:

objexcel = system.runtime.interopservices.marshal.getactiveobject("excel.application") 

for better understanding code excel , datepicker application below:

that's how open vb.net application in excel:

set wsh = vba.createobject("wscript.shell") arg = activeworkbook.path & ";" & activeworkbook.name & ";" & activesheet.name & ";" & target.address ' wait shelled application finish: errorcode = wsh.run(strpathdatepicker & " " & arg, windowstyle, waitonreturn) 

the code datepciker is:

public sub frmdatepicker_load(sender object, e eventargs) handles mybase.load     dim par() string     dim strtemp string      try         dim arg = environment.getcommandlineargs         strtemp = arg(1)          par = split(strtemp, ";")          'split arg, information excel workbook         strwbpath = par(0)         strwbname = par(1)                     strwsname = par(2)                     stradresse = par(3)                  catch ex exception                     me.close()     end try end sub 

if click on date vb.net application run code inserting selected date excel:

private sub monthcalendar1_dateselected(sender object, e daterangeeventargs) handles monthcalendar1.dateselected             dim objexcel excel.application     dim wb excel.workbook     dim ws excel.worksheet      try         'get excel object         objexcel = system.runtime.interopservices.marshal.getactiveobject("excel.application")           each wb in objexcel.workbooks             if wb.name = strwbname                 ws = wb.sheets(strwsname)                 ws.range(stradresse.tostring).value = e.start()                 me.close()             end if          next     catch ex exception         messagebox.show(ex.message)         me.close()     end try  end sub 

the usual ways pass process id, or application.hwnd, or risky activewindow.caption, think full external address enough find excel instance. example in vb.net (not tested):

dim o object = getobject("book 1.xls") dim wb excel.workbook = trycast(o, excel.workbook) 

note ; valid character in file, workbook, worksheet, , named range names recommend looking different separator. example non-printable characters chr(0) might work, or use full external address target.address(,,,1).

environment.getcommandlineargs(1) cause problems if argument contains spaces , not surrounded ": https://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -