Creating Digital Signature for VBA Project (Outlook VBA / Excel VBA/ Word VBA)

April 27, 2008 by vbadud

Creating Digital Signature for VBA Project (Outlook VBA / Excel VBA/ Word VBA)


How to digitally sign a VBA Project

To create a code-signing certificate, follow these steps:

From Programs menu, choose Microsoft Office -> Microsoft Office Tools -> Digital Certificate for VBA Projects

(or)

Navigate to the folder where your Office applications are installed, usually C:\Program
Files\Microsoft Office\Office12 for Office 2007 and run the Selfcert.exe program.

<>

SelfCert.exe is provided with Office 2000 and later. If it is not installed on your system, run Office Setup and install Office Tools  Digital Signature for VBA Projects.

Type a name for your certificate – just use your own name or product name and click OK to create your personal code-signing certificate.

<>

Now open the VBA project using Alt+F11 and select Tools ➪ Digital Signature
to open the dialog shown below. In this dialog, click the Choose button and select the
certificate and click OK twice to choose your certificate and
sign your code project with that certificate.

Save your VBA project; then exit and restart Application (Excel / Word / Outlook).
When you open your VBA project next time, you will be prompted to enable your macros in
the dialog shown below

You can use this dialog to trust your certificate by selecting the option to “Always trust macros from this publisher”, which prevents this dialog from appearing again, or you can just enable the macros for that session.

Click the Trust All Documents from This Publisher button to trust your code-signing certificate
and add it to the trusted publishers list. If you open the Trust Center dialog shown
again and click the Trusted Publishers area, you will now see your code-signing certificate listed
as a trusted publisher.

The same certificate can be used for multiple projects.

How to use .Net Array.Sort Function in VBA

April 27, 2008 by vbadud

How to use .Net Array.Sort Function in VBA

Using Vb.Net Function in VBA





How to use a VB.Net DLL/TLB in Excel VBA

Here is an example for using the customized .NET Function in Excel VBA. Unfortunately EXcel VBA doesn’t have a Array.Sort function. To overcome the shortcomings, we create our own function here in .Net and use the same in Excel VBA

Here are the steps:

  1. Create a class library project in Visual Studio
  2. Add a COM Class item (DND_SortArray in this example)
  3. In the assembly information edit the title, company and provide a meaningful description. This would be seen in the References dialog in Excel
  4. Add the code shown below:

_

  1. Public Class DotNetDud_SortArray

    #Region “COM GUIDs”

    ‘ These GUIDs provide the COM identity for this class

    ‘ and its COM interfaces. If you change them, existing

    ‘ clients will no longer be able to access the class.

    Public Const ClassId As String = “93534c94-9fc1-4a54-b022-338fa7d454c1″

    Public Const InterfaceId As String = “03787ed3-bc65-41a1-9053-d37f390ff94b”

    Public Const EventsId As String = “34d12c14-8afd-44b7-a987-fc2f909724b6″

    #End Region

    ‘ A creatable COM class must have a Public Sub New()

    ‘ with no parameters, otherwise, the class will not be

    ‘ registered in the COM registry and cannot be created

    ‘ via CreateObject.

    Public Sub New()

    MyBase.New()

    End Sub

    Public Sub SortArray(ByRef arTemp() As String)

    Array.Sort(arTemp)

    End Sub

    End Class

Compile the Project. You will get a DLL and a TLB.

Now open the Excel VBA Editor and add the TLB file to References.

The following code will now use the SortArray .NET Function created

Sub Use_DotNet_Sort()

Dim Cls1 As DotNetDud_SortArray.DotNetDud_SortArray

Set Cls1 = New DotNetDud_SortArray.DotNetDud_SortArray

Dim arTemp(0 To 2) As String

arTemp(0) = “Bottle”

arTemp(1) = “Apple”

arTemp(2) = “Aaron”

Cls1.SortArray arTemp

Set Cls1 = Nothing

End Sub

Thursday, April 17, 2008

Validate Date between Ranges using VBA

December 4, 2007 by vbadud

Validate Dates

Validate Date between Ranges

Sub Feed_Check_Date_Function()

Date is within the Valid Range
Check_Date “2007-05-01″, “2007-05-10″, “2007-05-01″

‘ Date is NOT within the Valid Range
Check_Date “2007-05-01″, “2007-05-10″, “2007-05-21″

End Sub

Function Check_Date(ByVal StartDate As Date, ByVal EndDate As Date, ByVal DateTobeChecked As Date)

If DateDiff(“d”, StartDate, DateTobeChecked) <> 0 Then
MsgBox “Enter a correct date!!!”
End If

End Function

Check Workbook Attributes

December 4, 2007 by vbadud

Check Workbook Attributes

Get File Attributes – GetAttr

Sub Check_Workbook_Attributes()

Dim oXL As Excel.Application
Dim oWB As Workbook

Set oXL = Excel.Application

oXL.DisplayAlerts = False

