VB.net manage opened excel file on the fly -
in current project, need modify excel file opened. mean, need see change in excel file whenever modify it.
since opened file locked , vb cannot access file, solution open excel file, modify content , copy original file using excel vba, excel vba can used copy data between opening sheets.
here vb code in other excel file (called ttht.xlsm):
private sub worksheet_change(byval target range) application.screenupdating = false if sheet1.[b5].value = "#" application.displayalerts = false workbooks.open(thisworkbook.path & "\pcn.xlsm") thisworkbook.sheets("ttht").[b5] .parent.range("b4").copy sheets("pcn").[b4].pastespecial 7 end end application.displayalerts = true end if end sub
runs fine between 2 opening excel file ( ttht 1 , pcn.xlsm ).
now need do, fill data ttht, , put "#" b5 code. think work, it's not..
private sub btnupdate_click(sender system.object, e system.eventargs) handles btnupdate.click dim oexcel object dim obook object dim osheet object oexcel = createobject("excel.application") oexcel.displayalerts = false obook = oexcel.workbooks.open(tthtpath) osheet = obook.worksheets("ttht") osheet.range("b4").value = txtvalue_needed.text osheet.range("b5").value = "#" obook.save() 'system.diagnostics.process.start(duongdanphieucongnghe) osheet = nothing obook.close() obook = nothing oexcel.quit() oexcel = nothing end sub
result is, if open pcn file , click button, won't change. if close pcn , click button, change.. need see change on fly, not close , reopen each time need update. can shed me light, how, or function ... either vba or vb.net, should use ?
if file opened , has focus can try looking in running objects table.
dim excelapplication excel.application excelapplication = getobject("c:\folder\excelworkbook.xls")
then can make modifications on instance referenced while trying avoid disrupting user.
the getobject() function great; here's link. https://msdn.microsoft.com/en-us/library/e9waz863(v=vs.71).aspx
Comments
Post a Comment