Home > DeveloperSection > Articles > HOW TO CREATE DOC FILE INSIDE FOLDER

HOW TO CREATE DOC FILE INSIDE FOLDER


C# C# 
Ratings:
1 Comment(s)
 12234  View(s)
Rate this:

HOW TO CREATE DOC FILE INSIDE FOLDER

How to create word document inside a folder

As we have shown in the below application textbox takes Folder Name and ID textbox takes word document file name.

In the first step we have to place two Textbox as Textbox Name = “Hello” //Folder Name Hello

And Textbox ID = “123” //Document Name will be “123.doc”

Name textbox is assigned for name of the folder where as id is assigned for doc id. As shown belowin the snapshot.

create word document inside a folder

On the click event of button “I.e. click here to create folder” first step is to create a folder in the directory which is already specify on the path, second step is to specify the name of the doc under that folder. For this we need to write these lines of code inside the click to create folder button option I, e,txtbutton property before writing code don’t forget to place a namespace” using.Microsoft.Office.Interaop.Word” and System.IO.

After that place a code in the highlighted section

private void button1_Click(object sender, EventArgs e)

        {

object missing=System.Reflection.Missing.Value;

            object visible=true;//set it to true for object

            object start1=0;   

            object end1=0;     

//creating  applicationclass object as a wordapp

            ApplicationClass wordapp = new ApplicationClass();

//creating document object as adoc and adding it into a wordapp.

            Document adoc = wordapp.Documents.Add(ref missing, ref missing, ref missing, ref missing);

//creating range object with start1 and missing parameters.

            Range rng = adoc.Range(ref start1, ref missing);

            try

            {

          

//Inside a messagebox it shows a message if we want to create folder as well as inside message box it will display button like OKCancel with Dialog Result Ok.

 if (MessageBox.Show("Do you want to create folder?", "Information", MessageBoxButtons.OKCancel,MessageBoxIcon.Information) == DialogResult.OK)

                {

 

 //Checking both the textbox wether it is null  or not.if it is null then display a messsage box that you can create folder files are empty.

                    if (txtName.Text.Trim() == string.Empty && txtID.Text.Trim() == "")

                    {

                        MessageBox.Show("You can create folder files are empty");

                    }

                    else

                    {

                        MessageBox.Show("do u want to create new file or cancel");

 

//creating name of the folder in that path.

                        string path = @"E:\abc\" + txtName.Text.Trim();

//passing this path it into a directoryinfo.

                        DirectoryInfo di = Directory.CreateDirectory(path);

 

                        rng.Font.Name = "Georgia";

                        rng.InsertAfter("hello my name is abc.");

 

                        object filename = @"E:\abc\" + txtName.Text.Trim() + "\\" + txtID.Text.Trim() + ".doc";

//saving the doc file with the help of ref.

                        adoc.SaveAs(ref filename, 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);

                        wordapp.Visible = true;

                    }

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

          

        }

 

Before writing these code we have to addReference in COM tab under the option like Microsoftword 12.0 object library. As shown below in snapshot:

Microsoftword 12.0 object library

 

The output of the result is shown like

If we press click to create folder message box will appear you want to create folder ok and cancel. If u press Ok and then enter name under the name textbox it will created name Andy folder in the directory E drive and if we place id inside the ID it will create id inside the folder and open the doc file inside the folder, as shown below in the snapshot.

Word Document


Microsoft does not recommend creation of Word documents with use of COM on server side

By Peter Johnson on   3 years ago
Thank you for your article, you’ve really made an effort but I wanted to warn you that using COM is not such a good idea  for the purpose of developing business apps. Using COM for mail-merge (generating a word document based on the tagged template) can causes problems such as:
  • Limitation to a version, i.e. it only works with one or certain versions of Office
  • Transferring or installation on other systems is an issue
  • There is no protection mechanism in the management of COM proxies
  • Causes memory leaks
  • It has very slow performance
  • Application using COM cannot work properly if it is deployed on the server
You can check Microsoft recommendation for (not) using COM on server side (http://support.microsoft.com/kb/257757).
You can also check at http://openxmldeveloper.org/ how to make your own custom OpenXML based solution without using COM or you can take a look at some of third-parties on the market such Aspose or Docentric to save hours for solving this task since OpenXML is very complex format.


Don't want to miss updates? Please click the below button!

Follow MindStick