Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with Process.Start("WINWORD.EXE", selectedFile); [modified]

Problem with Process.Start("WINWORD.EXE", selectedFile); [modified]

Scheduled Pinned Locked Moved C#
csharphelphtmlvisual-studiodebugging
19 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ _Q12_

    Problem with Process.Start("WINWORD.EXE", selectedFile); I even put the entire path there: //string MSWordPath = @"c:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE"; Still same result: the path and file name is: c:\Users\xxx\Documents\Visual Studio 2010\Projects\zMAP\bridgeWord-Html\bridgeWord-Html\bin\Debug\01 SettingsForVSC#.docx The MSWord is lunched but these errors are thrown: Microsoft Word This file could not be found! (c:\Users\xxx\Documents\Visual.doc) (c:\Users\xxx\Documents\...\Studio.doc) (c:\Users\xxx\Documents\...\01.doc) (c:\Users\xxx\Documents\...\Studio.doc) (c:\Users\xxx\Documents\...\SettingsForVSC#.doc) And then an empty msword page is open. [I see it breaks at the spaces in between names] ---How to fix this problem? I wish to open not only a docx file, but a html file also,or a txt file. thank you.

    string path = Application.StartupPath;
    private void button3_Click(object sender, EventArgs e)
    {
    string MSWordPath = "\"c:\\Program Files (x86)\\Microsoft Office\\Office14\\WINWORD.EXE\"";
    selectedFile = path + "\\" + listBox1.SelectedItem as string;
    //Process.Start("WINWORD.EXE", selectedFile); -or this but the same problem
    Process.Start(MSWordPath, selectedFile);
    }

    modified on Friday, May 20, 2011 1:13 PM

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #2

    Enclose the entire path to all filepaths, even your winword.exe, in double quotes. Your command line would end up looking like this:

    "C:\\Program Files (x86)\\Microsoft Office\\Office14\\WinWord.exe" "C:\\Users\\xxxx\\Documents\\Visual Studio 2010\\...\\01SettingsForVSC#.doc"
    

    The quotes MUST be part of the string, otherwise any spaces in the path will deliniate a parameter for the command line.

    A guide to posting questions on CodeProject[^]
    Dave Kreskowiak

    _ 1 Reply Last reply
    0
    • D Dave Kreskowiak

      Enclose the entire path to all filepaths, even your winword.exe, in double quotes. Your command line would end up looking like this:

      "C:\\Program Files (x86)\\Microsoft Office\\Office14\\WinWord.exe" "C:\\Users\\xxxx\\Documents\\Visual Studio 2010\\...\\01SettingsForVSC#.doc"
      

      The quotes MUST be part of the string, otherwise any spaces in the path will deliniate a parameter for the command line.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      _ Offline
      _ Offline
      _Q12_
      wrote on last edited by
      #3

      It worked... THANK YOU Dave !!!

      string MSWordPath = "\"c:\\Program Files (x86)\\Microsoft Office\\Office14\\WINWORD.EXE\"";
      selectedFile = "\"" + path + "\\" + listBox1.SelectedItem as string + "\"";
      // Process.Start(MSWordPath, selectedFile);
      Process.Start("WINWORD.EXE", selectedFile);

      L D 2 Replies Last reply
      0
      • _ _Q12_

        It worked... THANK YOU Dave !!!

        string MSWordPath = "\"c:\\Program Files (x86)\\Microsoft Office\\Office14\\WINWORD.EXE\"";
        selectedFile = "\"" + path + "\\" + listBox1.SelectedItem as string + "\"";
        // Process.Start(MSWordPath, selectedFile);
        Process.Start("WINWORD.EXE", selectedFile);

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #4

        Assuming Word is installed and is the default app to process .doc files, there is a much simpler alternative:

        Process.Start(myDocFilePath);

        This is equivalent to double-clicking "myDocFilePath" inside Windows Explorer, it doesn't care about spaces, and it doesn't need to know where your Word app is and what it is actually called. :) BTW: what you wrote earlier, then removed, wasn't correct; a partial success is not a proof of correctness!

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        _ 1 Reply Last reply
        0
        • _ _Q12_

          It worked... THANK YOU Dave !!!

          string MSWordPath = "\"c:\\Program Files (x86)\\Microsoft Office\\Office14\\WINWORD.EXE\"";
          selectedFile = "\"" + path + "\\" + listBox1.SelectedItem as string + "\"";
          // Process.Start(MSWordPath, selectedFile);
          Process.Start("WINWORD.EXE", selectedFile);

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #5

          You know, (you deleted what you originally replied with!) just because it worked with Notepad before does NOT mean that you did it correctly. Even in this latest code, I can see HUGE problems with it. You're code is making some rather large assumptions that will break it if run on another system that isn't identically configured with the exact Office installed in the exact same way it is on your machine.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          _ 1 Reply Last reply
          0
          • L Luc Pattyn

            Assuming Word is installed and is the default app to process .doc files, there is a much simpler alternative:

            Process.Start(myDocFilePath);

            This is equivalent to double-clicking "myDocFilePath" inside Windows Explorer, it doesn't care about spaces, and it doesn't need to know where your Word app is and what it is actually called. :) BTW: what you wrote earlier, then removed, wasn't correct; a partial success is not a proof of correctness!

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            _ Offline
            _ Offline
            _Q12_
            wrote on last edited by
            #6

            Yes I know, that putting only the fileName+path, that file is opening with the default app. But what I really want is to open a html file and edit it with msword. If you have other alternative than: Process.Start(myDocFilePath); please tell. Though, thank you for your fast response... :)

            D L 2 Replies Last reply
            0
            • _ _Q12_

              Problem with Process.Start("WINWORD.EXE", selectedFile); I even put the entire path there: //string MSWordPath = @"c:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE"; Still same result: the path and file name is: c:\Users\xxx\Documents\Visual Studio 2010\Projects\zMAP\bridgeWord-Html\bridgeWord-Html\bin\Debug\01 SettingsForVSC#.docx The MSWord is lunched but these errors are thrown: Microsoft Word This file could not be found! (c:\Users\xxx\Documents\Visual.doc) (c:\Users\xxx\Documents\...\Studio.doc) (c:\Users\xxx\Documents\...\01.doc) (c:\Users\xxx\Documents\...\Studio.doc) (c:\Users\xxx\Documents\...\SettingsForVSC#.doc) And then an empty msword page is open. [I see it breaks at the spaces in between names] ---How to fix this problem? I wish to open not only a docx file, but a html file also,or a txt file. thank you.

              string path = Application.StartupPath;
              private void button3_Click(object sender, EventArgs e)
              {
              string MSWordPath = "\"c:\\Program Files (x86)\\Microsoft Office\\Office14\\WINWORD.EXE\"";
              selectedFile = path + "\\" + listBox1.SelectedItem as string;
              //Process.Start("WINWORD.EXE", selectedFile); -or this but the same problem
              Process.Start(MSWordPath, selectedFile);
              }

              modified on Friday, May 20, 2011 1:13 PM

              A Offline
              A Offline
              Alan N
              wrote on last edited by
              #7

              It's up the the particular application to interpret the command line in any way it chooses. Notepad is unusual in that it does not require a path containing spaces to be quoted. This is probably because Notepad can take one argument, a filename, and it has been coded to accept spaces within the name. Word has many command line options http://support.microsoft.com/kb/210565[^]and requires that filenames containing spaces are quoted to ensure that they are recognised as one item. The executable path passed to the Process.Start method does not need quoting as it can only ever refer to one file. Alan.

              1 Reply Last reply
              0
              • D Dave Kreskowiak

                You know, (you deleted what you originally replied with!) just because it worked with Notepad before does NOT mean that you did it correctly. Even in this latest code, I can see HUGE problems with it. You're code is making some rather large assumptions that will break it if run on another system that isn't identically configured with the exact Office installed in the exact same way it is on your machine.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                _ Offline
                _ Offline
                _Q12_
                wrote on last edited by
                #8

                I am certain that you guys are right, but for the moment i want to make MY OWN HELPING FRIENDS APPs. :) After that, I will make app for distribution, though i wonder if i will really do that. :)

                1 Reply Last reply
                0
                • _ _Q12_

                  Yes I know, that putting only the fileName+path, that file is opening with the default app. But what I really want is to open a html file and edit it with msword. If you have other alternative than: Process.Start(myDocFilePath); please tell. Though, thank you for your fast response... :)

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #9

                  Editing an HTML file with Word is a horrible idea. When saved, the resulting HTML file will be all but useless to a website as Word adds TONS of crap to the file that just hoses up everything. CP gets a fair number of articles posted that were written in Word and saved in HTML format. You should see the carnage that ensues.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  _ 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    Editing an HTML file with Word is a horrible idea. When saved, the resulting HTML file will be all but useless to a website as Word adds TONS of crap to the file that just hoses up everything. CP gets a fair number of articles posted that were written in Word and saved in HTML format. You should see the carnage that ensues.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak

                    _ Offline
                    _ Offline
                    _Q12_
                    wrote on last edited by
                    #10

                    yes :)) I know that too "Word adds TONS of crap to the resulting HTML". But i dont make it for publication, just for my inner use. So, Ill adapt to its creepiness. If are some hints to make something look good/and right editing with msword, please do tell. thanks.

                    D 1 Reply Last reply
                    0
                    • _ _Q12_

                      Problem with Process.Start("WINWORD.EXE", selectedFile); I even put the entire path there: //string MSWordPath = @"c:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE"; Still same result: the path and file name is: c:\Users\xxx\Documents\Visual Studio 2010\Projects\zMAP\bridgeWord-Html\bridgeWord-Html\bin\Debug\01 SettingsForVSC#.docx The MSWord is lunched but these errors are thrown: Microsoft Word This file could not be found! (c:\Users\xxx\Documents\Visual.doc) (c:\Users\xxx\Documents\...\Studio.doc) (c:\Users\xxx\Documents\...\01.doc) (c:\Users\xxx\Documents\...\Studio.doc) (c:\Users\xxx\Documents\...\SettingsForVSC#.doc) And then an empty msword page is open. [I see it breaks at the spaces in between names] ---How to fix this problem? I wish to open not only a docx file, but a html file also,or a txt file. thank you.

                      string path = Application.StartupPath;
                      private void button3_Click(object sender, EventArgs e)
                      {
                      string MSWordPath = "\"c:\\Program Files (x86)\\Microsoft Office\\Office14\\WINWORD.EXE\"";
                      selectedFile = path + "\\" + listBox1.SelectedItem as string;
                      //Process.Start("WINWORD.EXE", selectedFile); -or this but the same problem
                      Process.Start(MSWordPath, selectedFile);
                      }

                      modified on Friday, May 20, 2011 1:13 PM

                      _ Offline
                      _ Offline
                      _Q12_
                      wrote on last edited by
                      #11

                      I see a little problem with saving from msWord back to original. The file is opened all right, but in read-only. How do I change this? Sorry for my lack of knowledge... but this things are a bit new to me.

                      G 1 Reply Last reply
                      0
                      • _ _Q12_

                        Yes I know, that putting only the fileName+path, that file is opening with the default app. But what I really want is to open a html file and edit it with msword. If you have other alternative than: Process.Start(myDocFilePath); please tell. Though, thank you for your fast response... :)

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #12

                        If you want to go against the established file associations, then you need to specify the executable as the first parameter to Process.Start, the arguments concatenated and properly quoted as the second parameter. You already have that. Using Word for HTML editing is a horrible idea. Type three words and you get a huge file, that should tell enough. Use a real HTML editor, or use Visual Studio, or whatever you choose, but not Word. :)

                        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                        1 Reply Last reply
                        0
                        • _ _Q12_

                          I see a little problem with saving from msWord back to original. The file is opened all right, but in read-only. How do I change this? Sorry for my lack of knowledge... but this things are a bit new to me.

                          G Offline
                          G Offline
                          Ghydo
                          wrote on last edited by
                          #13

                          Do the user under which is running your program have write privileges over the file? Can you open it by hand (eg. open Word and then open the html file) and save it?

                          _ 1 Reply Last reply
                          0
                          • G Ghydo

                            Do the user under which is running your program have write privileges over the file? Can you open it by hand (eg. open Word and then open the html file) and save it?

                            _ Offline
                            _ Offline
                            _Q12_
                            wrote on last edited by
                            #14

                            Yes, If I open it by hand, it is saving ok, loading ok... I am the only user and I have all the privileges over the file R&W.

                            L 1 Reply Last reply
                            0
                            • _ _Q12_

                              Yes, If I open it by hand, it is saving ok, loading ok... I am the only user and I have all the privileges over the file R&W.

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #15

                              Does your app (that contains the Process.Start stuff) do something else with the same .doc path, something Stream-like, File-like, FileInfo-like? If so, did you Close() and Dispose() it properly before calling Process.Start()? :)

                              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                              _ 1 Reply Last reply
                              0
                              • L Luc Pattyn

                                Does your app (that contains the Process.Start stuff) do something else with the same .doc path, something Stream-like, File-like, FileInfo-like? If so, did you Close() and Dispose() it properly before calling Process.Start()? :)

                                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                                _ Offline
                                _ Offline
                                _Q12_
                                wrote on last edited by
                                #16

                                Yes, it opened the file... and I was not disposed it. Now is working correct. Thank you.

                                1 Reply Last reply
                                0
                                • _ _Q12_

                                  yes :)) I know that too "Word adds TONS of crap to the resulting HTML". But i dont make it for publication, just for my inner use. So, Ill adapt to its creepiness. If are some hints to make something look good/and right editing with msword, please do tell. thanks.

                                  D Offline
                                  D Offline
                                  Dave Kreskowiak
                                  wrote on last edited by
                                  #17

                                  There is no way to get Word to stop screwing up your HTML.

                                  A guide to posting questions on CodeProject[^]
                                  Dave Kreskowiak

                                  A 1 Reply Last reply
                                  0
                                  • D Dave Kreskowiak

                                    There is no way to get Word to stop screwing up your HTML.

                                    A guide to posting questions on CodeProject[^]
                                    Dave Kreskowiak

                                    A Offline
                                    A Offline
                                    AspDotNetDev
                                    wrote on last edited by
                                    #18

                                    Dave Kreskowiak wrote:

                                    There is no way to get Work to stop screwing up your HTML.

                                    Darn coworkers! ;P

                                    [Managing Your JavaScript Library in ASP.NET]

                                    D 1 Reply Last reply
                                    0
                                    • A AspDotNetDev

                                      Dave Kreskowiak wrote:

                                      There is no way to get Work to stop screwing up your HTML.

                                      Darn coworkers! ;P

                                      [Managing Your JavaScript Library in ASP.NET]

                                      D Offline
                                      D Offline
                                      Dave Kreskowiak
                                      wrote on last edited by
                                      #19

                                      Damn autocorrect!

                                      A guide to posting questions on CodeProject[^]
                                      Dave Kreskowiak

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups