Friday, March 12, 2010

Upload Word Document using Sharepoint Web Services


All,
Below is the sample code to add a Microsoft Word Document inside the Sharepoint Documen Library's folder.

Say i have a Document Library as CoastGuard and it has four folders : MIS,Aviation,IT and Logistics.So i need to add document in these folders using Sharepoint in built web services.

Below is the Code:

using System;

usingSystem.Collections.Generic;

using System.Configuration;

usingSystem.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Xml;

using System.IO;

using System.Collections;

using Microsoft.SharePoint;

using System.Net;

namespace Signals

{

public partial class Form1:Form

{

public Form1()

{

InitializeComponent();

}

private voidForm1_Load(objectsender,EventArgs e)

{

GetFiles();

GetFoldersList();

}

private void GetFoldersList()

{

wsLists.Lists spList =newSignals.wsLists.Lists();

spList.Credentials = System.Net.CredentialCache.DefaultCredentials;

stringspUrl=ConfigurationSettings.AppSettings["SPFolders"].ToString();

spList.Url ="http://Win08r2/Signals/_vti_bin/Lists.asmx";

XmlNode listnodes = spList.GetListItems("CoastGuard", null, null, null, null, null,null);

foreach (XmlNode nodeinlistnodes)

{

if (node.Name == "rs:data")

{

for (int i = 0; i <>

{

if (node.ChildNodes[i].Name == "z:row")

{

listView1.Items.Add(node.ChildNodes[i].Attributes["ows_LinkFilename"].Value);

}

}

}

}

}

private void GetFiles()

{

string strPath ="..\\..\\Documents";

DirectoryInfo di =newDirectoryInfo(strPath);

FileInfo[] fi = di.GetFiles();

foreach (FileInfo f in fi)

{

listView2.Items.Add(f.Name);

}

}

privatevoidbutton1_Click(objectsender,EventArgs e)

{

wsLists.Lists spList =newSignals.wsLists.Lists();

spList.Credentials = System.Net.CredentialCache.DefaultCredentials;

spList.Url ="http://Win08r2/_vti_bin/Lists.asmx";

listView1.Items[0].Selected =true;

string DocFileName = "";

foreach (ListViewItem linlistView2.SelectedItems)

{

DocFileName = l.Text.ToString();

}

if (DocFileName != "")

{

if(listView1.CheckedItems.Count > 0)

{

for (int i = 0; i <>

{

WebClient WC =newWebClient();

WC.Credentials =CredentialCache.DefaultCredentials;

WC.UploadData("http://Win08r2/signals/CoastGuard/"+ listView1.CheckedItems[i].Text + "/" + DocFileName, "PUT", StreamFile(@"..\\..\\Documents\" + DocFileName));

}

MessageBox.Show("Files moved successfully");

}

else

{

MessageBox.Show("Please Select At Least One Folder");

}

}

else

{

MessageBox.Show("Please Select a File");

}

}

private byte[] StreamFile(stringfilename)

{

FileStream fs =newFileStream(filename,FileMode.Open,FileAccess.Read);

// Create a byte array of file stream length

byte[] ImageData =newbyte[fs.Length];

//Read block of bytes from stream into the byte array

fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));

//Close the File Stream

fs.Close();

return ImageData; //return the byte data

}

privatevoidbutton2_Click(objectsender,EventArgs e)

{

foreach (ListViewItem liinlistView1.Items)

{

li.Checked = true;

}

}

privatevoidbutton3_Click(objectsender,EventArgs e)

{

foreach (ListViewItem liinlistView1.Items)

{

li.Checked = false;

}

}

}

}

Here's the Result:

Form1 screenshot.Multiple folders can be selected,but only single file can be selected.


Document uploaded successfully..



Regards,

Nitin Sharma


Fetch Temp directory of Windows using VBA macros

Hey,
I am working on VBA Macros for a small module.I came across a need to find the temp directory path using VBA.so here it is:

Public Function GetTempDir() As String
Dim sRet As String, lngLen As Long
'create buffer
sRet = String(MAX_PATH, 0)

lngLen = GetTempPath(MAX_PATH, sRet)
If lngLen = 0 Then Err.Raise Err.LastDllError
GetTempDir = Left$(sRet, lngLen)
End Function



Access above function here as :
Sub TestMacro

Dim strFilePath As String
strFilePath = GetTempDir()
End Sub

Regards,
Nitin Sharma

Adding Images in Word Document using C#.NET

Hey,
I came across a requirement where i need to add Images in the Microsoft word document using C# code in Windows application.


I searched and wrote the code as follows but before that :

Step 1) do add reference to Microsoft Word Object Library.

Step 2) Add using Microsoft.Office.Interop.Word;
using System.IO; namespaces on top.


Code in Form1.cs:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace Sample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
object missing = System.Reflection.Missing.Value;
object Visible=true;
object start1 = 0;
object end1 = 0;

ApplicationClass WordApp = new ApplicationClass();
Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
Range rng = adoc.Range(ref start1, ref missing);

try
{

foreach (ListViewItem Li in listView1.Items)
{

FileInfo Fi = new FileInfo(Li.Text);

rng.InlineShapes.AddPicture(Fi.ToString(), ref missing, ref missing, ref missing);

}

rng.Font.Name = "Georgia";
rng.InsertAfter("Hello World!");
object filename = @"D:\"+textBox1.Text+".docx"; //Saves in D directory


adoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);


}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void butBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
string [] FileNames = openFileDialog1.FileNames;
foreach (string nameOfFile in FileNames)
{
listView1.Items.Add(nameOfFile);
}

}
}
}


Thats it..Now check your directory(here it is D directory)


