Wednesday, 24 July 2013

Conversion Of .rft, .txt And .doc File In To .pdf File In Vb.net


I write the program for conversion of .txt and .rtf in to the .pdf files. for this  program we
need to create a function  of conversion. Otherwise we need to use third party utility and
call the function from it,in our program.
                                                 Here i do the same ,i use third party utility in my program.
My project is in VS 2010 window form.let's see the program step by step ,what i do:-
1)Create your own functions(.dll) or download third party dll. and locate in  your system folder
    C:\Windows\System32.
2)Open your project and right click on project and click on 'Add Reference' form 'Browse' 
    tab in new dialog box select that(downloaded dll).
3)Drop the one button in your window form,write the code in Click_event.
4)Import that  dll in porgram and write the below code:-
5)In this program 'SautinSoft.PdfMetamorphosis' is the third party dll and 'RtfToPdfConvertFile'
   function.

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.IO
Imports System.Windows.Forms.Form

Public Class Form1
    
    Private Sub _Open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
                                                                                                           Handles _Open.Click
        InitializeComponent()
        Dim openFile As New OpenFileDialog
        Dim p As New SautinSoft.PdfMetamorphosis()
        openFile.Filter = "All Files (*.*)|*.*"
        openFile.ShowDialog()
        p.PageStyle.PageOrientation.Landscape()

        'specify header in HTML format
        p.Header.Html("<b>Sample header in HTML format</b>")

        'specify footer in RTF format
        p.Footer.Rtf("{\b Bold footer}")

        'specify page numbers
        p.PageStyle.PageNumFormat = "Page {page} of {numpages}"

        If p IsNot Nothing Then
            Try
                'Dim str As String
                TextBox1.Text = openFile.FileName
                Dim rtfPath As String = TextBox1.Text
                Dim pdfPath As String = "D:\test.pdf"
                Dim pdfPath1 As String = "D:\test1.pdf"
                p.RtfToPdfConvertStringToFile("hello shiv", pdfPath1)
                Dim i As Integer = p.RtfToPdfConvertFile(rtfPath, pdfPath)

                If i = 0 Then
                    System.Diagnostics.Process.Start(pdfPath)
                Else
                    MessageBox.Show("An error occured during converting RTF to PDF!")
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub   
End Class

Wednesday, 17 July 2013

How To Access Web Services From Service Reference In Vb.net

Access WebServices  In Vb.net

Here i describe about the web service access from vb.net, some time our requirement data 
is depend upon in web service,Then  we get the data from available function of web service.
I write here steps of access the web services:-
1)Open the 'VS 2010' ,create a new project of  'Excel Addins 2010' .
2)Add  a Ribbon and Button , Write a code on Click_Event.
3)Before the writing  ,we need to add 'service reference' ,for exanple.
   Here i create a project named 'BvdDataAddIn'.
4)Right click on project select  "Add Service Reference" ,open a new window 
  asking for Url , you just feed  the url address  of web services and give it's 
  (like i give 'BvdServiceReference').  click 'Ok' button.
5)After add 'Web Reference' ,double click on 'FindByBvdID' button. 
6)Here I write the code  according my 'Web Reference'(You can change it).

Imports Microsoft.Office.Tools.Ribbon
Imports System
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports System.Windows.Forms
'Imports AddinExpress.MSO
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Data
Imports System.IO
Imports System.Diagnostics
Imports BvdDataAddIn.BvdServiceReference

Public Class BvdID
    Dim activesheet As Excel.Worksheet
    Dim range As Excel.Range

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As       
                              Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs)  Handles Button1.Click
        Try
            Dim service As New RemoteAccessSoapClient()
            Dim selectionResult As BvdServiceReference.SelectionResult
            Dim sessionHandle As String
            Dim bvddata As New BvdDataAddIn.BvdID
            Dim row As Integer
            Dim bvdId As String
            Dim data As String
            Dim dataSet As New System.Data.DataSet
            Dim dataTable As DataTable

            sessionHandle = service.Open("ijameel", "Pwcwelcome1")
            activesheet = Globals.ThisAddIn.Application.ActiveSheet
            'Lastrow = activesheet.Cells.Find("*", SearchOrder:=Excel.XlSearchOrder.xlByRows,  
                                 SearchDirection:=Excel.XlSearchDirection.xlPrevious).Row
            For row = 2 To 2
                'range = activesheet.Cells("A" & row, 1)

                'If range.Value = vbNull Then
                'End If
                bvdId = "BE0878480104"

                'Try
                '    bvdId = range.Value.ToString()
                'Catch ex As Exception
                '    MsgBox(ex.ToString)
                'End Try

                'Marshal.ReleaseComObject(range)
                If (String.IsNullOrEmpty(bvdId)) Then
                End If

                selectionResult = service.FindByBVDId(sessionHandle, bvdId)
                If selectionResult.SelectionCount <> 0 Then
                    data = service.GetData(sessionHandle, selectionResult, "SELECT 
                                NAME,ADDR,ADDR2,ADDR3,CITY,POSTCODE,STATE,COUNTY,
                                 COUNTRY,WEBSITE FROM RemoteAccess.A", 0, 1,
                                 BvdServiceReference.ResultFormatting.MSDataS et)
                    dataSet.ReadXml(New StringReader(data))
                    dataTable = dataSet.Tables(0)
                    activesheet.Cells(row, 2).Value = dataTable.Rows(0).Item(0)
                    activesheet.Cells(row, 3).Value = dataTable.Rows(0).Item(1)
                    activesheet.Cells(row, 4).Value = dataTable.Rows(0).Item(2)
                    activesheet.Cells(row, 5).Value = dataTable.Rows(0).Item(3)
                    activesheet.Cells(row, 6).Value = dataTable.Rows(0).Item(4)
                    activesheet.Cells(row, 7).Value = dataTable.Rows(0).Item(6)
                    activesheet.Cells(row, 8).Value = dataTable.Rows(0).Item(7)
                    activesheet.Cells(row, 9).Value = dataTable.Rows(0).Item(8)
                    activesheet.Cells(row, 10).Value = dataTable.Rows(0).Item(9)
                End If
            Next
            service.Close(sessionHandle)

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

