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.
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 visible=true;//set it to true for object
//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);
//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");
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)
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:
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.