2

I have a Winforms app that utilizes a dll (docX) to create a .docx document from a StringBuilder. I'm trying to open that document with Microsoft Word (the default program) with a button click. I tried the folowing code but I keep getting errors. Can someone point me in the right direction to 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" is a 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);
    }
MirrorEyes
  • 57
  • 4

3 Answers3

1

Your approach hard-codes the path to WINWORD. While this may work for your case, it is inflexible and brittle.

You can instead simply do

Process.Start(x + "\\Testdocx.docx");

That will find the default document handler for .docx files (which is Winword, assuming it is installed and you have not installed anything else that handles .docx files).

Eric J.
  • 147,927
  • 63
  • 340
  • 553
1

Just change 3 lines in your code. Your problem will be solved

here...

using (DocX document = DocX.Create(Application.StartupPath + "\\Testdocx.docx"))

here

document.Save();
x = Application.StartupPath;

. and here

startInfo.Arguments = "\"" + x + "\\Testdocx.docx\"";  // -> Quotes on either sides

.

. Also I think you dont need to give full path for Word. Just do

 startInfo.FileName = "WINWORD.EXE";

Or even just

 startInfo.FileName = "WINWORD";
Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45
  • Thank you I tried all your suggestions. Now it's opening the Word Program but it gives me an error loading the document- "problem with the contents". Also, it only works from within Visual Studio, not when I try executing from an external exe. – MirrorEyes Mar 17 '15 at 04:48
  • can you show what you've put on your StringBuilder? – Abdul Saleem Mar 17 '15 at 04:49
  • I tried with some sample String like Welcome to Stackoverflow, and there wasn't any error – Abdul Saleem Mar 17 '15 at 04:50
  • It's basically a whole bunch of appended strings that are variables taken from a list of strings. Like this:sb.Append(Spacer(7) + list[2] + "\r\n\r\n"); //landlord sb.Append(Spacer(7) + list[3]); //landlord address sb.Append(Spacer(60 - (list[3].Length + 7)) + list[4] + "\r\n"); //affixed on premises date sb.Append(Spacer(8) + list[5]); //city – MirrorEyes Mar 17 '15 at 04:52
  • Might be some unsupported character or something. Have you tried with some simple string? – Abdul Saleem Mar 17 '15 at 05:02
  • Have you put quotes between the filename as said above? – Abdul Saleem Mar 17 '15 at 05:03
  • Also test it with C:\TestDocx.docx – Abdul Saleem Mar 17 '15 at 05:04
  • I just noticed that this question on stackoverflow is concerning the same error message: http://stackoverflow.com/questions/17675526/how-can-i-modify-the-foreground-and-background-color-of-an-openxml-tablecell – MirrorEyes Mar 17 '15 at 05:05
  • 1
    I think this answer is eligible for an upvote by giving you some knowledge – Abdul Saleem Mar 17 '15 at 09:11
1

Arguments passed includes : Application , location

 Process.Start("winword.exe", "C:\\you path here \\filename.docx");
Tharif
  • 13,794
  • 9
  • 55
  • 77