Saturday, 14 December 2013

The MessageBox Function

You can return a value from the Messagebox function and assign it to a variable.
tReturn=Msgbox( "Close the Application",vbRetryCancel,"Application Error!")
This return value for an application error returns 4 for retry and 2 for cancel. vbYesNo returns 4 as a return value.

Monday, 19 March 2012

Working with the Word Object Library

The word object library is present in the references window and should be included to implement word document applications. The code below describes 3 examples. The first example creates a word document and inserts letters, paragraphs ans sets font sizes to them. The second example inserts text and checks spelling of the inserted text. The third example creates a word document in a given file location and inserts text in it.
Public obWrd As Word.Application
Private Sub Form_Load()
Dim obDc As Word.Document
Set obWrd = New Word.Application
obWrd.Visible = True
Set obDc = objWord.Documents.Add
objDc.Activate
obDc.ActiveWindow.Selection.InsertAfter "The Real Text"
obDc.ActiveWindow.Selection.InsertParagraphAfter
obDc.ActiveWindow.Selection.InsertAfter "The Big Text"
obDc.ActiveWindow.Selection.InsertParagraphAfter
obDc.ActiveWindow.Selection.Font.Bold = True
obDc.ActiveWindow.Selection.EndOf
obWrd.Quit False
Set obWrd = Nothing
End Sub

Public objword1 As Word.Application
Private Sub Form_Load()
Dim objDoc1 As Word.Document
Set objWord = New Word.Application
objword1.Visible = True
Set objDoc = objWord.Documents.Add
objDoc1.Activate
objDoc1.ActiveWindow.Selection.InsertAfter ("The Text")
On Error Resume Next
objDoc1.CheckSpelling
If Err Then
MsgBox Err.Description & Err.Number
End If
End Sub
Private Sub Form_Load()
Dim objWord As Object
Set objWord = CreateObject("Word.Basic")
objWord.filenew
objWord.startofdocument
objWord.FontSize 24
objWord.Insert "Big Letter Text"
objWord.FontSize 12
objWord.insertpara
objWord.Insert "Text with Smaller Font"
objWord.filesaveas "D:\test.doc"
'objWord.Close
Set objWord = Nothing
End Sub

Sunday, 18 March 2012

The SysInfo Control

The following example uses a sysInfo control on a form and displays the properties of the system in a listbox.
Private Sub Form_Load()
On Error Resume Next
MsgBox "Sysinfo1.Index: " & SysInfo1.Index
If Err Then
MsgBox "" & Err.Description & Err.Number
End If
List1.AddItem "SysInfo1.OSBuild: " & SysInfo1.OSBuild
List1.AddItem "SysInfo1.BatteryFullTime: " & SysInfo1.BatteryFullTime
List1.AddItem "SysInfo1.ACStatus: " & SysInfo1.ACStatus
List1.AddItem "SysInfo1.BatteryLifePercent: " & SysInfo1.BatteryLifePercent
List1.AddItem "SysInfo1.BatteryLifeTime: " & SysInfo1.BatteryLifeTime
List1.AddItem "SysInfo1.BatteryStatus: " & SysInfo1.BatteryStatus
List1.AddItem "SysInfo1.Name: " & SysInfo1.Name
List1.AddItem "SysInfo1.Object: " & SysInfo1.Object
List1.AddItem "SysInfo1.OSPlatform: " & SysInfo1.OSPlatform
List1.AddItem "Sysinfo1.OSVersion: " & SysInfo1.OSVersion
List1.AddItem "Sysinfo1.Parent: " & SysInfo1.Parent
List1.AddItem "Sysinfo1.scrollbarsize: " & SysInfo1.ScrollBarSize
End Sub

The Beep and Arc Functions

The Beep produces a low frequency beep or system sound for a given number of milliseconds. An arc function prints an arc on the form.

Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
Private Sub Form_Activate()
        Dim Cnt As Long
    For Cnt = 0 To 5000 Step 10
        'play a tone of 'Cnt' hertz, for 50 milliseconds
        Beep Cnt, 50
        Me.Caption = Cnt
        DoEvents
    Next Cnt
End Sub

Private Declare Function Arc Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long, ByVal X4 As Long, ByVal Y4 As Long) As Long
Private Sub Form_Load()
    'Set graphical mode to persistent
    Me.AutoRedraw = True
    'Draw to arcs
    Arc Me.hdc, 0, 0, 100, 100, 100, 50, 50, 100
    Arc Me.hdc, 49, 49, 149, 149, 49, 99, 99, 49
