c# - How can I open a Microsoft Word document from within a winforms .exe app? -


i have winforms app utilizes dll (docx) create .docx document stringbuilder. i'm trying open document microsoft word (the default program) button click. tried folowing code keep getting errors. can point me in right direction accomplish this?

private void button3_click(object sender, eventargs e)     {         var x = "";         using (docx document = docx.create("testdocx.docx"))         {             document.margintop = 25f;             document.marginbottom = 25f;             document.marginleft = 25f;             document.marginright = 25f;             paragraph p = document.insertparagraph();             fontfamily fontfamily = new fontfamily("courier new");              p.append(sb.tostring()).font(fontfamily).fontsize(8); //where "sb" stringbuilder             document.save();             x = environment.currentdirectory;         }         processstartinfo startinfo = new processstartinfo();         startinfo.filename = @"c:\program files (x86)\microsoft office\office12\winword.exe";         startinfo.arguments = x + "\\testdocx.docx";         startinfo.useshellexecute = true;         process.start(startinfo);     } 

your approach hard-codes path winword. while may work case, inflexible , brittle.

you can instead do

process.start(x + "\\testdocx.docx"); 

that find default document handler .docx files (which winword, assuming installed , have not installed else handles .docx files).


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -