کلینیک فوق تخصصی اکسس ( کاربرد vba در اکسس )

کلینیک فوق تخصصی اکسس ( کاربرد vba در اکسس )

به اشتراک گذاری اطلاعات کسب شده در اکسس از سایت آفیس و سایت های تخصصی خارجی
کلینیک فوق تخصصی اکسس ( کاربرد vba در اکسس )

کلینیک فوق تخصصی اکسس ( کاربرد vba در اکسس )

به اشتراک گذاری اطلاعات کسب شده در اکسس از سایت آفیس و سایت های تخصصی خارجی

متد Shell.NameSpace




Shell.Application


یک شئ Folder برای پوشه مشخص شده ایجاد و برمی گرداند


retVal=Shell.NameSpace(vdir)


vDir : 

پوشه ای که در آن شئ Folder ایجاد می شود.می تواند رشته ای باشد که مسیر پوشه یا یکی از مقادیر ShellSpecialFolderConstants را مشخص می کند.  توجه کنید که نام های ثابت موجود در ShellSpecialFolderConstants در Visual Basic در دسترس هستند ، اما در VBScript یا  JScript موجود نیستند. در این موارد ، مقادیر عددی باید در جای خود استفاده شوند.


VB : 

Object reference to the Folder object for the specified folder. If the folder is not successfully created, this value returns null.


شئ Folder :


Represents a Shell folder. This object contains properties and methods that allow you to retrieve information about the folder.

The Folder object has these types of members:

یکی از متدها ParseName :


یک شئ FolderItem را که نشان دهنده یک آیتم مشخص است را ایجاد و برمی گرداند.







Set objFolder=objShell.NameSpace("C:\Windows")

if Not (objFolder Is Nothing) Then

Set objFolderItem=objFolder.ParseName("clock.avi")

'اضافه کردن کد در اینجا

Set objFolderItem=Nothing

End If


شماری از ویژگیهای شئ FolderItem :

GetFolder

GetLink

IsFileSystem

IsFolder

IsLink

Name

Path

Size

Type








WNDPROC


متغیرهایی تعریف شده که زمان ارجاع به آن راحت باشید و مقادیری را به آن تخصیص بدهید با Private Type و مشخص کردن نام و دیتا تایپ آن.


Private Type CUSTOM_MSGBOX lTimeout As Long
lExitButton As Long
lInterval As Long
strPrompt As String
End Type


Public cm As CUSTOM_MSGBOX


برای آفیس 32 بیت است نه 64 برای 64 باید Longptr شود .

Private Function WinProc(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long 

Dim hwndCaption As Long 
Dim CurrentStyle As Long 
Dim ClassName As String 
Dim lResult As Long 
Dim Timeout As Long 
اگر پنجره ای فعال شد می بایست ClassName آنرا گرفته و چنانچه 32770 بود یعنی درست است و مطمئن میشوید که خود پنجره مسیج باکس است.

If lMsg = HCBT_ACTIVATE Then

ClassName = Space(256)

lResult = GetClassNameA(wParam, ClassName, 256)
If Left(ClassName, lResult) = "#32770" Then

' Make sure we spotted a messagebox (dialog)

hwndMsgBox = wParam 
Timeout = cm.lInterval 
'IIntrval=10000 Miliseconds

If Timeout = 0 Then
Timeout = cm.lTimeout 
If cm.lTimeout Then 
در اینجا تابع SetTimer عمل میکند و تابع TimeHandler اجرا می شود 

lTimerHandle = SetTimer(0&, 0&, Timeout, AddressOf TimerHandler)
از بین بردن hook که توسط SetWindowsHookEx نصب شده
'Remove Hook Procedure installed By a hook chaib  SetWindowsHookEx 
UnhookWindowsHookEx hHook 
End If
End If 
این خط مهم است وگرنه خطا ایجاد میکند.
WinProc = False 
End Function 



Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long 

Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long

Private Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long

Private Declare Function PostMessageA Lib "user32" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Declare Function SetDlgItemTextA Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As String) As Long 

Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Public Const IDPROMPT = &HFFFF&

Public Sub TimerHandler(hwnd As Long, uMSG As Integer, idEvent As Integer, dwTime As Double)

Dim hWndTargetBtn As Long
cm.lTimeout = cm.lTimeout - cm.lInterval  SetDlgItemTextA hwndMsgBox,IDPROMPT,
 Replace(cm.strPrompt, "%T",CStr(cm.lTimeout / 1000)) 

If cm.lTimeout <= 0 Then
hWndTargetBtn = GetDlgItem(hwndMsgBox, cm.lExitButton) 
' set the focus to the target button and ' simulate a click to close the dialog and ' return the correct value

فوکس را به باتن مقصد می برد و پیام ویندوزی LButtonDown و سپس LButtonUp را به پنجره باتن ارسال میکند و در نتیجه Close انجام میشود ( یک کلیک را تصویر گری می کند )


If hWndTargetBtn <> 0 Then 
SetFocus hWndTargetBtn
DoEvents
Call PostMessageA(hWndTargetBtn, WM_LBUTTONDOWN, 0, ByVal 0&)
Call PostMessageA(hWndTargetBtn, WM_LBUTTONUP, 0, ByVal 0&)
End If 
End If 
End Sub 


Private hHook As Long

Public hwndMsgBox As Long

Public lTimerHandle As Long

Public hAppInstance As Long




Public Function vbTimedMsgBox(Prompt As String,Optional Buttons As VbMsgBoxStyle = vbOKOnly,Optional Title As String, Optional Timeout As Long = 0,Optional Tick As Long = 1000,Optional DefaultExitButton As ExitButton = IDOK) As Long 

cm.lTimeout = TimeOut
cm.lExitButton = DefaultExitButton
hAppInstance =GetWindowLong(hWndAccessApp, GWL_HINSTANCE) 
' Access specific. In VB, this would be App.hInstance

hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc, hAppInstance, 0)

vbTimedMsgBox = MsgBox(Prompt, Buttons, Title) 

End Function



این یک نمونه کار است هر زمان که توابع API را مطالعه کردید می توانید با چیدمان درست کدها به مقاصد خود دست یابید البته هوک کردن مشکل است اگر خطایی اتفاق بیافتد سیستم هنگ خواهد کرد به WSCRIP.SHELL و POPUT هم می توان مراجعه کرد .


CREATEOBJECT("WSCRIPT.SHELL")

OBJECT.POPUP


Wscript Popup Method vbsedit

Echo Method vbsedit

Wscript.Shell + Shell.Application Objects shell.html


فرضا ساختن مرجع آبجکت یا شئ به یک فولدر با متد NAMESPACE از آبجکت SHELL.APPLICATION



filesystemobject-object


FolderItems.Count property :

Contains the number of items in the collection.


ssfWINDOWS = 36

Dim objShell,objFolder As Object

Set objShell =CreateObject("Shell.Application")

Set objFolder = objShell.NameSpace(ssfWINDOWS)

Set objFolderItems = objFolder.Items


nCount = objFolderItems.Count


Debug.Print nCount





ShellExecuteA


عملی را روی فایل خاصی انجام میدهد.


مثل Edit ، Open یا Explore ، Print  و جنس آن از نوع String است.


طبق روش زیر برای ویندوز ۶۴ بیت قبل از فانکشن PtrSafe قرار گرفته بغیر از hWnd که نمایانگر پنجره است و nShowCmd که نمایانگر نمایش پنجره در وضعیت هایی است و از نوع عددی ( Long ) هستند بقیه پارامترها از نوع String می باشند.


#If VBA7 Then
    Private Declare PtrSafe 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
#Else
    Private 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
#End If



lpFile specifies a document file, lpParameters should be NULL.


اگر lpFile سندی را مشخص کند lpParameters نمی تواند Null باشد.


The flags that specify how an application is to be displayed when it is opened. If lpFile specifies a document file,


nShow مشخص میکند چگونه یک اپلیکیشن زمانیکه باز میشود نمایش داده شود اگر lpFile یک فایلی مثل Word را مشخص نماید.


If the function succeeds, it returns a value greater than 32. If the function fails, it returns an error value that indicates the cause of the failure. 


اگر تابع درست انجام شود یک مقداری بزرگتر از ۳۲ را برمی گرداند می توانید با Msgbox در اکسس ببینید و اگر Fail دهد یا درست انجام نشود مقداری که علت انجام نشدنش باشد را نشان می دهد فرضا اگر دسترسی به فایل نداشته باشیم.