‘ Check if the Workbook is Read-Only. If it is then close the workbbok

Set oWB = oXL.Workbooks.Open(Filename:=”c:\MyBook.xls”, ReadOnly:=False)

If oWB.ReadOnly = True Then
MsgBox “The Workbook is Read-Only!!”, vbInformation
oWB.Close False
End If

oXL.DisplayAlerts = True

‘ Using the GetAttr Function, WE can check if the file is read-only
If (GetAttr(“c:\MyBook.xls”) And vbReadOnly) Then
MsgBox “The Workbook is Read-Only!!”, vbInformation
End If

End Sub

The GetAttr function will work if the file has read-only attributes. If the file is locked and hence it is available as read only this will not be useful

Resizing Text Boxes Automatically

December 4, 2007 by vbadud

Automaticaly Resize Text Boxes

Resizing Text Boxes

Sub Initialize_TextBox()

‘ To automatically resize the text box set the AutoSize to True. This will resize the text box as the user types text

TextBox1.AutoSize = True

‘ The sizing can be limited by providing the maximum length using MaxLength property

TextBox1.MaxLength = 20

‘ You can inform the user of resing with the Tooltip.

TextBox1.ControlTipText = “Maximum Length is 20″

End Sub

ControlTipText is the VBA variant for Visual Basic ToolTipText

Drag & DRop Files to Text Box

December 4, 2007 by vbadud

Drag & DRop Files to Text Box

Show File Name in Text Box using Drag & Drop

Private Sub TextBox1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim i1 As Integer ‘* Files Counter

On Error GoTo Err_Trap

If Data.GetFormat(vbCFFiles) = True Then
i1 = Data.Files.Count
If i1 = 1 Then
If InStr(1, LCase$(Data.Files(i1)), “.xls”) Then
txtExcel.Text = Data.Files(i1)
End If
End If
End If

‘ ——————————————
‘ Error Handling
‘ ——————————————
Err_Trap:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
End If
End Sub

This will be used to show the file name in the Text Box if a file is dragged and dropped into it

Retrieve the file properties – VBA

December 4, 2007 by vbadud

Retrieve the file properties – VBA

Sub Get_WorkBook_Properties()

Dim oWB As Workbook

‘ Here is the program to retrieve the file properties.

Set oWB = ActiveWorkbook

‘——————————————————–
‘ Coded by Shasur for http://vbadud.blogspot.com
‘——————————————————–

‘Get the Title property
sTitle = oWB.BuiltinDocumentProperties(“Title”).Value

‘Get the Subject property
sSubject = oWB.BuiltinDocumentProperties(“Subject”).Value

‘Get the Author property
sAuthor = oWB.BuiltinDocumentProperties(“Author”).Value

‘Get the Keywords property
sKeywords = oWB.BuiltinDocumentProperties(“Keywords”).Value

‘Get the Comments property
sComments = oWB.BuiltinDocumentProperties(“Comments”).Value

‘Get the Template property
sTemplate = oWB.BuiltinDocumentProperties(“Template”).Value

‘Get the Last author property
sLastauthor = oWB.BuiltinDocumentProperties(“Last author”).Value

‘Get the Revision number property
sRevisionnumber = oWB.BuiltinDocumentProperties(“Revision number”).Value

‘Get the Application name property
sApplicationName = oWB.BuiltinDocumentProperties(“Application name”).Value

‘Get the Last print date property
sLastprintdate = oWB.BuiltinDocumentProperties(“Last print date”).Value

‘Get the Creation date property
sCreationdate = oWB.BuiltinDocumentProperties(“Creation date”).Value

‘Get the Last save time property
sLastsavetime = oWB.BuiltinDocumentProperties(“Last save time”).Value

‘Get the Total editing time property
sTotaleditingtime = oWB.BuiltinDocumentProperties(“Total editing time”).Value

‘Get the Number of pages property
sNumberofpages = oWB.BuiltinDocumentProperties(“Number of pages”).Value

‘Get the Number of words property
sNumberofwords = oWB.BuiltinDocumentProperties(“Number of words”).Value

‘Get the Number of characters property
sNumberofcharacters = oWB.BuiltinDocumentProperties(“Number of characters”).Value

‘Get the Security property
sSecurity = oWB.BuiltinDocumentProperties(“Security”).Value

‘Get the Category property
sCategory = oWB.BuiltinDocumentProperties(“Category”).Value

‘Get the Format property
sFormat = oWB.BuiltinDocumentProperties(“Format”).Value

‘Get the Manager property
sManager = oWB.BuiltinDocumentProperties(“Manager”).Value

‘Get the Company property
sCompany = oWB.BuiltinDocumentProperties(“Company”).Value

‘Get the Number of bytes property
sNumberofbytes = oWB.BuiltinDocumentProperties(“Number of bytes”).Value

‘Get the Number of lines property
sNumberoflines = oWB.BuiltinDocumentProperties(“Number of lines”).Value

