Reputation: 1
This block of code throws an error called file name is invalid.
I want to create a folder named as "test" inside this there will be another folder named as today's date "date" , i want to keep the the word document inside this date folder, please help.
public string File_path;
public string docfile_path;
public string filename;
private void button1_Click(object sender, EventArgs e)
{
string time = DateTime.Now.ToString("HH.mm.ss");
string date = DateTime.Today.ToShortDateString();
docfile_path = File_path+ "test" + date;
Directory.CreateDirectory(docfile_path);
filename = docfile_path + "worddoc"+"-" +".docx";
Word.Application app = new Word.Application();
Word.Document doc = new Word.Document();
try
{
doc = app.Documents.Open(filename);
}
catch
{
}
Word.Paragraph oPara1;
oPara1 = doc.Content.Paragraphs.Add();
oPara1.Range.Text = "Test Result";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24;
oPara1.Range.InsertParagraphAfter();
oPara1.Range.InsertParagraphAfter();
Word.Paragraph oPara2;
oPara2 = doc.Content.Paragraphs.Add();
oPara2.Range.Text = "Test Name";
oPara2.Range.Font.Bold = 1;
oPara2.Format.SpaceAfter = 24;
oPara2.Range.InsertParagraphAfter();
doc.SaveAs2(filename);
doc.Close();
doc = null;
app.Quit();
app = null;
}
Upvotes: 0
Views: 545
Reputation: 191
Surprisingly enough, this code compile and run, but the outcome is not what you probably wanted.
A couple of things is wrong in this code:
1.you cant add strings like that to create a path, a path should be created with the '/' symbol between directories. this is a legal path:
string path = @"C:\Users\username\Desktop\Games";
this is not :
string path = @"C:UsersusernameDesktopGames";
you can fix it by using the Path.Combine function as follow:
docfile_path = Path.Combine(File_path , "test" , date);
be sure to this for all path strings (including File_path that is value is not shown in the code above).
2.you should use
Document doc = app.Documents.Add();
to create a new Word document and not
Document doc = new Document();
3.you should use a different format for string date, DateTime.ToShortDateString() is dividing the date with the '/' symbol which will create new folders. try using:
string date = DateTime.Today.ToString("dd.MM.yyyy");
4.I don't see any reason for the line
doc = app.Documents.Open(filename);
You are trying to open the the file that you intent to create?
here is the code i used:
string File_path = @"C:\Users\yakir\Desktop";
string docfile_path;
string filename;
string time = DateTime.Now.ToString("HH.mm.ss");
string date = DateTime.Today.ToString("dd.MM.yyyy");
docfile_path = Path.Combine(File_path , "test" , date);
Directory.CreateDirectory(docfile_path);
filename = Path.Combine(docfile_path, "worddoc" + "-" + ".docx");
Application app = new Application();
Document doc = app.Documents.Add();
Paragraph oPara1;
oPara1 = doc.Content.Paragraphs.Add();
oPara1.Range.Text = "Test Result";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24;
oPara1.Range.InsertParagraphAfter();
oPara1.Range.InsertParagraphAfter();
Paragraph oPara2;
oPara2 = doc.Content.Paragraphs.Add();
oPara2.Range.Text = "Test Name";
oPara2.Range.Font.Bold = 1;
oPara2.Format.SpaceAfter = 24;
oPara2.Range.InsertParagraphAfter();
doc.SaveAs2(filename);
doc.Close();
doc = null;
app.Quit();
app = null;
}
Upvotes: 1