Monday, June 30, 2008

Tuesday, June 17, 2008

Export Unbound DataGrid To CSV File\Excel with WINDOWS FORM

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=51174&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=289856&SiteID=1

C# - Retrieve Excel Workbook Sheet Names.

///
/// This mehtod retrieves the excel sheet names from
/// an excel workbook.
///

/// The excel file.
/// String[]
private String[] GetExcelSheetNames(string excelFile)
{
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
try
{
// Connection String. Change the excel file to the file you
// will search.
String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + excelFile + ";Extended Properties=Excel 8.0;";
// Create connection object by using the preceding connection string.
objConn = new OleDbConnection(connString);
// Open connection with the database.
objConn.Open();
// Get the data table containg the schema guid.
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

if(dt == null)
{
return null;
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
// Add the sheet name to the string array.
foreach(DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
// Loop through all of the sheets if you want too...
for(int j=0; j < excelSheets.Length; j++)
{
// Query each excel sheet.
}
return excelSheets;
}
catch(Exception ex)
{
return null;
}
finally
{
// Clean up.
if(objConn != null)
{
objConn.Close();
objConn.Dispose();
}
if(dt != null)
{
dt.Dispose();
}
}
}

Monday, June 9, 2008

Connection string to MS access in VBA

http://www.vbforums.com/showthread.php?p=2223896

Oledb Connection string in VBA

Option Explicit

Dim adoConn As ADODB.Connection
Dim adoRst As ADODB.Recordset

Private Sub Command1_Click()
'============================
Dim strConString As String
Dim strSQL As String

'assign connection string
strConString = "Provider=MS Remote;" & _
"Remote Server=http://192.168.1.1;" & _
"Remote Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=MyRemoteDB;Persist Security Info=False"

'initialize connection object variable
Set adoConn = New ADODB.Connection
'open connection
adoConn.Open strConString, "admin", ""

strSQL = "Select * from Orders"

'initialize recordset object variable
Set adoRst = New ADODB.Recordset
With adoRst
.Open strSQL, adoConn, , , adCmdText
If Not .EOF Then
Do While Not .EOF
'read each record here
'...
.MoveNext
Loop
.Close
End If
End With

'destroy recordset object if necessary (or do it when you unload the form)
'Set adoRst = Nothing

'destroy connection object if necessary (or do it when you unload the form)
'Set adoConn = Nothing

End Sub

Wednesday, June 4, 2008

outlook with .net

http://msdn.microsoft.com/en-us/library/aa168022.aspx

reading un read messages from outlook in .net

http://msdn.microsoft.com/en-us/library/ms268996(VS.80).aspx



private void button1_Click(object sender, EventArgs e)
{
Outlook.Application oApp = new Outlook.Application();
// String used for comparison with mail item.
object sClassComp = "IPM.Note";
Outlook.NameSpace oNS = oApp.GetNamespace("MAPI");
Outlook.MAPIFolder oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items oItems = oInbox.Items;
label1.Text="Total : " + oItems.Count;
// Get unread e-mail messages.
oItems = oItems.Restrict("[Unread] = true");
label2.Text = "Total Unread : " + oItems.Count;
Outlook.MailItem oMsg;
Outlook.MailItem[] mailitems = new Microsoft.Office.Interop.Outlook.MailItem[oItems.Count];
//oItems.Application.
int i=0;
string str = "";
foreach (object o in oItems)
{
mailitems[i] = (Outlook.MailItem)o;
i = i + 1;
}
foreach(Outlook.MailItem o in mailitems)
{
//Test to make sure item is a mail item
//and not a meeting request.
if (o != null)
{
if (string.Compare(o.MessageClass.ToString(),sClassComp.ToString(),true)==0)
{
str =(string)o.Subject;
}
}
//oMsg = oItems.Item[i];
// str+=oMsg.Subject;
//Console.WriteLine(oMsg.ReceivedTime);
//Console.WriteLine(oMsg.Body);
//Console.WriteLine("---------------------------");

//Console.WriteLine(i);
//Console.WriteLine(oMsg.SenderName);
//Console.WriteLine(oMsg.Subject);
//Console.WriteLine(oMsg.ReceivedTime);
//Console.WriteLine(oMsg.Body);
//Console.WriteLine("---------------------------");
}
// Clean up.
label3.Text = str;
oApp = null;
oNS = null;
oItems = null;
oMsg = null;
}
}