Wednesday, July 7, 2010

Object Oriented Programming & Objects

Object Oriented Programming:-
VBA is a structured programming language where sentences (called statements) are constructed of building blocks such as objects, methods, and properties. These VBA statements are grouped in larger blocks called procedures. A procedure is a set of VBA statements that performs a specific task or calculates a specific result. The first step to learning how to create procedures is to learn about the building blocks.

Objects:-
VBA is an object-oriented programming language, which means the statements you create in VBA act on specific objects rather than begin general commands. Excel is made of objects that you can manipulate through the VBA statements. The entire workbook file is an object, an individual sheet is an object, a range within a sheet can be an object, and an individual cell is an object. There are many more types of objects, and as you see objects can be containers for (called collections) other objects.

VBA Programming Basics

The VBA programming language provides the tools needed to create solutions when the macro recorder can’t get the job done. VBA is such a powerful programming language that you can (if you have the programming ability) create a solution to virtually any problem you encounter in Excel. In fact you can completely customize Excel through VBA and you can even change the entire Excel interface.

(i) Object Oriented Programming.
(ii) Objects.
(iii) Collections.
(iv) Methods.
(v) Properties.
(vi) Functions.
(vii) Procedures.
(viii) Variables and Constants.

Wednesday, June 9, 2010

Activate window by name in MS-Word

Sub GenerateGlossary()
Dim strSource As String
Dim strDestination As String
Dim strGlossaryName As String

strSource = ActiveWindow.Caption
strGlossaryName = "word"

Documents.Add
ActiveDocument.SaveAs FileName:=strGlossaryName, FileFormat:=wdFormatDocument
strDestination = ActiveWindow.Caption
Windows(strSource).Activate
End Sub

Active document paragraph in word.

Sub loopDemo()
Dim i As Integer
For i = 1 To ActiveDocument.Paragraphs.Count
Application.StatusBar = "formatting" & i & " out of " & ActiveDocument.Paragraphs.Count & "..."
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdMove
Next i
End Sub

Turning Off Track Changes

Sub trac()
Dim blnTrackChangesOn As Boolean
blnTrackChangesOn = ActiveDocument.TrackRevisions
ActiveDocument.TrackRevisions = False
ActiveDocument.TrackRevisions = blnTrackChangesOn
End Sub

To delete all the files in a given directory:-

'Loop through all the files in the directory by using Dir$ function
Dim MyFile As String
MyFile = Dir$("c:\temp\*.*")
Do While MyFile <> ""
KillProperly "c:\temp\" & MyFile
'need to specify full path again because a file was deleted 1
MyFile = Dir$("c:\temp\*.*")
Loop

Kill statement can't delete readonly files:-

Dim KillFile As String
KillFile = "c:\temp\temp.doc"
'Check that file exists
If Len(Dir$(KillFile)) > 0 Then
'First remove readonly attribute, if set
SetAttr KillFile, vbNormal
'Then delete the file
Kill KillFile
End If