Wednesday, May 16, 2012

The type or namespace name 'Interop' does not exist in the namespace 'Microsoft.Office' error

 

I encountered the following error "The type or namespace name 'Interop' does not exist in the namespace 'Microsoft.Office' "

Found a working solution in one of the posts in this thread.

Here’s what I did:

  1. Download the Microsoft Office 2010: Primary Interop Assemblies Redistributable at this link.
  2. Installed the software; it would extract as a setup file named “o2010pia.msi”
  3. Navigated to the “.NET” tab and added the reference “Microsoft.Office.Interop.Word”.  You can also manually add the reference under the
    <assemblies>
    tag
    <add assembly="Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71E9BCE111E9429C"/>

2012-05-16_163730


If all goes well, it should get rid of the error message just as it did for me :)

How to solve the "The type or namespace 'Packaging' does not exist in the namespace 'System.IO'" error

If you encounter this error "The type or namespace 'Packaging' does not exist in the namespace 'System.IO', here's an easy way to solve it.

Just locate the
<assemblies>
 tag in your web.config file and add this line.  It should resolve the compilation errors straightaway.
<add assembly="WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

Wednesday, December 21, 2011

New version of WordXML document using OpenXML SDK 2.0 with bold, color and font size formatting


I first dabbled with the OpenXML SDK 2.0 in January 2011.

At that time, there was a problem which I could not solve: specifying different font sizes, font colors and bolding effects for different words on the same line.

I managed to achieve different formatting effects for sentences on different lines, but never managed to get it working if the words were on the same line.

Today, I finally figured out how to do it, and scroll down if you are interested to look at the code.

Oh, and do remember to add in the reference for OpenXML SDK and add the imports.

A screenshot of how the generated Word document looks like:

2011-12-21_214518
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
public void GenerateWord(){
            WordprocessingDocument doc = WordprocessingDocument.Create(filename, WordprocessingDocumentType.Document);
            MainDocumentPart mainPart = doc.AddMainDocumentPart();
            mainPart.Document = new Document();
            Body body = new Body();

            /**** Create the contents ****/
            DocumentFormat.OpenXml.Wordprocessing.Run run1, run2, run3, run4;
            Paragraph para;

            string text1 = "OpenXML Demo";
            string text2 = "By Dora Chua";
            string text3 = "Code modified from original at : ";
            string text4 = "http://msdn.microsoft.com/en-us/library/bb448854.aspx";
            string text5 = "Alex";


            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties1 = new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new RunFonts() { Ascii = "Times New Roman" });
            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties2 = new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new RunFonts() { Ascii = "Arial" });
            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties3 = new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new RunFonts() { Ascii = "Script" });
            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties4 = new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new RunFonts() { Ascii = "Courier New" });
            DocumentFormat.OpenXml.Wordprocessing.RunProperties runProperties5 = new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new RunFonts() { Ascii = "Courier New" });
            
            DocumentFormat.OpenXml.Wordprocessing.FontSize fs1 = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
            DocumentFormat.OpenXml.Wordprocessing.Color c1 = new DocumentFormat.OpenXml.Wordprocessing.Color();
            DocumentFormat.OpenXml.Wordprocessing.Bold b1 = new DocumentFormat.OpenXml.Wordprocessing.Bold();

            DocumentFormat.OpenXml.Wordprocessing.FontSize fs2 = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
            DocumentFormat.OpenXml.Wordprocessing.Color c2 = new DocumentFormat.OpenXml.Wordprocessing.Color();
            DocumentFormat.OpenXml.Wordprocessing.Bold b2 = new DocumentFormat.OpenXml.Wordprocessing.Bold();

            DocumentFormat.OpenXml.Wordprocessing.FontSize fs3 = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
            DocumentFormat.OpenXml.Wordprocessing.Color c3 = new DocumentFormat.OpenXml.Wordprocessing.Color();
            DocumentFormat.OpenXml.Wordprocessing.Bold b3 = new DocumentFormat.OpenXml.Wordprocessing.Bold();

            DocumentFormat.OpenXml.Wordprocessing.FontSize fs4 = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
            DocumentFormat.OpenXml.Wordprocessing.Color c4 = new DocumentFormat.OpenXml.Wordprocessing.Color();
            DocumentFormat.OpenXml.Wordprocessing.Bold b4 = new DocumentFormat.OpenXml.Wordprocessing.Bold();


            DocumentFormat.OpenXml.Wordprocessing.FontSize fs5 = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
            DocumentFormat.OpenXml.Wordprocessing.Color c5 = new DocumentFormat.OpenXml.Wordprocessing.Color();
            DocumentFormat.OpenXml.Wordprocessing.Bold b5 = new DocumentFormat.OpenXml.Wordprocessing.Bold();


            /*** Format text1   ***/
            run1 = new DocumentFormat.OpenXml.Wordprocessing.Run();
            fs1.Val = "20";
            c1.Val = "green";
            b1.Val = true;
           
            runProperties1.Append(fs1);
            runProperties1.Append(c1);
            runProperties1.Append(b1);

            run1.Append(runProperties1);
            run1.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(text1));

            para = new Paragraph(run1);
            body.AppendChild(para);


            /*** Format text2 ***/
            run2 = new DocumentFormat.OpenXml.Wordprocessing.Run();

            fs2.Val = "20";
            c2.Val = "black";
            b2.Val = false;

            runProperties2.Append(fs2);
            runProperties2.Append(c2);
            runProperties2.Append(b2);

            run2.Append(runProperties2);
            run2.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(text2));

            para = new Paragraph(run2);
            body.AppendChild(para);

            /*** Format text3 and text4 as one paragraph ***/
            run3 = new DocumentFormat.OpenXml.Wordprocessing.Run();

            fs3.Val = "15";
            c3.Val = "black";
            b3.Val = false;
            runProperties3.Append(fs3);
            runProperties3.Append(c3);
            runProperties3.Append(b3);

            run3.Append(runProperties3);
            run3.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(text3));

            fs4.Val = "15";
            c4.Val = "red";
            b4.Val = true;
            runProperties4.Append(fs4);
            runProperties4.Append(c4);
            runProperties4.Append(b4);

            run3.Append(runProperties4);
            run3.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(text4));

            para = new Paragraph(run3);
            body.AppendChild(para);


            /*** Format the user-typed text ***/
            fs5.Val = "30";
            c5.Val = "blue";
            b5.Val = false;
            runProperties5.Append(fs5);
            runProperties5.Append(c5);
            runProperties5.Append(b5);

            run4 = new DocumentFormat.OpenXml.Wordprocessing.Run();

            run4.Append(runProperties5);
            run4.Append(new DocumentFormat.OpenXml.Wordprocessing.Text(text5));

            para = new Paragraph(run4);
            body.AppendChild(para);


            /*** Append the entire body ****/
            mainPart.Document.Append(body);

            /* Save the results and close */
            mainPart.Document.Save();
            doc.Close();
}//end GenerateWord
        

