Convert string to date
Function ConvertStringToDate(dateString As String) As Date
Dim parts() As String
parts = Split(dateString, ".")
If UBound(parts) = 2 Then
' parts(0) = day, parts(1) = month, parts(2) = year
ConvertStringToDate = DateSerial(CInt(parts(2)), CInt(parts(1)), CInt(parts(0)))
Else
Err.Raise vbObjectError + 1, , "Invalid date format."
End If
End Function
यह VBA function ConvertStringToDate, Excel या Word में किसी text date (जैसे "25.12.2025") को real Date value में convert करता है।
यह इस प्रकार काम करता है:
- यह function, Split(dateString, ".") के साथ dot (.) को separator के रूप में उपयोग करके input text को separate करता है।
- यह तीन भागों को expect करता है: day, month और year।
- यदि format सही है, तो यह इन parts से एक valid date बनाने के लिए DateSerial का उपयोग करता है।
- यदि format गलत है (तीन parts नहीं), तो यह "Invalid date format" वाला एक error message raise करता है।
यह उचित date conversion और validation सुनिश्चित करता है।
Remove Duplicate
Function RemoveDuplicates(arr As Variant) As Variant
Dim dict As Object
Dim item As Variant
Dim result() As Variant
Dim i As Long
Set dict = CreateObject("Scripting.Dictionary")
' Add unique items to dictionary
On Error Resume Next
For Each item In arr
If Not dict.exists(item) Then
dict.Add item, Nothing
End If
Next item
On Error GoTo 0
' Transfer dictionary keys to result array
ReDim result(1 To dict.Count) 'ReDim result(0 To dict.Count-1)
i = 1 'i=0
For Each item In dict.Keys
result(i) = item
i = i + 1
Next item
RemoveDuplicates = result
End Function
यह VBA fuction RemoveDuplicates किसी array से duplicate values को हटाता है।
यह इस प्रकार काम करता है:
- यह एक dictionary object बनाता है, जो केवल unique keys store करता है।
- Cod input array arr में प्रत्येक item के माध्यम से loop करता है।
- यदि कोई item पहले से dictionary में नहीं है, तो यह उसे जोड़ देता है। इससे duplicate automatic रूप से हट जाते हैं।
- सभी unique items एकत्र करने के बाद, यह dictionary keys (unique values) को result नामक एक नई array में copy करता है।
- अंत में, यह केवल unique items वाली यह result array लौटाता है।
यह function VBA में duplicate data को साफ़ करने के लिए उपयोगी है।
Get user ID
Public Function UserID()
UserID = Environ$("UserName")
End Function
यह VBA function UserID, code चलाने वाले व्यक्ति का current Windows username लौटाता है।
यह इस प्रकार काम करता है:
- यह function Environ$ command का उपयोग करता है, जो computer के environment variables से जानकारी प्राप्त करता है।
- "UserName" एक ऐसा environment variable है जो logged-in user का नाम store करता है।
- जब function चलता है, तो यह UserID को username value assign करता है और उसे function result के रूप में लौटाता है।
उदाहरण के लिए, यदि आपका Windows username Ram है, तो function Ram लौटाएगा। यह users को track करने या Excel या Word VBA में report को customize करने के लिए उपयोगी है।
Get user Name
Public Function UserName()
UserName = Application.UserName
End Function
यह VBA function UserName, Microsoft Office application में set किए गए person का name लौटाता है।
यह इस प्रकार काम करता है:
- यह function Application.UserName का उपयोग करता है, जो Office user settings (आमतौर पर File → Option → General → User Name के अंतर्गत) में enter किए गए name को retrieve करता है।
- जब function चलता है, तो यह UserName को यह name assign करता है और उसे लौटाता है।
उदाहरण के लिए, यदि Excel user name Ram पर set है, तो function Ram लौटाएगा।
यह document creator की पहचान करने या Excel या Word report में personalize details जोड़ने के लिए उपयोगी है।
Get Outlook properties like mobile number, email etc.
Function ResolveDisplayNameToSMTP(sFromName) As String
' takes a Display Name (i.e. "Ram Kumar") and turns it into an email address (ram.kumar@myco.com)
' necessary because the Outlook address is a long, convoluted string when the email is going to someone in the organization.
' source: https://stackoverflow.com/questions/31161726/creating-a-check-names-button-in-excel
Dim olApp As Object 'Outlook.Application
' Dim myNamespace As Outlook.Namespace
' Dim oRecip As Outlook.Recipient
Dim oRecip As Object 'Outlook.Recipient
Dim oEU As Object 'Outlook.ExchangeUser
'Dim oEDL As Object 'Outlook.ExchangeDistributionList
Set olApp = CreateObject("Outlook.Application")
Set oRecip = olApp.Session.CreateRecipient(sFromName)
' Set myNamespace = Application.GetNamespace("MAPI")
' Set oRecip = myNamespace.CreateRecipient(sFromName)
oRecip.Resolve
'Sleep 1000
If Not oRecip.Resolved Then
oRecip.Resolve
End If
If oRecip.Resolved Then
Select Case oRecip.AddressEntry.AddressEntryUserType
Case 0, 5 'olExchangeUserAddressEntry & olExchangeRemoteUserAddressEntry
Set oEU = oRecip.AddressEntry.GetExchangeUser
If Not (oEU Is Nothing) Then
'ResolveDisplayNameToSMTP = oEU.PrimarySmtpAddress 'for email
'ResolveDisplayNameToSMTP = oEU.MobileTelephoneNumber ' for mobile number
ResolveDisplayNameToSMTP = oEU.Name 'for name
End If
Case 10, 30 'olOutlookContactAddressEntry & 'olSmtpAddressEntry
'ResolveDisplayNameToSMTP = oRecip.AddressEntry.Address
ResolveDisplayNameToSMTP = oRecip.AddressEntry.Address
End Select
End If
'Sleep 2000
'Set olApp = Nothing
'Set oRecip = Nothing
Set oEU = Nothing
Set oRecip = Nothing
Set olApp = Nothing
End Function
यह VBA function ResolveDisplayNameToSMTP, किसी Outlook display name (जैसे "Ram Kumar") को किसी अधिक उपयोगी रूप में परिवर्तित करता है, जैसे कि email address, mobile number या name।
यह इस प्रकार काम करता है:
- यह CreateObject("Outlook.Application") का उपयोग करके Outlook से connect होता है।
- यह दिए गए नाम के लिए एक recipient object बनाता है और उसे Outlook की address book में resolve करने का प्रयास करता है।
- सफल होने पर, यह address entry के type की जाँच करता है।
- Internal users के लिए, यह primary SMTP email, mobile number या display name (बिना comment की गई lines के आधार पर) लौटा सकता है।
- इसके बाद यह सभी Outlook object को memory free करने के लिए release कर देता है।
यह function Outlook में names से users के true email addresses की पहचान करने में मदद करता है।