7)Run the program and click on 'FindByBvdID' ,your data will display on worksheet.

Thursday, 6 June 2013

Call C Function On Vb.net

Call C Dll File Using Vb.net 

Here I have some steps for calling a functions of C language in vb.net:
1.Write a program and create a dll  file for it.
2.Open the VS2010 create a project and add a module in module write the program
    as shown  in figure:
3.You can  able to access the required function in this program the Dll name is "ishan.dll" and
    function name which we want to access, is 'add'.

Note:-
   After creating the dll file we can keep it in three place
    a.Past here "C:\Windows\System32".
    b.Keep in 'bin\Debug' folder of project directory Like:
       "C:\Users\Computer_name\Documents\Visual Studio 2010\
          Projects\WindowsAppForDll\WindowsAppForDll\bin\Debug".
   c.The third way is set the environment path ,give the path value where you store the file.
      path is separated by ';'.

Friday, 10 May 2013

Download Attachments From Email In Outlook To Local System.


                                          Download Attachments From Email
You can download the  attached item of emails in outlook and save it  some where. Just try this
code in your local system.
     
        Dim objOutlook As New Outlook.Application
        Dim objNS As Outlook.NameSpace = objOutlook.GetNamespace("MAPI")
        Dim objInboxFolder As Outlook.MAPIFolder = Nothing
        Dim objXYZFolder As Outlook.MAPIFolder = Nothing
        Dim objFailedFolder As Outlook.MAPIFolder = Nothing
        Dim emails As Items
        Dim email As MailItem
        Dim Num_mails As Integer = 0
        Dim email_attachment As Attachment
        Dim extension As String

        objInboxFolder = objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        objXYZFolder = objInboxFolder.Folders("Valuehire")
        objFailedFolder = objXYZFolder.Folders("Failed")

        emails = objXYZFolder.Items
        Num_mails = emails.Count

        Dim MyDocumentsDir As String = vbNull
        Dim DownloadDirName As String = vbNull
       
        MyDocumentsDir = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        DownloadDirName = MyDocumentsDir + "\Outlook Files\Valuehire\Downloads"

        If Num_mails > 0 Then
            For Each email In emails
                If email.Class = OlObjectClass.olMail Then
                    For Each email_attachment In email.Attachments
                        Dim filename As String = DownloadDirName + "\" + email_attachment.FileName
                        extension = Path.GetExtension(email_attachment.FileName)

                        If (extension = ".doc" Or extension = ".docx" Or extension = ".pdf" Or extension = ".rtf") Then
                            Try
                                email_attachment.SaveAsFile(filename)                               
                                MsgBox("File is downloaded")
                                Catch ex As System.Exception
                                If ex.Message.Contains("530") Then
                                    MsgBox("Your account is invalid. Please try again or contact your Administrator.")                                   
                                Else
                                     MsgBox("File is not downloaded")                                   
                                End If
                                Return
                            End Try
                        End If
                    Next email_attachment
                End If
            Next email
        End If

Notes- You can see above, in the first underline is pointed  to destination place where the file
 is store and second one for save the file in destination folder.

Friday, 3 May 2013

Connecting And Testing To A FTP Server


                            Connection To FTP Server  From Vs2010              

In this  blog i discuss on connection for ftp server, here is a small code ,i tried this in outlook
add_ins with successful result.But before  try this code you need server ftp address and  path
where file is located.

Dim ftpRequest As System.Net.FtpWebRequest
Dim clsStream As System.IO.Stream
Dim bFile() As Byte
Dim AccountName_var As String
Dim Password_var As String
Dim email_attachment As Attachments
Dim filename As String = DownloadDirName + "\" + email_attachment.FileName                       
  Try
                                email_attachment.SaveAsFile(filename)
                                ftpRequest = DirectCast(System.Net.WebRequest.Create("ftp://" + "xxx.xxx.xxx.xx" + "/" +
                                                        EmailID_var + "/" + email_attachment.FileName), System.Net.FtpWebRequest)
                                ftpRequest.Credentials = New System.Net.NetworkCredential(AccountName_var, Password_var)
                                ftpRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
                                bFile = System.IO.File.ReadAllBytes(filename)
                                clsStream = ftpRequest.GetRequestStream()
                                clsStream.Write(bFile, 0, bFile.Length)
                                clsStream.Close()
                                clsStream.Dispose()
 Catch ex As System.Exception
                                If ex.Message.Contains("530") Then
                                    MsgBox("Your account is invalid. Please try again or contact your Administrator.")
                                Else
                                    MsgBox(ex.Message)
 End Try
               
Here the  'filename' is name of file with the file's path and 'xxx.xxx.xxx.xx' is replace by the ip address.

                                      Testing The Connection To FTP Server
After the connection we just test the connection,is it working or not. you see the highlighted 
yellow line ,this is the simple way for check the ftp connection.

Dim ValuehireRegKey As RegistryKey
        Dim AccountName_var As String
        Dim Password_var As String
        Dim EmailID_var As String
        ValuehireRegKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Office\
                                                                                                         Outlook\Addins\Valuehire", True)
        AccountName_var = ValuehireRegKey.GetValue("Account")
        EmailID_var = ValuehireRegKey.GetValue("Email")
        Password_var = ValuehireRegKey.GetValue("Password")
        ValuehireRegKey.Close()
        Dim ftpRequest As System.Net.FtpWebRequest
        ftpRequest = DirectCast(System.Net.WebRequest.Create("ftp://118.139.182.77/" + 
                                                   EmailID_var + "/"), System.Net.FtpWebRequest)
        ftpRequest.Credentials = New System.Net.NetworkCredential(AccountName_var, Password_var)
        ftpRequest.Method = System.Net.WebRequestMethods.Ftp.ListDirectory
        Try
            ftpRequest.GetResponse()
            Return True
        Catch ex As System.Exception
            If ex.Message.Contains("530") Then
                MsgBox("Your account is invalid. Please try again or contact your Administrator.")
                Dim sf As New SettingsForm
                sf.ShowDialog()
            Else
                If ex.Message.Contains("Unable to connect to the remote server") Then
                    MsgBox("Could not connect to the server. Please try again or contact your Administrator.")
                Else
                    MsgBox(ex.Message)
                End If
            End If
            Return False
        End Try
    End Functionb




Tuesday, 23 April 2013

Setting Intervals Using Timer

                                                Setting Interval Through The Timer

Mostly timer is use for set the time for appropriate action in our application.I am make a 
small application that set the interval for to do something.
1)Open the VS any version, create a new project,or and new form in existing project in
    your computer.
2) Add new window form -
     Right click on project--> Click Add-->New Item-->Open new dialog box-->
     select Window form
3)After add the form  in project 
4)Design the form like that,see only red in circle-
5)Put the code inside the Check Box and Timer.
6)Here I call the function in timer,which is execute within the interval.
      Public Sub ShivFunction()
          MsgBox("file is downloaded")
      End Sub 



Office Ribbon Design Elements

Office Ribbon Design Element With VS 2010-

Here I describe about some feature and properties of ribbon design in office,in project it
can hold project on excel,word,power point,outlook,choose any one of the as you want.
Ribbon is use for add new tab in office,contain different type tools. In this blog i explore
feature with the outlook.So when we open the VS 2010 ,just follow the instruction -

   1)If you don't know how to open the outlook project then follow this  link.
   2)Here i give the  project name as 'SampleOutlookAddIn' .

   3)Right click on the project-->select Add-->New Item-->Open new dialog box.
   4)You have two type ribbon,Ribbon(visual designer) and Ribbon(XML),you can choose
      any one, just like i choose Ribbon(visual designer) here,give the name and click Add.

   5)Go to ribbon property--> ribbon type--> select 'Microsoft.Outlook.Explorer'.

   6)Drag-drop the tools in ribbon as shown in image.
      In image i show the ribbon contain Group,and group contain Box, box hold button and
      checkbox.You can change the name of 'AddIn' from property.
7)If You want to add custom type tab then go to property of tab and change it to 'custom'.


Now,here introduce some tools,used in ribbon
    a)Ribbon- Ribbon is hold the all tools like buttons,groups,checkbox,textbox etc.Used
       for built simple tab and custom tab.
    b)Tab-In ribbon we can add one or more  tab(simple tab and custom tab).
    c)Group- After the ribbon 'group' is an important element for collection of tools,and group is 
       contained by ribbon.
    d)Button-Button is like a simple button as in other application.
    e)Separator-This is use for separate and provide a some space ,give a line between two or
       more tools.
    f)Toggle Button-This is use for one time click,like a work as on/off, active/inactive
      able/enable act.
   g)Split Button- Button is Spite in two part one is for selection/clicking and other is see the list 
      own feature it may be collect a  buttons,checkbox etc.
   h)Button Group- This contain the buttons or it maintain the group of button/checkbox,but here 
      only one button/checkbox work at a time.
   j)Combo/Dropdown/Textbox, Checkbox/Level-Works same as visual basic tools. 

8)After run this project open outlook,  it look like-