Showing posts with label vb6. Show all posts
Showing posts with label vb6. Show all posts

Thursday, July 8, 2010

Startup with Windows (use wshom)

'two options - both require reference to Windows Script Host Object Mode (wshom.ocx)

'add a registry entry in HKLM\Software\Microsoft\Windows\CurrentVersion\Run
Dim oShell As IWshShell_Class
Set oShell = New IWshShell_Class
oShell.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion\Run\MyVBApp" , _
"c:\app\appname.exe" , "REG_SZ"


'put shortcut in startup menu
'include reference in VB program to Windows Script Host Object Mode (wshom.ocx)
Dim oShell As IWshShell_Class
Dim oSCut As IWshShortcut_Class
Dim sPath As String

Set oShell = New IWshShell_Class
sPath = oShell.SpecialFolders( "Startup" )
Set oSCut = oShell.CreateShortcut(sPath & "\TestStartup.lnk" )
oSCut.TargetPath = cPGM
oSCut.Save

List files and folders - FSO

Option Explicit
Dim X As New FileSystemObject

Dim F As Folder
Set F = X.GetFolder( "d:\data" )
GetFolderList F


Sub GetFolderList(aFolder As Folder)
'Put code here if you want to stop the search If SearchStop = False Then Exit Sub
For Each tempFile In aFolder.Files
Print tempFile.Name
Next

Dim tempFolder As Folder
For Each tempFolder In aFolder.SubFolders
Print tempF.ParentFolder & "\"; tempF.Name
GetFolderList tempFolder
Next
End Sub

Play CD (MM control)

MMControl1.DeviceType = "CDAudio"
MMControl1.Command = "Open"

Upload file

'upload a file using FTP
Inet1.URL = " ftp://ftp.domain.com "
Inet1.UserName = "UserName"
Inet1.Password = "password"
Inet1.Execute , "Put C:\putme.txt /files/putme.txt"
Inet1.Execute , "Close"

Open a web page in default browser

'Put this In the Declare section of the project

Public Declare Function ShellExecute Lib "SHELL32.DLL" Alias "ShellExecuteA"
( ByVal hwnd As Long , ByVal lpOperation As String , ByVal lpFile As String ,
ByVal lpParameters As String , ByVal lpDirectory As String , ByVal nShowCmd As Long ) As Long
Public Const SW_ShowNormal = 1

'put this where you can execute it, such as in a button click event

Dim iReturn As Long
iReturn = ShellExecute(frmMain.hwnd, "Open" , " http://www.garybeene.com " ,
vbNullString, vbNullString, SW_ShowNormal)

Get remote file information - size, date

'this uses the Internet Transfer Control
Dim temp As String
Inet.OpenURL " http://www.garybeene.com/index.htm "

'file length
temp = Inet.GetHeader( "Content-length" )
'file date
temp = Inet.GetHeader( "Last-modified" )

Create URL shortcut

'create a text file With two lines (use your own URL)
Open "c:\myurl.url" For Output As #1
Print #1, "[InternetShortcut]"
Print #1, "URL= http://www.microsoft.com /"
Close #1

Check for Internet connection

Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" Alias _
"InternetGetConnectedStateExA" (lpdwFlags As Long , lpszConnectionName As Long , _
dwNameLen As Long , ByVal dwReserved As Long ) As Long

Function IsConnected() As Boolean
Dim lNameLen As Long
Dim lRetVal As Long
Dim lConnectionFlags As Long
Dim LPTR As Long
Dim lNameLenPtr As Long
Dim sConnectionName As String
sConnectionName = Space$(256)
lNameLen = 256
LPTR = StrPtr(sConnectionName)
lNameLenPtr = VarPtr(lNameLen)
lRetVal = InternetGetConnectedStateEx(lConnectionFlags, ByVal LPTR, ByVal lNameLen, 0&)
IsConnected = (lRetVal <> 0)
End Function

Capture any key

'in the KeyPress event of any control

If KeyAscii = vbKeyReturn Then
MSGBox "Enter was pressed!"
End If

'this is the same as using character code constants
If KeyAscii = 13 Then
MSGBox "Enter was pressed"
End If

'KeyCode represents the physical key that is pressed/released (all keys except TAB)

'in the KeyUp events

If KeyCode = vbKeyReturn Then
MSGBox "Enter key was released"
End If

'in the KeyDown events
If KeyCode = vbKeyReturn Then
MSGBox "Enter key was pressed"
End If

'other useful KeyCode constants include:
'vbKeyLButton vbKeyRButton
'vbKeyReturn
'vbKeyShift
'vbKeyControl
'vbKeyEscape
'vbKeySpace
'vbKeyUp vbKeyDown vbKeyLeft vbKeyRight
'vbKeyPageUp vbKeyPageDown vbKeyEnd vbKeyHome
'vbKeyF1 - vbKey16

Create desktop shortcut

'this version goes to a folder of your choice
Dim wShell As New IWshShell_Class
Dim wShortcut As IWshShortcut_Class
Set wShortcut = wShell.CreateShortcut( "d:\test.lnk" )
wShortcut.TargetPath = "d:\test.txt"
wShortcut.Save

'and this version goes to the desktop
Dim wShell As New IWshShell_Class
Dim wShortcut As IWshShortcut_Class

Dim sDesktop As String ' path of desktop
Dim sTargetPath As String ' path to file for which a link is to be created

sDesktop = wShell.SpecialFolders.Item( "Desktop" )
sTargetPath = wShell.ExpandEnvironmentStrings( "%windir%" )

Set wShortcut = wShell.CreateShortcut(sDesktop & "filename.lnk" )

wShortcut.TargetPath = sTargetPath & "\filename.exe"
wShortcut.Arguments = ""
wShortCut.WorkingDirectory = sDesktop
wShortCut.Save

Get list of available fonts

Private Sub CreateFontList()
' populate a list with system fonts
Dim Cnt As Long
Dim s1 As String
For Cnt = 1 To Screen.FontCount
s1 = Screen.Fonts(Cnt)

If s1 <> "" Then List1.AddItem s1
Next
List1.ListIndex = 0
End Sub