To fix the file name splitting problem, wrap the file name in quotes. Word
will see it as a single paraneter then.
To find the Word executable, you can wrap the FindExecutable api call. Just
pass it the name of the file you're about to open.
[System.Runtime.InteropServices.DllImport("shell32. dll")]
static extern int FindExecutable(string fileName, string fileDir,
System.Text.StringBuilder buffer);
// Returns the name and path of the EXEcutable file that is associated to
the specified file.
// Returns an empty string if there is no such associated file,
// and raises an error if the file or the path hasn't been found.
public string GetExecutableFile(string filePath)
{
const int ERROR_FILE_NO_ASSOCIATION = 31;
const int ERROR_FILE_NOT_FOUND = 2;
const int ERROR_PATH_NOT_FOUND = 3;
const int ERROR_FILE_SUCCESS = 32;
System.Text.StringBuilder buffer = new System.Text.StringBuilder(260);
// call the FindExecutable API function and process the return value
int ret = FindExecutable(System.IO.Path.GetFileName(filePath ),
System.IO.Path.GetDirectoryName(filePath), buffer);
if (ret == ERROR_FILE_NOT_FOUND || ret == ERROR_PATH_NOT_FOUND)
throw new System.IO.FileNotFoundException();
else if (ret == ERROR_FILE_NO_ASSOCIATION)
return "";
else if (ret >= ERROR_FILE_SUCCESS)
{
// extract the ANSI string that contains the name of the associated
executable file
return buffer.ToString();
}
else
return "";
}
HTH
Steve
"gazza67" <ga*******@gmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...
Hi,
I want to do something that I thought would be simple but i cant seem
to work it out, perhaps someone out there could help me.
I want to browse for a file (it will be a word document), save the file
name to a string and then at some later stage open that file with word.
The operating system will be windows 2000 (dont know if that makes a
difference or not).
string pathToWord = "c:\\Program File\\Microsoft
Office\\Office\\winword.exe";
OpenFileDialog theDiag = new OpenFileDialog();
theDiag.ShowDialog();
string theFileName = theDiag.FileName; // note the file name is
c:\Test Directory\testFile.doc
Process.Start (pathToWord, theFileName);
This resulted in word trying to open two files called c:\test and
Directory\testFile.doc.
Could someone tell me what I can do to make word see this filename
correctly?
Another question is, if I dont include the pathToWord and just rely on
Windows to work out how to open a .doc file it takes almost 30 seconds
to open the document (it does it successfully - but it takes too long)
- even though if I go into the file explorer and double click on the
.doc file it will open inside word immediately. I dont really want to
include the path for word (since it may be different on different
computers) - how do I get around this problem?
Gary