Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I have got the following error when I was trying to do some operations with a word file.

This File could not be found. (C:\//Users/rahul raj/AppData/Loc......).

The following code caused exception...
C#
textBox1.Text = @"C:/Users/rahul raj/AppData/Local/Temp/ConsultantResumes/PROGRAMMER_NAVEEN.doc";
textBox2.Text = @"E:\Ace Logic\Consultrak\Resumes\PROGRAMMER_NAVEEN.doc";

Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

object originalFileName = textBox1.Text;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
object destinationName = textBox2.Text ;

try
{
    Microsoft.Office.Interop.Word.Document openDocument = WordApp.Documents.Open(ref originalFileName, ref missing, ref readOnly,
                                                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                                    ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
    openDocument.SaveAs(ref destinationName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

    ((Microsoft.Office.Interop.Word._Document)openDocument).Close(ref missing, ref missing, ref missing);
    ((Microsoft.Office.Interop.Word._Application)WordApp).Quit(ref missing, ref missing, ref missing);

    System.Windows.Forms.MessageBox.Show("Finished", "AddTo - Test");
}
catch (Exception Ex)
{
    System.Windows.Forms.MessageBox.Show(Ex.Message.ToString(), "AddTo - Test");
}



Can someone help me to solve this issue?

I have solved this problem by simply changing /

here is the code...

C#
if (textBox1.Text.ToString().Contains("/"))
            {
                textBox1.Text = textBox1.Text.Replace("/", "\\");
            }


Regards
Sebastian
Posted
Updated 3-Apr-13 19:56pm
v4
Comments
bbirajdar 3-Apr-13 7:40am    
You are a senior member.. I dont believe you posted a unclear question..

Is it a web or windows app?

The error is because of the space in the directory name.. Remove the space.. I could have suggested a exact solution if you has posted the code... May be it requires to use \\ instead of \ ..depends how you are declaring the path in code

And never use hardcoded drive name in path.. Not every machine has D drive
Berry Harahap 3-Apr-13 11:27am    
Perhaps ("D:\\Shared\rahul raj\")... Perhaps He plays pleasantry, 12.8K, huuuuft.....
Sebastian T Xavier 4-Apr-13 1:45am    
I have updated my question now. The hard coded path is included for testing purpose only.

Not really, no.
Do you have a "D" drive? does the directory exist on it? What values are you passing through?

Put a breakpoint on the line, and look at the "originalFileName" parameter in the debugger - what value do you get? Does the file exist? Start there, and look at what you get.
 
Share this answer
 
Test here.. I assume you wanna open file by using openDialog, try this, make textbox and name it "tbDocument" to show directory path.

Then here the code..
private void readFile()
        {
            try
            {
                //OpenFileDialog openDialog = new OpenFileDialog();
                openDialog.DefaultExt = ".doc";
                openDialog.Filter = "Word Documents|*.doc|Text Files|*.txt";
                openDialog.FileName = string.Empty;
                openDialog.ShowDialog();
                tbDocument.Text = openDialog.FileName;

                if (tbDocument.Text.Length > 0)
                {
                    readFileContent(tbDocument.Text);
                    currentFile = openDialog.FileName;
                    //rtbInput.Modified = false;
                }
                else
                {
                    MessageBox.Show("Enter a valid file path");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error");
            }
        }


private void readFileContent(string path)
        {
            Microsoft.Office.Interop.Word.ApplicationClass wordAppClass = new ApplicationClass();
            object file = path;
            object nullobj = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Word.Document wordDoc = wordAppClass.Documents.Open(
                ref file, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj);
            wordDoc.ActiveWindow.Selection.WholeStory();
            wordDoc.ActiveWindow.Selection.Copy();
            IDataObject data = Clipboard.GetDataObject();
            rtbInput.Text = data.GetData(DataFormats.Text).ToString();
            //wordDoc.Close(ref nullobj, ref nullobj, ref nullobj);            
            wordAppClass.Quit(ref nullobj, ref nullobj, ref nullobj);
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900