locked
Writing to a Word Doc from my ASP.Net form RRS feed

  • Question

  • User1879685628 posted

    Hello,

    I am trying to write some HTML content to a Word doc using my ASP.Net form. I have code in place using

    Microsoft.Office.Interop.Wordwhich generates the doc but when I specify the text, i.e. <strong>Something</strong>, it comes through literally, not bolded. How do I get formatted text (HTML) into a Word doc? Here's what I got:

     

                object oMissing = System.Reflection.Missing.Value;
                
                ApplicationClass wordApp = new ApplicationClass();
                Document wordDoc = wordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
                Paragraph para = wordDoc.Paragraphs.Add(ref oMissing);
                para.Range.Text = "<strong>SAMPLE HERE</strong>";
                para.Range.InsertParagraphAfter();
    
                object fileName = "c:\\test.doc";
                wordDoc.SaveAs(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                wordApp.Quit(ref oMissing, ref oMissing, ref oMissing);


    Thanks,

     

    Tuesday, September 7, 2010 5:31 PM

All replies

  • User197322208 posted

    para.Range.Text = "SAMPLE HERE"

    para.range.Bold =True


    On the other hand, I will do this :
    Dim r as Range= para.Range
    r.Text== "SAMPLE HERE"
    r.Bold=true

    Marshall.ReleaseComObject(r);
    r=null;


    Marshall.ReleaseComObject(para);
    para=null;

    //do the same with other objects from Word

    Tuesday, September 7, 2010 11:08 PM
  • User1879685628 posted

    Thanks, but I have a full HTML string I need to put in there, an HTML string the user has modified. I don't know what should be bold, or font size or colored. 

    Wednesday, September 8, 2010 10:00 AM
  • User197322208 posted

    write on disk as a html file and load with Word

    Wednesday, September 8, 2010 10:13 AM
  • User1879685628 posted

    write on disk as a html file and load with Word

     

    I dont have permissions to write to disk, really need to do it all in code/memory.

    Wednesday, September 8, 2010 10:19 AM
  • User1879685628 posted

    So now I'm trying the clipboard approach, and it almost works, but I can't seem to set the content of the clipboard.  If I copy something through Windows, that content (formatted correctly) is pasted into the Word doc. I want to send in the content to be pasted, that's not work. Here's what I have now

     

    public void btnOpenInWord_Click(object sender, EventArgs e)
            {
                try
                {
                    Thread newThread = new System.Threading.Thread(new ParameterizedThreadStart(ThreadMethod));
                    newThread.SetApartmentState(ApartmentState.STA);
                    newThread.Start(RadEditor1.Text);
                }
                catch (Exception ex)
                {
                    ErrorLabel.Text = ex.ToString();
                }
            }
    
            [STAThread]
            static void ThreadMethod(object myHtml)
            {
                try
                {
                    object oMissing = System.Reflection.Missing.Value;
                    string theHtml = myHtml.ToString();
    
                    ApplicationClass wordApp = new ApplicationClass();
                    Document wordDoc = wordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    
                    Paragraph para = wordDoc.Paragraphs.Add(ref oMissing);
                    //para.Range.Text = theHtml;
                    //para.Range.InsertParagraphAfter();
    
                    System.Windows.Forms.Clipboard.SetText(theHtml); // tried adding , System.Windows.Forms.TextDataFormat.Html);
    
                    para.Range.Paste(); //pastes in what I have, not what is setup above.
    
                    object fileName = "c:\\test.doc";
                    wordDoc.SaveAs(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                    wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                    wordApp.Application.Quit(ref oMissing, ref oMissing, ref oMissing);
                    //save word object to SP library
                    //open the word doc so user can edit
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }


     

     

     

     

    Wednesday, September 8, 2010 12:15 PM