Monday, October 24, 2011

Create a hyperlink using a QueryString or Form parameter in C# ASP.NET

 

This is a very short code snippet which shows you how you can create a hyperlink using a QueryString or Form parameter received from a referring page.

Note that you would need to do two things in order for this to work:

Step 1
First, paste the code below in the HTML markup.  The code below assumes that you are using a querystring parameter called “ID” that was passed over from a previous page.

<asp:HyperLink Target="_blank" ID="HyperLinkViewFeedback" runat="server"
NavigateURL='<%# String.Concat("~/Feedback/ViewFeedback.aspx?ID=",Request.QueryString["ID"]) %>'>View Feedback</asp:HyperLink>

Step 2
Add the “Page.DataBind()” method to the OnLoad of the code-behind file.

protected void Page_Load(object sender, EventArgs e)
{
Page.DataBind();
}

After that, you should see the hyperlink with the ID automatically appended as follows:


2011-10-24_132412

Wednesday, February 23, 2011

How to: Enable User Password Recovery Using the ASP.NET PasswordRecovery Control

How to: Enable User Password Recovery Using the ASP.NET PasswordRecovery Control
How to: Install and Configure SMTP Virtual Servers in IIS 6.0
Solving exceptions when doing password recovery

asp.net - Hashed passwords and PasswordRecovery control - Stack Overflow

asp.net - Hashed passwords and PasswordRecovery control - Stack Overflow: "UPDATE:

1) For some reason it works now. Namely, if we set requiresQuestionAndAnswer to false, then PR also sends email to firstUser


2) If passwords are stored in hashed form, then if:

a) enablePasswordRetrieval='true' and enablePasswordReset is set to either true or false --> PR generates exception
b) if enablePasswordRetrieval='false' and enablePasswordReset='false' --> PR generates exception
c) if enablePasswordRetrieval is set to false and enablePasswordReset is set to true, then PR automatically generates new pwd and emails it.


Similarly, if pwd is not hashed, but we have enablePasswordRetrieval='false', then enablePasswordReset must be set to true (so that PR generates a new pwd and emails it), else we get an exception"