Monday, October 3, 2011

Save your word document as PDF file using Office 2007 or Letter version

0 comments

There are many tools available to covert your word document to PDF and multiple ways availbale to do that but i got the eaisier way , would like to share here..

Prerequisite is that you need office 2007 or letter version in your system.
Download Save-as-PDF addon for your office version from here and installed It.

Now when you save any word document then Save as (.PDF) option will be available and you can save it in pdf format, that's it!

Now i would like to talk here how you can save it as PDF from C# code.

You need to first add Refernce of office introp dll as per shown in below figure.

 
And do code as per below...

using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
...
// Create a new Microsoft Word application object
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;
// Get list of Word files in specified directory
DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder");
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");
word.Visible = false;
word.ScreenUpdating = false;
foreach (FileInfo wordFile in wordFiles)
{
    // Cast as Object for word Open method
    Object filename = (Object)wordFile.FullName;
    // Use the dummy value as a placeholder for optional arguments
    Document doc = word.Documents.Open(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);
    doc.Activate();
    object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
    object fileFormat = WdSaveFormat.wdFormatPDF;
    // Save document into PDF Format
    doc.SaveAs(ref outputFileName,
        ref fileFormat, 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);
    // Close the Word document, but leave the Word application open.
    // doc has to be cast to type _Document so that it will find the
    // correct Close method.                
    object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
    ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
    doc = null;
}
// word has to be cast to type _Application so that it will find
// the correct Quit method.
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;

You have to make change in above code as per your requirements.

Saturday, October 1, 2011

DateTime Manipulation in SQL – Convert Universal time to local time

0 comments

There are many datetime manipulation functions available in sql server , we will look at it one by one in this series of "DateTime Manipulation in SQL".

You all are aware of UTC which stands for Universal Time Coordinated , some are also known as GMT which stands for Greenwhich Mean Time , this are standard date & time , we can get our local time by adding offset to it (e.g for india it is +5:30).

At time of building query in SQL you need to convert UTC time local time, and you can do it following way

getutcdate() return UTC datetime


getdate() return local datetime (as per your system configuration)


dateadd() function is to add parts (hour, day, minute etc..) to any datetime object.


Sunday, August 28, 2011

Store your document on cloud with Google Docs

0 comments

We are using excel , word in daily life to manage our data like contact information , personal information , business documents etc.
We stored that data on our local computer so we need to take care about for backup of all this documents in case of system crash or in any other case by which we can lost that document.
Also when we need that documents on another system then we need to copy past that documents through USB or any other device.
Google docs is cloud based service to store our documents on Google’s server so we do not need to take care about to backup or system failure and we can access it from any computer or any location, You required only internet connection and Google account.
Take a look at http://docs.google.com (You can use your google credential).
More tips on google docs in future..