Example:
FileName = "C:\Documents and Settings\All Users\Start Menu\Programs\Microsoft Office\Microsoft Office Word 2003.lnk"

iRet = ShellExecute(GetDesktopWindow, "open", FileName, vbNullString, vbNullString, vbNormalFocus)
If lRet = 5 Then
    MsgBox "Access Denied" & vbCrLf & "Try pointing directely to the program EXE", , ""
End If



مثال بالا که از سایت گرفته شده نمایانگر این است که اگر دسترسی به فایلی امکانپذیر نبود بعلت محدودیت از طرف ادمین جعبه پیامی را باز کرده و به شما اعلام می نماید. ( GetDeskTopWindow یک هندل است که می بایست تابع آنرا در Module اضافه کنید در اینترنت تابع مورد نظر موجود است حتما از CloseHandle استفاده کنید.


ConstantValueDescription
vbHide0Window is hidden and focus is passed to the hidden window.
vbNormalFocus1Window has focus and is restored to its original size and position.
vbMinimizedFocus2Window is displayed as an icon with focus.
vbMaximizedFocus3Window is maximized with focus.
vbNormalNoFocus4Window is restored to its most recent size and position. The currently active window remains active.
vbMinimizedNoFocus6Window is displayed as an icon. The currently active window remains active.










نوشتن ولیو در رجیستری




Set the registry flag to display Hidden and System files in Windows Explorer


WScript.Shell


 _"myKey="HKCU\Software\Microsoft\Windows

"\CurrentVersion\Explorer\Advanced\Hidden\"

"WshShell.RegWrite myKey,1,"REG_DWORD




"WScript.Shell"
Methods
   .AppActivate      'Activate running command.
   .Run              'Run an application
   .TileVertically   'Tile app windows
   .RegRead          'Read from registry
   .RegDelete        'Delete from registry
   .RegWrite         'Write to the registry


"Shell.Application"

Methods
   .CanStartStopService("ServiceName")   'Can the current user start/stop the named service?
   .CascadeWindows      'Arrange app windows
   .EjectPC             'Eject PC from a docking station
   .Explore(FolderPath) 'Open a folder
   .FileRun             'Open the File-run dialogue
   .GetSystemInformation("PhysicalMemoryInstalled")  'Physical memory installed, in bytes.
   .IsServiceRunning("ServiceName")  'Check if a Windows service is running
   .MinimizeAll         'Minimize everything
   .NameSpace("C:\\")   'Create an object reference to a folder
   .ServiceStart("ServiceName", true)  'Start a windows service
   .ServiceStop("ServiceName", true)   'Stop a windows service
   .SetTime             'Open the set time GUI
   .ShellExecute        'Run a script or application
   .ShutdownWindows
   .TileHorizontally   'Tile app windows
   .TileVertically     'Tile app windows
   .ToggleDesktop      'Show/Hide Desktop
   .TrayProperties     'Display the Taskbar/Start Menu Properties
   .UndoMinimizeAll    'Un-Minimize everything







باز کردن فولدر مشخص شده


Shell.Application

ساخت آبجکت بالا 

("\\:Shell.Open("C.



ثابت های فولدرهای خاص یا SpecialFolder 


Environment.SpecialFolderEnum




Desktop                    0

Cookies                   33

DesktopDirectory   16

Favorites                  6

Fonts                        20

History                     34

MyComputer           17

MyDocuments         5

MyMusic                  13

MyPictures               39

MyVideos                 14

ProgramFiles           38

ProgramFilesX86    42

Recent                      8

StartMenu               11

Startup                     7

System                    37

Windows                 36

دسترسی به فولدرهای ویندوز ( ایجاد شی فولدر ) / کپی آیتم ( ها )به فولدر


Shell.Application


برای دسترسی به آیتم  یا آیتم های  فولدرهای  خاص استفاده از متد شی Folder  که Items هست 



متد CopyHere ... آیتم یا آیتم هایی را به فولدراضافه میکند 


("CopyHere("C:\AUTOEXEC.BAT.






مثال :  ثابت هگزا H14& یا دسیمال 20 ( حاصل 1 ×16+4) برای فولدر Fonts

(NameSpace(&H14.