Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Word
When pressing button genarate do .Doc
i get that i can open or save
when trying to open i get this error
 
Word experienced an error trying to opet the file.
Try these sugesstions.
*Check the file permissions for the document or drive
*Make sure there is suffient free memory and disk space.
*Open the file with the text recovery convert

 
when doing save as, it works to open the document, but when trying second time to download the document with save as i get the error
This is not a valid file name.
Try one or more of the following:
* Check the path to make sure it was typed correctly.
* Select a file from the list of files and folders.

 
Exception Details: System.Runtime.InteropServices.COMException: This is not a valid file name.
Try one or more of the following:
* Check the path to make sure it was typed correctly.
* Select a file from the list of files and folders.

 
My function for downloading the to word looks like this
 
private void SetWordDocument()
        {
            string strFileName =
                CleanUp(LabelFirstName.Text + "_" + LabelLastName.Text + "_" + DateTime.Now.ToString("yyyy-MM-dd") +
                        "." + DropDownListDownloadCv0.SelectedItem.Text);
 
            object fs = Server.MapPath("~/Upload/") + strFileName;
 
            using (var db = new knowitCVdbEntities())
            {
                var theEmpl = (from p in db.EMPLOYEES
                               where p.username == strUserName
                               select p).FirstOrDefault();
 
                if (theEmpl != null)
                {
 
                    object missing = Missing.Value;
                    object start1 = 0;
                    var wordApp = new ApplicationClass();
                    var myDoc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
 
                    object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
                    Range rng = myDoc.Range(ref start1, ref missing);
 
                    try
                    {
                        const char newLine = (char) 11;
                        myDoc.SaveAs(ref fs,
                                     ref missing, ref missing, ref missing, ref missing, ref missing,
                                     ref missing, ref missing, ref missing, ref missing, ref missing,
                                     ref missing,
                                     ref missing,
                                     ref missing, ref missing, ref missing);
 

                        SetSkillAndLevelMax3();
                        for (int i = 0; i < RepeaterStrongestSkillsMax2.Controls.Count; i++)
                        {
                            var skill = (Label) RepeaterStrongestSkillsMax2.FindControl("skill" + i);
                            var pie = (Image) RepeaterStrongestSkillsMax2.FindControl("pie" + i);
                            if (skill != null & pie != null)
                            {
                                rng.InsertBefore(skill.Text);
                                string piee = pie.ImageUrl;
                                rng.InlineShapes.AddPicture(piee, ref missing, ref missing, ref missing);
 

                            }
                        }
                        rng.InsertAfter(newLine.ToString());
                        rng.InsertBefore(newLine.ToString());
                        rng.InsertBefore(newLine + LabelPosition.Text + " " + LabelFirstName.Text + " " +
                                         LabelLastName.Text + newLine + LabelAbout.Text);
                        rng.InsertBefore(Label3Strong.Text);
                        foreach (var Label3StrongAbout in BulletedListStrong3About.Items)
                        {
 
                            rng.InsertBefore(newLine + Label3StrongAbout.ToString());
                        }
 
                        string imageKnowItLogo = Loggo.ImageUrl;
                        rng.InlineShapes.AddPicture(imageKnowItLogo, ref missing, ref missing, ref missing);
                        rng.InsertBefore(newLine.ToString());
 

 
                        rng.InsertBefore(newLine.ToString());
 

                        string imageProfPic = ImagePicture.ImageUrl;
                        rng.InlineShapes.AddPicture(imageProfPic, ref missing, ref missing, ref missing);
                        rng.InsertBefore(newLine.ToString());
                        rng.InsertAfter(newLine.ToString());
                        wordApp.Visible = true;
 

 

 
                        rng.InsertAfter(newLine.ToString());
                        rng.InsertAfter(lbldateofbirth.Text + " " + LabelDateOfBirth.Text);
                        rng.InsertAfter(newLine.ToString());
                        rng.InsertAfter(lblphonenumber.Text + " " + LabelPhoneNumber.Text);
                        rng.InsertAfter(newLine.ToString());
                        rng.InsertAfter(lblemail.Text + " " + LabelEmail.Text);
                        rng.InsertAfter(newLine.ToString());
                        rng.InsertAfter(newLine.ToString());
 
                        rng.InsertAfter(lbllanguages.Text);
                        rng.InsertAfter(newLine.ToString());
 

                        foreach (var vItem in BulletedListLanguages.Items)
                        {
                            rng.InsertAfter(vItem.ToString());
 
                            rng.InsertAfter(newLine.ToString());
                        }
                        rng.InsertAfter(newLine.ToString());
 

                        rng.InsertAfter(lblprevwork.Text);
                        rng.InsertAfter(newLine.ToString());
 
                        foreach (var vItem in BulletedListPrevWork.Items)
                        {
                            rng.InsertAfter(vItem.ToString());
                            rng.InsertAfter(newLine.ToString());
                        }
                        rng.InsertAfter(newLine.ToString());
                        rng.InsertAfter(lbleducations.Text);
                        rng.InsertAfter(newLine.ToString());
 
                        foreach (var vItem in BulletedListEducation.Items)
                        {
                            rng.InsertAfter(vItem.ToString());
                            rng.InsertAfter(newLine.ToString());
                        }
                        rng.InsertAfter(newLine.ToString());
                        rng.InsertAfter(lblcert.Text);
                        rng.InsertAfter(newLine.ToString());
 
                        foreach (var vItem in BulletedListCertifications.Items)
                        {
                            rng.InsertAfter(vItem.ToString());
                            rng.InsertAfter(newLine.ToString());
                        }
                        rng.InsertAfter(newLine.ToString());
                        rng.InsertAfter(lblcourses.Text);
                        rng.InsertAfter(newLine.ToString());
 
                        foreach (var vItem in BulletedListCourses.Items)
                        {
                            rng.InsertAfter(vItem.ToString());
                            rng.InsertAfter(newLine.ToString());
                        }
 
                        rng.InsertAfter(newLine.ToString());
                        rng.InsertAfter(lblbusniess.Text);
                        rng.InsertAfter(newLine.ToString());
 
                        foreach (var vItem in BulletedListBusiness.Items)
                        {
                            rng.InsertAfter(vItem.ToString());
                            rng.InsertAfter(newLine.ToString());
                        }
                        rng.InsertAfter(newLine.ToString());
                        //////////////////////////////////////////////////////////
                        rng.InsertAfter(lbltechnicalskills.Text);
                        rng.InsertAfter(newLine.ToString());
 

                        rng.InsertAfter(LabelDataBas.Text);
                        rng.InsertAfter(newLine.ToString());
                        foreach (var vItem in BulletedListDataBase.Items)
                        {
                            rng.InsertAfter(vItem.ToString());
                            rng.InsertAfter(newLine.ToString());
                        }
 
                        ////////////////////////////////////////////////////
                        rng.InsertAfter(lblprogramming.Text);
                        rng.InsertAfter(newLine.ToString());
                        foreach (var vItem in BulletedListProgramming.Items)
                        {
                            rng.InsertAfter(vItem.ToString());
                            rng.InsertAfter(newLine.ToString());
                        }
                        //////////////////////////////////////////////
                        rng.InsertAfter(lblmethods.Text);
                        rng.InsertAfter(newLine.ToString());
                        foreach (var vItem in BulletedListMethods.Items)
                        {
                            rng.InsertAfter(vItem.ToString());
                            rng.InsertAfter(newLine.ToString());
                        }
                        ////////////////////////////////////////////////////////
                        rng.InsertAfter(lbldevop.Text);
                        rng.InsertAfter(newLine.ToString());
 
                        foreach (var vItem in BulletedDevelopmentTools.Items)
                        {
                            rng.InsertAfter(vItem.ToString());
                            rng.InsertAfter(newLine.ToString());
                        }
                        /////////////////////////////////////////////////////
                        rng.InsertAfter(lblwebserver.Text);
                        rng.InsertAfter(newLine.ToString());
                        foreach (var vItem in BulletedListWebServer.Items)
                        {
                            rng.InsertAfter(vItem.ToString());
                            rng.InsertAfter(newLine.ToString());
                        }
 
                        if (RadioButtonListChooseLang.SelectedItem.Text == "Swedish")
                        {
                            GetAlSwedishlAssigmentsDoc(theEmpl);
 
                            Label lbl = new Label
                                            {
                                                Text = newLine + "Uppdrag"
                                            };
 
                            rng.InsertAfter(lbl.Text);
 
                            for (int i = 0; i < RepeaterAssigments.Controls.Count; i++)
                            {
                                Label comp = (Label) RepeaterAssigments.FindControl("comp" + i);
                                Label sect = (Label) RepeaterAssigments.FindControl("sect" + i);
                                Label area = (Label) RepeaterAssigments.FindControl("are" + i);
                                Label from = (Label) RepeaterAssigments.FindControl("fro" + i);
                                Label to = (Label) RepeaterAssigments.FindControl("too" + i);
                                Label description = (Label) RepeaterAssigments.FindControl("des" + i);
                                Label tool = (Label) RepeaterAssigments.FindControl("tool" + i);
                                Label tec = (Label) RepeaterAssigments.FindControl("tec" + i);
                                Label reference = (Label) RepeaterAssigments.FindControl("ref" + i);
 
                                Label franManad = (Label) RepeaterAssigments.FindControl("franmanad" + i);
                                Label tillManad = (Label) RepeaterAssigments.FindControl("tillmanad" + i);
                                Label kundnytta = (Label) RepeaterAssigments.FindControl("kundnyttaSve" + i);
                                Label arbetsupgifter = (Label) RepeaterAssigments.FindControl("arbetsuppgifterSve" + i);
                                Label roll = (Label) RepeaterAssigments.FindControl("rollSve" + i);
 

                                Literal namesectorLit = (Literal) RepeaterAssigments.FindControl("NamesectorLit" + i);
                                Literal nameAreaLit = (Literal) RepeaterAssigments.FindControl("NameAreaLit" + i);
                                Literal nameTechLit = (Literal) RepeaterAssigments.FindControl("NameTechLit" + i);
                                Literal nameToolLite = (Literal) RepeaterAssigments.FindControl("NameToolLit" + i);
                                Literal nameRefLit = (Literal) RepeaterAssigments.FindControl("NameRefLit" + i);
 
                                Literal arbetsuppgifterLit =
                                    (Literal) RepeaterAssigments.FindControl("arbetsuppgifter" + i);
                                Literal rollLit = (Literal) RepeaterAssigments.FindControl("roll" + i);
                                Literal uppdragLit = (Literal) RepeaterAssigments.FindControl("updragSve" + i);
                                Literal kundnyttaLit = (Literal) RepeaterAssigments.FindControl("NyttaKundSve" + i);
 

                                if (comp != null && from != null
                                    && to != null && description != null && tool != null && tec != null
 
                                    && nameTechLit != null && nameToolLite != null &&
                                    nameRefLit != null && franManad != null && tillManad != null && kundnytta != null &&
                                    arbetsupgifter != null
                                    && arbetsuppgifterLit != null && rollLit != null
                                    && uppdragLit != null && kundnyttaLit != null && roll != null)
                                {
 
                                    rng.InsertAfter(newLine + comp.Text);
                                    rng.InsertAfter(from.Text);
                                    rng.InsertAfter(to.Text + newLine);
 
                                    rng.InsertAfter(nameToolLite.Text);
 
                                    rng.InsertAfter(tool.Text + newLine);
 
                                    rng.InsertAfter(nameTechLit.Text);
                                    rng.InsertAfter(tec.Text + newLine);
 

 
                                    rng.InsertAfter(franManad.Text);
                                    rng.InsertAfter(tillManad.Text);
                                    rng.InsertAfter(kundnyttaLit.Text);
                                    rng.InsertAfter(kundnytta.Text);
                                    rng.InsertAfter(arbetsuppgifterLit.Text);
                                    rng.InsertAfter(arbetsupgifter.Text);
                                    rng.InsertAfter(rollLit.Text);
                                    rng.InsertAfter(roll.Text);
                                    rng.InsertAfter(description.Text + newLine);
                                    rng.InsertAfter(uppdragLit.Text);
 

                                    Image image = new Image
                                                      {
                                                          ImageUrl =
                                                              SPContext.Current.Web.Url + "/" +
                                                              SPContext.Current.File.Url +
                                                              "~/_layouts/images/Cv.Knowit/img_line.png"
                                                      };
                                    //rng.InsertAfter(image.ImageUrl);
                                    rng.InlineShapes.AddPicture(image.ImageUrl, ref missing, ref missing,
                                                                ref missing);
 
                                }
                                if (sect == null && namesectorLit == null
                                    && reference == null && nameRefLit == null &&
                                    nameAreaLit == null && area != null)
                                {
 
                                }
                                if (sect != null && namesectorLit != null
                                    && reference != null && nameRefLit != null &&
                                    nameAreaLit != null && area != null)
                                {
                                    rng.InsertAfter(namesectorLit.Text);
                                    rng.InsertAfter(sect.Text + newLine);
                                    rng.InsertAfter(nameAreaLit.Text);
                                    rng.InsertAfter(area.Text + newLine);
                                    rng.InsertAfter(nameRefLit.Text);
                                    rng.InsertAfter(reference.Text + newLine);
                                }
                            }
                        }
 

 

                        else if (RadioButtonListChooseLang.SelectedItem.Text == "English")
                        {
                            GetAlEnglishAssigmentsDoc(theEmpl);
 
                            Label lbl = new Label
                                            {
                                                Text = newLine + "Assigments"
                                            };
 
                            rng.InsertAfter(lbl.Text);
 
                            for (int i = 0; i < RepeaterAssigments.Controls.Count; i++)
                            {
                                Label comp = (Label) RepeaterAssigments.FindControl("comp" + i);
                                Label sect = (Label) RepeaterAssigments.FindControl("sect" + i);
                                Label area = (Label) RepeaterAssigments.FindControl("are" + i);
                                Label from = (Label) RepeaterAssigments.FindControl("fro" + i);
                                Label to = (Label) RepeaterAssigments.FindControl("too" + i);
                                Label description = (Label) RepeaterAssigments.FindControl("des" + i);
                                Label tool = (Label) RepeaterAssigments.FindControl("tool" + i);
                                Label tec = (Label) RepeaterAssigments.FindControl("tec" + i);
                                Label reference = (Label) RepeaterAssigments.FindControl("ref" + i);
                                Literal namesectorLit =
                                    (Literal) RepeaterAssigments.FindControl("NamesectorLit" + i);
                                Literal nameAreaLit = (Literal) RepeaterAssigments.FindControl("NameAreaLit" + i);
                                Literal nameTechLit = (Literal) RepeaterAssigments.FindControl("NameTechLit" + i);
                                Literal nameToolLite = (Literal) RepeaterAssigments.FindControl("NameToolLit" + i);
                                Literal nameRefLit = (Literal) RepeaterAssigments.FindControl("NameRefLit" + i);
 

 

                                if (comp != null && sect != null && area != null && from != null
                                    && to != null && description != null && tool != null && tec != null &&
                                    reference != null
                                    && namesectorLit != null
                                    && nameAreaLit != null && nameTechLit != null && nameToolLite != null &&
                                    nameRefLit != null)
                                {
 
                                    rng.InsertAfter(newLine + comp.Text);
                                    rng.InsertAfter(namesectorLit.Text);
 
                                    rng.InsertAfter(sect.Text + newLine);
 
                                    rng.InsertAfter(nameAreaLit.Text);
                                    rng.InsertAfter(area.Text + newLine);
                                    rng.InsertAfter(from.Text);
                                    rng.InsertAfter(to.Text + newLine);
                                    rng.InsertAfter(description.Text + newLine);
                                    rng.InsertAfter(nameToolLite.Text);
 
                                    rng.InsertAfter(tool.Text + newLine);
 
                                    rng.InsertAfter(nameTechLit.Text);
                                    rng.InsertAfter(tec.Text + newLine);
 
                                    rng.InsertAfter(nameRefLit.Text);
                                    rng.InsertAfter(reference.Text + newLine);
 
                                    Image image = new Image
                                                      {
                                                          ImageUrl =
                                                              SPContext.Current.Web.Url + "/" +
                                                              SPContext.Current.File.Url +
                                                              "~/_layouts/images/Cv.Knowit/img_line.png"
                                                      };
                                    
                                    rng.InlineShapes.AddPicture(image.ImageUrl, ref missing, ref missing,
                                                                ref missing);
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
 
                    finally
                    {
                        myDoc.Save();
 
                        myDoc.Close(ref doNotSaveChanges, ref missing, ref missing);
 

                        if (RadioButtonListChooseLang.SelectedItem.Text == "Swedish")
                        {
                            RepeaterAssigments.Controls.Clear();
                            GetAlSwedishlAssigments(theEmpl);
 
                        }
                        else if (RadioButtonListChooseLang.SelectedItem.Text == "English")
                        {
                            RepeaterAssigments.Controls.Clear();
                            GetAlEnglishAssigments(theEmpl);
 
                        }
                    }
 
                }
 
                string path = fs.ToString();
                FileInfo file = new FileInfo(path);
                if (file.Exists)
                {
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    Response.ContentType = "application/msword";
                    Response.WriteFile(file.FullName);
                    Response.End();
                }
                else
                {
                    Response.Write("This file does not exist.");
                }
            }
        }
Posted 3-May-13 10:08am
Kurac1748
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Make sure the file name is valid. Use the debugger and see what is the file name which you application tries to use to save the document, correct accordingly.
 
You already posted 83 question. After all, learn the art of asking questions in a productive way. The code you show is way too long, yet your post does not provide sufficient information. But, in this very case, we don't need this information. It's only you who needs it. You really need to know what's the attempted file name, to fix it. In other words, you need to learn debugging, instead of asking questions like this one, which is not helpful.
 
—SA
  Permalink  
Comments
Kurac1 at 3-May-13 15:48pm
   
the filename is correct...
Sergey Alexandrovich Kryukov at 3-May-13 15:52pm
   
No, no true. You did not check up properly. The error "not a valid file name" means it is not valid, period. You did not event bother tell us this name.
Why asking questions at all, if you resist resolving the problem so badly?!
—SA
Kurac1 at 3-May-13 15:57pm
   
and i dont get when debbug first time the filename is ok, second time when trying to save the document not a valid file name i get...
Sergey Alexandrovich Kryukov at 3-May-13 16:34pm
   
Can you try to be more specific?..
—SA
Kurac1 at 3-May-13 15:53pm
   
and i have debbuged everything looks correct filename are the same everywere , but i cannot open the document only save and its not possible to debbug the filedialog that apperas if i select open or save as, so its diffecult
Sergey Alexandrovich Kryukov at 3-May-13 15:53pm
   
Please see my reply above. What you say is just words.
—SA
Kurac1 at 3-May-13 16:01pm
   
And i know that not a valid file name what i means but i dont not what means when first time it work second not.. thats why i am asking for help... and second when posting smal code everyone ask for more code, thats why i post all the code so the user dont get confused that some code is missing..
Sergey Alexandrovich Kryukov at 3-May-13 16:34pm
   
And again you did not tell the file name! It's getting more and more interesting... :-)
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You may get rid of this error owing to corrupt .doc file fixer
 
This solution [spam link removed please report the account as a spammer] will solve your trouble
  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 Sergey Alexandrovich Kryukov 555
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 359
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 6 Jul 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