hello everyone
i have a problem in writing in word document the proble is that i want to append a string after a table i made in that document but it overides it the code is:-

//Text to word file
StringBuilder strBuilder = newStringBuilder();
strBuilder.Append("<h1 title='Header' align='Center'>Writing To Word Using ASP.NET</h1> ".ToString());
strBuilder.Append("<br>".ToString());
strBuilder.Append("<table align='Center'>".ToString());
strBuilder.Append("<tr>".ToString());
strBuilder.Append("<td style='width:100px;color:green'><b>amiT</b></td>".ToString());
strBuilder.Append("<td style='width:100px;color:red'>India</td>".ToString());
strBuilder.Append("</tr>".ToString());
strBuilder.Append("</table>".ToString());
// string strPath = Request.PhysicalApplicationPath + "\\document\\Test.doc";
string strPath = filename;
//string strTextToWrite = TextBox1.Text;
FileStream fStream = newFileStream (strPath,FileMode.Append);
fStream.Close();
StreamWriter sWriter = newStreamWriter(strPath);
sWriter.Write(strBuilder);
sWriter.Close();

any help would be appreciated

FileStream fStream = newFileStream (strPath,FileMode.Append);
fStream.Close();
StreamWriter sWriter = newStreamWriter(strPath);
sWriter.Write(strBuilder);
sWriter.Close();

any help would be appreciated

You probably need to seek to the end of the file? It seems like that would be the way to go. The Streamwriter starts at position 0, I think, and just clobbers whatever is there...

You probably need to seek to the end of the file?

would you just tell me how to do that?
thnaks alot for your reply
appreciated

FileStream fStream = newFileStream (strPath,FileMode.Append);
fStream.Close();
StreamWriter sWriter = newStreamWriter(strPath);
sWriter.Write(strBuilder);
sWriter.Close();
FileStream fStream = newFileStream (strPath,FileMode.Append);
//fStream.Close();
//StreamWriter sWriter = newStreamWriter(strPath);
StreamWriter = new StreamWriter(fStream);
sWriter.Write(strBuilder);
sWriter.Flush();
sWriter.Close();

The changes should fix your append at least. Not sure if you are even getting it to compile though.

FileStream fStream = newFileStream (strPath,FileMode.Append);
//fStream.Close();
//StreamWriter sWriter = newStreamWriter(strPath);
StreamWriter = new StreamWriter(fStream);
sWriter.Write(strBuilder);
sWriter.Flush();
sWriter.Close();

The changes should fix your append at least. Not sure if you are even getting it to compile though.

actually they did nothing i don,t know why but any way appreciated
sorry for replying lately

actually they did nothing i don,t know why but any way appreciated
sorry for replying lately

No problem. When you say "did nothing", I really don't know what that means.

I'm not sure if know, but you can append to any file you want. However, that doesn't mean that the reader (MS Word in this case) of the file will understand the data appended.

Anyway, if you will post your code so I can run it on my end, I will help you figure it out.

Anyway, if you will post your code so I can run it on my end, I will help you figure it out.

thanks alot
that's my project and it's databases
my code is supposed to make crystal report ,save it as a word document then append some words in that word document
would you just press "save report" in form1.cs then "save " in form2.cs the append code in save menustripitem handler in form2.cs

thanks alot for ur help it's really appreciated

thanks alot
that's my project and it's databases
my code is supposed to make crystal report ,save it as a word document then append some words in that word document
would you just press "save report" in form1.cs then "save " in form2.cs the append code in save menustripitem handler in form2.cs

thanks alot for ur help it's really appreciated

I've compiled the project and am looking at the warnings, particularly the missing reference: Microsoft.Office.Interop.Word

I am installing the class library download and will get back to you...

i have a problem in writing in word document the proble is that i want to append a string after a table i made in that document but it overides it...

Ok, your text is being appended to the word document, as you have told it to do. The problem is how Word is interpreting the document. When debugging, I saw that the string "hank" was indeed getting in the document, so I played with some formatting changes in the code and used BinaryWriter instead of StreamWriter so I could position the stream 1 position away from the EOF:

//StreamWriter sWriter = new StreamWriter(fStream);
            BinaryWriter sWriter = new BinaryWriter(fStream);
            sWriter.Seek(1, SeekOrigin.End);
            sWriter.Write("hany}");

Now, Word will display the appended text on a new page. Notice the addition of the closing brace in the text: "hany}". That is because I overwrote the closing brace already existing in the file (with Seek) and I wanted word to see "hany" before the final closing brace.

What you are dealing with at this point is Word scripting and you need to figure out how you can modify word documents externally or use some library that facilitates doing so, instead of using standard stream writers.

If the word "hany" is all you need appear at the end of the doc, and you wanted it to appear on the same page, you might be able to modify the export options to not fill the entire page with the table, then simply use the quick fix I just gave you.

Does this make sense?

As an after thought, have you considered just adding a field to the end of your report that you can set to text "hany", or whatever, so that when it is exported it will properly format it in the word document? Not sure exactly what you need to do for your project, but a text field in the report or page footer section seems like it might be the way to go.

I ran into that StringBuilder code using StreamWriter on a another website. It's not going to work simply appending it to another word document. To understand why, do the following:

Set a breakpoint after the export of your CrystalReport and run program to there.

Open doc in Notepad, and look over the contents: note the length of the document, spacing, formatting, reference to CrystalReports, etc,. It's all viewable text, so you should have no problem.

Close notepad.

Now, open it in Word and go to the end of the document by hitting PgDn a few times. Do a Ctrl-Enter, which will insert a manual page break. PgDn into the new page.

Now, type in some text like: "some text", and save the document.

Now, open again in Notepad and notice the changes Word made to that document--it's pretty drastic. Not only does it create a bunch of new formatting script, it rearranges the existing script too. Search for the string "some text" and look at how it was embedded inside formatting code.

You are mingling data inside of an interprative format that must be formatted or reformatted correctly so that Word understands what to do. The reason that simple StringBuilder output to a stream probably works with a new file is because when Word first reads it, it will try to determine the format at that time, which is just HTML. However, when you append the HTML to the existing Crystal embedded document, it is not expecting information outside of the already defined report interpratation created during the export.

There is probably a way you can mingle the two, but I don't know how. Obviously, Word knows how when you add new stuff inside the application. I will keep looking for better information on the subject, but I would suggest modifying the rpt file to accomplish your objective if possible.

As an after thought, have you considered just adding a field to the end of your report that you can set to text "hany", or whatever, so that when it is exported it will properly format it in the word document? Not sure exactly what you need to do for your project, but a text field in the report or page footer section seems like it might be the way to go.

actually i thought about that but that text that i want to view in the word document is variable i need to pass it as a parameter ro the report but when i tried to pass string parameters to the report i completely failed in run time it asks me about a range.....any way i couldn't do that do u know how to pass parameters to crystal report?
but actually your help is really appreciated thanks alot DoubleD

actually i tried to make the program in the link you showed me to run but i couldn't but i tried to pass parameters to the crystal report and export it to word using export in it's options in the report viewer not using my code it exported the report with the parameters in it so it makes me don't need to append in the word document thanks double d you helped me alot and it's so appreciated

but there is another problem when the crystal report is exported to the word document the it exports the arabic daynamic data in it reversed do you know how to handle that

actually i tried to make the program in the link you showed me to run but i couldn't but i tried to pass parameters to the crystal report and export it to word using export in it's options in the report viewer not using my code it exported the report with the parameters in it so it makes me don't need to append in the word document thanks double d you helped me alot and it's so appreciated

but there is another problem when the crystal report is exported to the word document the it exports the arabic daynamic data in it reversed do you know how to handle that

You are most welcome!

I don't know why you are getting the data reversed. May I suggest you close this thread and start a new one about the reversal of the data?

Cheers!

thanks alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.