VB.Net working with ActiveDocument in an already running instance of word -
i have small vb.net application opens new word document template (opens document1.docx example). runs code find replace text.
it allows user edit open document. there small winform open button called 'complete'. when user clicks this, want save document , further editing etc.
however, cannot code work see active document in running instance of word.
whenever refer activedocument, throws , exception saying there no documents open.
the code looking save active document in separate sub word.application object created. word running document open still fails.
i've tried following make sure getting instance of word open; wordapp = marshal.getactiveobject("word.application")
but still not see open documents.
any suggestions?
edit #1 - sorry not including code;
this code starts word , basic find/replace. starts form3 stays in place whilst user edits word document. code runs button click on form 2;
dim oword word.application dim odoc word.document dim path string path = "c:\templates/" try oword = createobject("word.application") oword.visible = true catch ex exception ''some error handling code not included purpose of stackoverflow end try try odoc = oword.documents.add(path & "mg11.dot") catch ex exception ''some error handling code not included purpose of stackoverflow end try oword.selection.find ''find & replace code - works not included stackoverflow end form3.show() me.windowstate = formwindowstate.minimized form1.windowstate = formwindowstate.minimized '(form 1 has no code effects word) end sub
form 3 has 1 button, when pressed should save word document. give filename based on info elsewhere. basic code shown here - need vb.net see active document now. code ran on button_1 click form 3.
dim wordapp word.application dim odoc word.document = wordapp.activedocument
odoc = wordapp.activedocument
i've tried;
dim wordapp word.application wordapp = marshal.getactiveobject("word.application") dim odoc word.document odoc = wordapp.activedocument
'once odoc set use saveas2 handle saving etc
it fails on wordapp.activedocument, stating there no open documents.
either make sure oword in scope in form3. might in scope or add public word.applications property form3 , set property before showing form.
in first case access activedocument on existing oword object in scope, in second set property on form , access before showing.
oword.activedocument
Comments
Post a Comment