‘Get the Number of paragraphs property
sNumberofparagraphs = oWB.BuiltinDocumentProperties(“Number of paragraphs”).Value

‘Get the Number of slides property
sNumberofslides = oWB.BuiltinDocumentProperties(“Number of slides”).Value

‘Get the Number of notes property
sNumberofnotes = oWB.BuiltinDocumentProperties(“Number of notes”).Value

‘Get the Number of hidden Slides property
sNumberofhiddenSlides = oWB.BuiltinDocumentProperties(“Number of hidden Slides”).Value

‘Get the Number of multimedia clips property
sNumberofmultimediaclips = oWB.BuiltinDocumentProperties(“Number of multimedia clips”).Value

‘Get the Hyperlink base property
sHyperlinkbase = oWB.BuiltinDocumentProperties(“Hyperlink base”).Value

‘Get the Number of characters (with spaces) property
sNumberofcharacters = oWB.BuiltinDocumentProperties(“Number of characters (with spaces)”).Value

‘keywords: VBA Update File Properties, Macro to Update File Properties

End Sub

Visual Basic Get Screen Area

December 4, 2007 by vbadud

Visual Basic Get Screen Area

Windows API Get Screen Area

Declare Function GetSystemMetrics Lib “user32″ _

(ByVal nIndex As Long) As Long
Const SM_CXSCREEN As Long = 0
Const SM_CYSCREEN As Long = 1

Here is the VBA function using GetSystemMetrics to get the screen area

Sub Get_Screen_Metrics()

‘ Windows API Function to Get Screen Area

lx = GetSystemMetrics(SM_CXSCREEN)
ly = GetSystemMetrics(SM_CYSCREEN)

‘——————————————————–
‘ Coded by Shasur for http://vbadud.blogspot.com
‘——————————————————–

MsgBox “The Screen Area is ” & lx & ” x ” & ly & ” pixels”

Visual Basic Get Screen Area, Visual Basic Get Screen Height, Visual Basic Get Screen Width ,Windows API Get Screen Area, Windows API Get Screen Height, Windows API Get Screen Width
End Sub

Add Combo Box to the command Bar

December 4, 2007 by vbadud

Add Combo Box to the command Bar

Add Combo Box to the command Bar

Sub Show_Combo_CommandBar()

Dim oCB As CommandBar
Dim oCtl As CommandBarComboBox

On Error Resume Next

‘Delete Control From CommandBar
CommandBars(“Sample Command Bar”).Delete

Set oCB = CommandBars.Add
oCB.Name = “Sample Command Bar”
oCB.AdaptiveMenu = True

‘Add Control to CommandBar
Set oCtl = oCB.Controls.Add(Type:=msoControlComboBox)
oCtl.Caption = “ComboSamp”

‘Link Macro to CommandBar,
oCtl.OnAction = “Change_Header_Background”

‘Add list Item to Combo Box Control
oCtl.AddItem “NoColor”
oCtl.AddItem “Blue”
oCtl.AddItem “Yellow”

Show the Command Bar
oCB.Visible = True

‘ Place the CommandBar at the bottom of the screen
oCB.Position = msoBarBottom

End Sub

Sub Change_Header_Background()

‘ Acts based on the value in the Combo Box

Dim oCB As CommandBar
Dim oCtl As CommandBarComboBox

On Error Resume Next

Set oCB = CommandBars(“Sample Command Bar”)

Set oCtl = oCB.Controls(“ComboSamp”)

If oCtl.ListIndex <> -1 Then

Select Case oCtl.ListIndex

Case 1

ActiveSheet.Rows(1).Interior.ColorIndex = 0

Case 2

ActiveSheet.Rows(1).Interior.ColorIndex = 5

Case 3

ActiveSheet.Rows(1).Interior.ColorIndex = 36

Case Else

‘ Do nothing

End Select

End If

‘ Show the Command Bar
oCB.Visible = True

‘ Place the CommandBar at the bottom of the screen
oCB.Position = msoBarBottom

End Sub

Display ToolTipText in CommandBar Controls

December 4, 2007 by vbadud

Add ToolTipText in CommandBar Controls

Display ToolTipText in CommandBar Controls

Sub Show_ToolTipText_In_Controls()

Dim oCB As CommandBar
Dim oCtl As CommandBarControl

On Error Resume Next

‘ Delete Existing Command Bar
CommandBars(“MyProject”).Delete

‘Create New Command Bar
Set oCB = CommandBars.Add
oCB.Name = “MyProject”
oCB.AdaptiveMenu = True

Set oCtl = oCB.Controls.Add(Type:=msoControlButton)
oCtl.Caption = “Show Message Box”
oCtl.TooltipText = “This is a sample”
oCtl.OnAction = “Display_Msg_Box”
oCtl.SetFocus

‘ Show the Command Bar
oCB.Visible = True

‘ Place the CommandBar at the bottom of the screen
oCB.Position = msoBarBottom

End Sub

Sub Display_Msg_Box()

MsgBox “You have clicked me!!!”

End Sub