Below is the Screen Shot:













Happy Coding..!
Regards,
Nitin Sharma

Wednesday, February 24, 2010

How to Convert Numerics/Digits into Words using C# .NET?



Hi All,
I came into thinking mode of having a system in where i enter "Numeric" values and get converted into "Words" within a single click using .NET framework.
For eg: 20,212 to " Twenty Thousand Two hundred Twelve" .
I googled and found the solution.
Below is the code (It is a C# Windows Application)


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Numeric;

namespace TestApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{


}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = changeToWords(textBox1.Text, false);
}

public String changeNumericToWords(double numb)
{
String num = numb.ToString();
return changeToWords(num, false);
}
public String changeCurrencyToWords(String numb)
{
return changeToWords(numb, true);
}
public String changeNumericToWords(String numb)
{
return changeToWords(numb, false);
}
public String changeCurrencyToWords(double numb)
{
return changeToWords(numb.ToString(), true);
}
private String changeToWords(String numb, bool isCurrency)
{
String val = "", wholeNo = numb, points = "", andStr = "", pointStr = "";
String endStr = (isCurrency) ? ("Only") : ("");
try
{
int decimalPlace = numb.IndexOf(".");
if (decimalPlace > 0)
{
wholeNo = numb.Substring(0, decimalPlace);
points = numb.Substring(decimalPlace + 1);
if (Convert.ToInt32(points) > 0)
{
andStr = (isCurrency) ? ("and") : ("point");// just to separate whole numbers from points/cents
endStr = (isCurrency) ? ("Cents " + endStr) : ("");
pointStr = translateCents(points);
}
}
val = String.Format("{0} {1}{2} {3}", translateWholeNumber(wholeNo).Trim(), andStr, pointStr, endStr);
}
catch { ;}
return val;
}
private String translateWholeNumber(String number)
{
string word = "";
try
{
bool beginsZero = false;//tests for 0XX
bool isDone = false;//test if already translated
double dblAmt = (Convert.ToDouble(number));
//if ((dblAmt > 0) && number.StartsWith("0"))
if (dblAmt > 0)
{//test for zero or digit zero in a nuemric
beginsZero = number.StartsWith("0");
int numDigits = number.Length;
int pos = 0;//store digit grouping
String place = "";//digit grouping name:hundres,thousand,etc...
switch (numDigits)
{
case 1://ones' range
word = ones(number);
isDone = true;
break;
case 2://tens' range
word = tens(number);
isDone = true;
break;
case 3://hundreds' range
pos = (numDigits % 3) + 1;
place = " Hundred ";
break;
case 4://thousands' range
case 5:
case 6:
pos = (numDigits % 4) + 1;
place = " Thousand ";
break;
case 7://millions' range
case 8:
case 9:
pos = (numDigits % 7) + 1;
place = " Million ";
break;
case 10://Billions's range
pos = (numDigits % 10) + 1;
place = " Billion ";
break;
//add extra case options for anything above Billion...
default:
isDone = true;
break;
}
if (!isDone)
{//if transalation is not done, continue...(Recursion comes in now!!)
word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos));
//check for trailing zeros
if (beginsZero) word = " and " + word.Trim();
}
//ignore digit grouping names
if (word.Trim().Equals(place.Trim())) word = "";
}
}
catch { ;}
return word.Trim();
}
private String tens(String digit)
{
int digt = Convert.ToInt32(digit);
String name = null;
switch (digt)
{
case 10:
name = "Ten";
break;
case 11:
name = "Eleven";
break;
case 12:
name = "Twelve";
break;
case 13:
name = "Thirteen";
break;
case 14:
name = "Fourteen";
break;
case 15:
name = "Fifteen";
break;
case 16:
name = "Sixteen";
break;
case 17:
name = "Seventeen";
break;
case 18:
name = "Eighteen";
break;
case 19:
name = "Nineteen";
break;
case 20:
name = "Twenty";
break;
case 30:
name = "Thirty";
break;
case 40:
name = "Fourty";
break;
case 50:
name = "Fifty";
break;
case 60:
name = "Sixty";
break;
case 70:
name = "Seventy";
break;
case 80:
name = "Eighty";
break;
case 90:
name = "Ninety";
break;
default:
if (digt > 0)
{
name = tens(digit.Substring(0, 1) + "0") + " " + ones(digit.Substring(1));
}
break;
}
return name;
}
private String ones(String digit)
{
int digt = Convert.ToInt32(digit);
String name = "";
switch (digt)
{
case 1:
name = "One";
break;
case 2:
name = "Two";
break;
case 3:
name = "Three";
break;
case 4:
name = "Four";
break;
case 5:
name = "Five";
break;
case 6:
name = "Six";
break;
case 7:
name = "Seven";
break;
case 8:
name = "Eight";
break;
case 9:
name = "Nine";
break;
}
return name;
}
private String translateCents(String cents)
{
String cts = "", digit = "", engOne = "";
for (int i = 0; i < digit =" cents[i].ToString();" engone = "Zero" engone =" ones(digit);">


below is the screen Shot :






Thanks,
Nitin Sharma

How to know that your machine/system is 64 bit capable?

Hi All,


If you want to know all about your PC machine hardware insights,then this post is for you.


I came across to this tool when i was in need of installing Sharepoint 2010 but need to check that my machine was 64 bit capable or not...and my mentor found this tool on Google :)






Download the Download version 1.53 32-bit (.zip, no install, english) if your machine is 32 bit and you want to check that is it 64 bit capable?




Also running on .exe it display hardware insights like Graphics,ram(its slots),processor etc etc...



I really found it useful.


Thanks,

Nitin Sharma