append in word document using c#

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 28
Reputation: fafi_ali is an unknown quantity at this point 
Solved Threads: 0
fafi_ali fafi_ali is offline Offline
Light Poster

append in word document using c#

 
0
  #1
Aug 10th, 2009
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:-
  1. //Text to word file
  2. StringBuilder strBuilder = newStringBuilder();
  3. strBuilder.Append("<h1 title='Header' align='Center'>Writing To Word Using ASP.NET</h1> ".ToString());
  4. strBuilder.Append("<br>".ToString());
  5. strBuilder.Append("<table align='Center'>".ToString());
  6. strBuilder.Append("<tr>".ToString());
  7. strBuilder.Append("<td style='width:100px;color:green'><b>amiT</b></td>".ToString());
  8. strBuilder.Append("<td style='width:100px;color:red'>India</td>".ToString());
  9. strBuilder.Append("</tr>".ToString());
  10. strBuilder.Append("</table>".ToString());
  11. // string strPath = Request.PhysicalApplicationPath + "\\document\\Test.doc";
  12. string strPath = filename;
  13. //string strTextToWrite = TextBox1.Text;
  14. FileStream fStream = newFileStream (strPath,FileMode.Append);
  15. fStream.Close();
  16. StreamWriter sWriter = newStreamWriter(strPath);
  17. sWriter.Write(strBuilder);
  18. sWriter.Close();

any help would be appreciated
Last edited by John A; Aug 10th, 2009 at 10:18 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: append in word document using c#

 
0
  #2
Aug 13th, 2009
Originally Posted by fafi_ali View Post
  1. FileStream fStream = newFileStream (strPath,FileMode.Append);
  2. fStream.Close();
  3. StreamWriter sWriter = newStreamWriter(strPath);
  4. sWriter.Write(strBuilder);
  5. 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...
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 28
Reputation: fafi_ali is an unknown quantity at this point 
Solved Threads: 0
fafi_ali fafi_ali is offline Offline
Light Poster

Re: append in word document using c#

 
0
  #3
Aug 15th, 2009
[QUOTE=alc6379;948080]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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 901
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 141
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: append in word document using c#

 
0
  #4
Aug 15th, 2009
Originally Posted by fafi_ali View Post
  1. FileStream fStream = newFileStream (strPath,FileMode.Append);
  2. fStream.Close();
  3. StreamWriter sWriter = newStreamWriter(strPath);
  4. sWriter.Write(strBuilder);
  5. sWriter.Close();
  1. FileStream fStream = newFileStream (strPath,FileMode.Append);
  2. //fStream.Close();
  3. //StreamWriter sWriter = newStreamWriter(strPath);
  4. StreamWriter = new StreamWriter(fStream);
  5. sWriter.Write(strBuilder);
  6. sWriter.Flush();
  7. sWriter.Close();
The changes should fix your append at least. Not sure if you are even getting it to compile though.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 28
Reputation: fafi_ali is an unknown quantity at this point 
Solved Threads: 0
fafi_ali fafi_ali is offline Offline
Light Poster

Re: append in word document using c#

 
0
  #5
Aug 21st, 2009
Originally Posted by DdoubleD View Post
  1. FileStream fStream = newFileStream (strPath,FileMode.Append);
  2. //fStream.Close();
  3. //StreamWriter sWriter = newStreamWriter(strPath);
  4. StreamWriter = new StreamWriter(fStream);
  5. sWriter.Write(strBuilder);
  6. sWriter.Flush();
  7. 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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 901
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 141
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: append in word document using c#

 
0
  #6
Aug 21st, 2009
Originally Posted by fafi_ali View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 28
Reputation: fafi_ali is an unknown quantity at this point 
Solved Threads: 0
fafi_ali fafi_ali is offline Offline
Light Poster

Re: append in word document using c#

 
0
  #7
Aug 22nd, 2009
Originally Posted by DdoubleD View Post
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
Attached Files
File Type: zip data.zip (23.7 KB, 5 views)
File Type: zip work.zip (823.8 KB, 5 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 901
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 141
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: append in word document using c#

 
0
  #8
Aug 22nd, 2009
Originally Posted by fafi_ali View Post
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...
Last edited by DdoubleD; Aug 22nd, 2009 at 1:51 pm. Reason: Found the download
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 901
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 141
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: append in word document using c#

 
0
  #9
Aug 22nd, 2009
Originally Posted by fafi_ali View Post
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:
  1. //StreamWriter sWriter = new StreamWriter(fStream);
  2. BinaryWriter sWriter = new BinaryWriter(fStream);
  3. sWriter.Seek(1, SeekOrigin.End);
  4. 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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 901
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 141
DdoubleD DdoubleD is offline Offline
Posting Shark

Re: append in word document using c#

 
0
  #10
Aug 22nd, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC