Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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

C#
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

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
 
Share this answer
 
Comments
Kurac1 3-May-13 15:48pm    
the filename is correct...
Sergey Alexandrovich Kryukov 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 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 3-May-13 16:34pm    
Can you try to be more specific?..
—SA
Kurac1 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
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
 
Share this answer
 
v2

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



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