End Sub

The FlashWindow, GetsystemDirectory and GetSystemInfo Api Call

The FlashWindow function flashes a window to a user. The GetSystemDirecoty prints the present system directory and the GetSystemInfo function presents the system information to the user.
Private Declare Function FlashWindow Lib "user32" (ByVal hWnd As Long, ByVal bInvert As Long) As Long
Private Sub Timer1_Timer()
Dim nReturnValue As Long
nReturnValue = FlashWindow(Form1.hWnd, True)
End Sub

Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Sub Form_Load()
    Dim sSave As String, Ret As Long
    'Create a buffer
    sSave = Space(255)
    'Get the system directory
    Ret = GetSystemDirectory(sSave, 255)
    'Remove all unnecessary chr$(0)'s
    sSave = Left$(sSave, Ret)
    'Show the windows directory
    MsgBox "Windows System directory: " + sSave
End Sub

Private Declare Sub GetSystemInfo Lib "kernel32" (lpSystemInfo As SYSTEM_INFO)
Private Type SYSTEM_INFO
    dwOemID As Long
    dwPageSize As Long
    lpMinimumApplicationAddress As Long
    lpMaximumApplicationAddress As Long
    dwActiveProcessorMask As Long
    dwNumberOrfProcessors As Long
    dwProcessorType As Long
    dwAllocationGranularity As Long
    dwReserved As Long
End Type
Private Sub Form_Load()
    Dim SInfo As SYSTEM_INFO
    'Set the graphical mode to persistent
    Me.AutoRedraw = True
    'Get the system information
    GetSystemInfo SInfo
    'Print it to the form
    Me.Print "Number of procesor:" + Str$(SInfo.dwNumberOrfProcessors)
    Me.Print "Processor:" + Str$(SInfo.dwProcessorType)
    Me.Print "Low memory address:" + Str$(SInfo.lpMinimumApplicationAddress)
    Me.Print "High memory address:" + Str$(SInfo.lpMaximumApplicationAddress)
End Sub


 

Thursday, 15 March 2012

Creating a Window Using the Windows API

The Windows API is used to create windows for forms in Visual Basic. These forms have properties such as title, custom text and other features such as forecolor and backcolor. Users can create and destroy windows on forms. API functions are declared before being used or implemented. You must declare functions as private to use on forms.
Const WS_EX_STATICEDGE = &H20000
Const WS_EX_TRANSPARENT = &H20&
Const WS_CHILD = &H40000000
Const CW_USEDEFAULT = &H80000000
Const SW_NORMAL = 1
Private Type CREATESTRUCT
    lpCreateParams As Long
    hInstance As Long
    hMenu As Long
    hWndParent As Long
    cy As Long
    cx As Long
    y As Long
    x As Long
    style As Long
    lpszName As String
    lpszClass As String
    ExStyle As Long
End Type
Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Dim mWnd As Long
Private Sub Form_Load()
    Dim CS As CREATESTRUCT
    'Create a new label
    mWnd = CreateWindowEx(WS_EX_STATICEDGE Or WS_EX_TRANSPARENT, "STATIC", "Hello World !", WS_CHILD, 0, 0, 300, 50, Me.hwnd, 0, App.hInstance, CS)
    Me.Caption = mWnd
    'Show our label
    ShowWindow mWnd, SW_NORMAL
End Sub
Private Sub Form_Unload(Cancel As Integer)
    'destroy our label
    DestroyWindow mWnd
End Sub

Wednesday, 7 March 2012

Working with String Objects

The string object has several functions that are able to reverse a string-strreverse(), compare a string strcomp(), extract characters from a string-Left(), mid(), right(), UCase(), Lcase() and InStr().
The mid(string, start of string to extract, number of characters) function is used to extract a string inside a given string.
The Left(string, start of string, number of characters) is used to extract charcters from the left portion of a string. The Mid$() functions return a string value compared the Mid() functions returning a variant.
Private Sub Form_Load()
Dim str As String
Dim i As Integer
str = "stringring"
str1 = "ring"
'MsgBox UCase(str)
'MsgBox LCase(str)
'i = InStr(str, str1)
i = InStrRev(str, str1)
'str = Mid(str, 2, 5)
'str = Left(str, 4)
'str = Right(str, 4)
'MsgBox " " & Mid$(str, 2, 5) & vbCrLf & Left(str, 4)
'MsgBox Left(str, 1)
'MsgBox Mid(str1, 3, 1)
End Sub