September 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Using Visual Studio to Write Word Add-ins

  • August 4, 2009
  • By Jani Järvinen
  • Send Email »
  • More Articles »

Converting currency values

The next part of functionality in the sample add-in is the ability to convert currencies. In this case, the sample application can take a USD dollar amount, and convert it to Euros (€). This is done with the help of a free currency conversion service available at Nimacon.net.

To use this service, you will first need to add a service reference to the project. This is done easily using the Add Service Reference dialog box in Visual Studio, launched for example using the similarly named command in the Project menu. Once the reference has been added, the following code can be used to convert a USD amount into Euros:

  float usdAmount = float.Parse(usdAmountTextBox.Text);
  CurrencyRateService.CurrencyRateServiceClient
    client = new CurrencyRateService.CurrencyRateServiceClient();
  float euroAmount = client.GetEuroAmount(
    "USD", usdAmount);
  MessageBox.Show("$" + usdAmount + " is " +
    euroAmount + " €.");

First, the code takes the USD amount the user has entered, and converts the string to a float value. Then, an instance of the web service client class is constructed (Visual Studio automatically creates the CurrencyRateServiceClient class when the service reference is added). The next step is to make the actual web service (SOAP) call using the GetEuroAmount method of the service. This method takes in a currency amount in any supported currency, and a string specifying which currency the value is in. The return value is the amount in Euros with the current exchange rate.

Publishing to a blog

In a corporate setting, sharing information is a necessity. For instance, if a member of a sales team creates or updates an important offer, the other members of the team should know about it. However, letting other people know that you've updated a document is an additional, often manual step. Why couldn't the system automatically send a status update to colleagues whenever an important document changes?

In the sample application, this need to inform others is addressed via a blog. Whenever the user saves the offer (i.e. commits the updates made), the add-in automatically publishes a new entry on a Google Blogger blog, which is available at www.blogspot.com (Figure 8). The Blogger service uses a free HTTP based API, which is divided into two major parts: authentication with the generic Google authentication APIs, and the specific Blogger interfaces to submit blog posts.



Click here for larger image

Figure 8. The sample application can publish to a Google Blogger blog.

Since the focus of this article is in Word add-ins and not the Google interfaces, only a short description follows. As mentioned previously, the first part of using the Blogger service is to authenticate properly. This is done by sending a HTTP POST request to the authentication URL, and passing in the username and password for the service. In exchange, the service returns an authentication token, which must be passed when submitting a post to the user's blog with the Blogger API.

The following is a snippet of the code to authenticate with the service. The Google developer documentation for the service is publicly available on the Internet; see the Links section for details.

  
  WebClient web = new WebClient();
  try
  {
    NameValueCollection postParams =
      new NameValueCollection();
    postParams.Add("accountType", accountType);
    postParams.Add("Email", email);
    postParams.Add("Passwd", password);
    postParams.Add("service", service);
    postParams.Add("source", sourceApp);
    byte[] result = web.UploadValues(
      loginUrl, "POST", postParams);
    string resultStr =
      Encoding.ASCII.GetString(result);
    string[] tokens = resultStr.Split();
    bloggerAuthToken = tokens[2].Substring(5);
  }
  finally
  {
    web.Dispose();
  }
  

With the authentication token available (the bloggerAuthToken member at the end of the code snippet), the next step is to call the actual posting function. This can be done with code similar to the following:

  public static void SubmitBlogPost(
    string title, string body)
  {
    string xml;
    MemoryStream xmlStream =
      new MemoryStream();
    try
    {
      XmlWriterSettings settings =
        new XmlWriterSettings();
      settings.Indent = true;
      XmlWriter writer = XmlWriter.Create(
        xmlStream, settings);
      try
      {
        writer.WriteStartElement(
          "entry", "http://www.w3.org/2005/Atom");
        ...
        writer.WriteEndElement();
      }
      finally
      {
        writer.Close();
      }
      xml = Encoding.ASCII.GetString(xmlStream.ToArray());
    }
    finally
    {
      xmlStream.Dispose();
    }
    WebClient web = new WebClient();
    try
    {
      web.Headers.Add("Content-Type", "application/atom+xml");
      web.Headers.Add("Authorization", "GoogleLogin auth=" +
        bloggerAuthToken);
      web.Headers.Add("GData-Version", "2");
      web.Encoding = Encoding.UTF8;
      string response = web.UploadString(submitUrl, "POST", xml);
    }
    finally
    {
      web.Dispose();
    }
  }

Here, the code first constructs an XML based Atom entry that the Blogger service accepts (the actual lines are omitted for brevity), and then uses a WebClient to connect to the HTTP service. Note how several custom headers have to be added to the HTTP request for the Google API to accept it.





Page 3 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel