Creating a MS Word document in C# .NET - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Creating a MS Word document in C# .NET

RibbletonRibbleton Posts: 152Member
[b][red]This message was edited by Ribbleton at 2006-1-16 8:1:57[/red][/b][hr]
Right, i can create a Word Document in a Windows Application but can't get it to work in a Web Application. I've tried Google with no success.

The code which i am using is

I get a error "Word Could Not Fire the Event"

[code]
//add ref
using System.Reflection;
using Microsoft.CSharp;
using Microsoft.Office.Core;

object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\endofdoc"; /* endofdoc is a predefined bookmark */

Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing); // ERROR LINE

string todaysDate = System.DateTime.Now.ToShortTimeString().ToString();
Word.Paragraph Date;
Date = oDoc.Content.Paragraphs.Add(ref oMissing);
Date.Range.Text = todaysDate;
Date.Range.InsertParagraphAfter();

Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Name";
oPara1.Range.InsertParagraphAfter();

Word.Paragraph oPara2;
oPara2 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara2.Range.Text = "Address 1";
oPara2.Range.InsertParagraphAfter();

Word.Paragraph oPara3;
oPara3 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara3.Range.Text = "Address 2";
oPara3.Range.InsertParagraphAfter();

Word.Paragraph oPara4;
oPara4 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara4.Range.Text = "Address 3";
oPara4.Range.InsertParagraphAfter();

Word.Paragraph oPara5;
oPara5 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara5.Range.Text = "Town";
oPara5.Range.InsertParagraphAfter();

Word.Paragraph oPara6;
oPara6 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara6.Range.Text = "Post Code";
oPara6.Range.InsertParagraphAfter();

Word.Paragraph oPara7;
oPara7 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara7 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara7.Range.Text = "Dear";
oPara7.Range.InsertParagraphAfter();

Word.Paragraph oPara9;
oPara9 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara9 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara9.Range.Text = "SOME TEXT";
oPara9.Range.InsertParagraphAfter();

Word.Paragraph oPara10;
oPara10 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara10 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara10.Range.Text = "1111 2222 3333 4444 ";
oPara10.Range.InsertParagraphAfter();

Word.Paragraph oPara11;
oPara11 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara11 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara11.Range.Text = "Thank you for supporting ";
oPara11.Range.InsertParagraphAfter();

Word.Paragraph oPara12;
oPara12 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara12 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara12.Range.Text = "Yours sincerely";
oPara12.Range.InsertParagraphAfter();

Word.Paragraph oPara13;
oPara13 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara13 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara13.Range.Text = "name";
oPara13.Range.InsertParagraphAfter();
[/code]


Anyone got any ideas. I think it might have something to do with security.

Kind Regards,
Rob



Sign In or Register to comment.