Tuesday, January 13, 2015

VB.NET - Export Excel to PDF

Imports System
Imports System.IO
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel


    ''' <summary>
    ''' converts excel spreadsheet to PDF
    ''' </summary>
    ''' <param name="strXLFile">file path of excel spreadsheet</param>
    ''' <param name="strPDF">file path of where to save the pdf</param>
    ''' <remarks></remarks>
    Public Shared Sub ConvertToPDF(ByVal strXLFile As String, ByVal strPDF As String)

        Dim objExcel As Excel.Application
        Dim objWB As Excel.Workbook

        Try

            'Open Excel and the workbook
            objExcel = New Application
            objExcel.Visible = False
            objWB = objExcel.Workbooks.Open(strXLFile)
            objWB.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, strPDF, , , , , , False)

        Catch ex As Exception
            Throw ex
        Finally
            If objWB IsNot Nothing Then
                objWB.Close()
            End If

            NAR(objWB)

            If objExcel IsNot Nothing Then
                objExcel.Quit() ' Need to leave application open for display.
            End If

            NAR(objExcel)
            GC.Collect()
            GC.WaitForPendingFinalizers()
        End Try

    End Sub


    ''' <summary>
    ''' Used to properly clean up excel or powepoint COM objects.
    ''' </summary>
    ''' <param name="o"></param>
    ''' <remarks></remarks>
    Shared Sub NAR(ByVal o As Object)
        Try
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(o)
        Catch

        Finally
            o = Nothing
        End Try
    End Sub

Friday, January 9, 2015

VB.NET - Regular Expression to get all HREF's out of a String

This function will accept a string and return an arraylist of all the HREF's that were inside:

    ''' <summary>
    ''' Using regular expression to get HREF out of string
    '''
    ''' </summary>
    ''' <param name="inputString"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetHtmlTags(inputString As String) As ArrayList
        Dim m As Match
        Dim HRefPattern As String = "<img[^>]+src\\s*=\\s*['\""]([^'\""]+)['\""][^>]*>"
        Dim arrlist As New ArrayList
        Try
            'Using regular expression retrieve all HREFs from the datacontent string
            m = Regex.Match(inputString, HRefPattern, RegexOptions.IgnoreCase Or RegexOptions.Compiled)
            Do While m.Success
                'check to see if the link already exists in the arraylist before adding it again
                If arrlist.Contains(m.Groups(1).ToString()) = False Then
                    arrlist.Add(m.Groups(1).ToString())
                End If

                m = m.NextMatch()
            Loop

            Return arrlist

        Catch ex As Exception
            Throw ex
        End Try
    End Function



VB.NET - Write Text File Using StreamWriter

Imports System.IO

 Dim strDirectory As String = "C:\temp324\"

        If Directory.Exists(strDirectory) = False Then
            Directory.CreateDirectory(strDirectory)
        End If


        Using objWriter As StreamWriter = New StreamWriter(strDirectory & "\links.txt", False)
            objWriter.Write(txtText.Text)
        End Using