Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Word
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...
            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...
 
if (textBox1.Text.ToString().Contains("/"))
            {
                textBox1.Text = textBox1.Text.Replace("/", "\\");
            }
 
Regards
Sebastian
Posted 3-Apr-13 2:15am
Edited 3-Apr-13 20:56pm
v4
Comments
aspnet_regiis -i at 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 at 3-Apr-13 11:27am
   
Perhaps ("D:\\Shared\rahul raj\")... Perhaps He plays pleasantry, 12.8K, huuuuft.....
Sebastian T Xavier at 4-Apr-13 1:45am
   
I have updated my question now. The hard coded path is included for testing purpose only.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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);
        }
  Permalink  
v2

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

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 256
1 OriginalGriff 251
2 BillWoodruff 240
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 4 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100