Monday, January 22, 2007

Dynamically Create a tool bar in Excel VBA


In Excel the tool bar button is used to call the xla file, for that i written a code to create the tool bar dynamicallyno need to add the xla file manually in to the workbook.Place the code in the one seperate module, the code is written in the "Auto_Open" event of excel workbook,the below code will create a tool bor with two buttons.Take a look at that and try it once,

'--------------------------------------------------------------------
Public Sub Auto_Open()


Dim cm As Office.CommandBar
On Error Resume Next
'Delete the toolbar if it already exists
Application.CommandBars("NewToolBar").delete
On Error GoTo 0
'Create the new toolbar each time this workbook opens

Set cm = Application.CommandBars.Add("NewToolBar", , , Temporary:=True)

With cm.Controls

'Add a button to the tool bar
With .Add(msoControlButton)
.Caption = "New Button"
'While click the button in the tool bar
'the method name entered here will called
.OnAction = "EntertheMethodName"
.FaceId = 1763
.Style = msoButtonIcon
End With

'Can add more than one button in the same tool bar
With .Add(msoControlButton)
.Caption = "New Button1"
'While click the button in the tool bar
'the method name entered here will called
.OnAction = "EntertheMethodName1"
.FaceId = 2882
.Style = msoButtonIcon
End With

End With

cm.Position = msoBarTop
cm.Visible = True

End Sub
'--------------------------------------------------------------------
Cheers........

Creating a simple XML file in VB.net

The below is code for creating the XML file in VB.net.
copy and paste the code in on module and see how its work...
----------------------------------------------------------------
Private sub CreateXMLFile (strRootDirectoryName as string)

Dim mywriter As System.Xml.XmlTextWriter
mywriter = New System.Xml.XmlTextWriter(strRootDirectoryName & "AddressType.xml", Nothing)
mywriter.WriteStartDocument()
mywriter.WriteStartElement("AddressType")
mywriter.WriteElementString("Company", strCompany)
mywriter.WriteElementString("Address1", strAddress1)
mywriter.WriteElementString("Address2", strAddress1)
mywriter.WriteElementString("City", strCity)
mywriter.WriteElementString("State", strState)
mywriter.WriteElementString("Country", strCountry)
mywriter.WriteElementString("Zip", strZip)
mywriter.WriteElementString("Fax", strFax)
mywriter.WriteElementString("Phone", strPhone)
mywriter.WriteElementString("Email", strEmail)
mywriter.WriteEndDocument()
mywriter.Flush()
mywriter.Close()

End Sub
----------------------------------------------------------------
Hope this is more helpfull for XML beginners.

Wednesday, January 10, 2007

Workbook Opened or not (VBA code)

The below is the VBA code to check whether the workbook is already opened or not.

If the file is already opened means we dont want ot open the file no need to open it again,

just activate the file and proceed.

if the file is open the function returns true other wise false.

copy and pase the below code in one module.

Public Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.

' Check to see which error occurred.
Select Case errnum

' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False

' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True

' Another error occurred.
Case Else
Error errnum
End Select

End Function