How to add a DocumentProperty to CustomDocumentProperties in Excel? - vba

I'm trying to add a DocumentProperty to the CustomDocumentProperties collection. Code as follows:
Sub testcustdocprop()
Dim docprops As DocumentProperties
Dim docprop As DocumentProperty
Set docprops = ThisWorkbook.CustomDocumentProperties
Set docprop = docprops.Add(Name:="test", LinkToContent:=False, Value:="xyz")
End Sub
Running this gives me the following error:
Run-time error '5':
Invalid procedure call or argument
I tried running it with .Add as a void function, like so:
docprops.Add Name:="test", LinkToContent:=False, Value:="xyz"
This gave me the same error. How do I add a custom document property?

Try this routine:
Public Sub updateCustomDocumentProperty(strPropertyName As String, _
varValue As Variant, docType As Office.MsoDocProperties)
On Error Resume Next
ActiveWorkbook.CustomDocumentProperties(strPropertyName).Value = varValue
If Err.Number > 0 Then
ActiveWorkbook.CustomDocumentProperties.Add _
Name:=strPropertyName, _
LinkToContent:=False, _
Type:=docType, _
Value:=varValue
End If
End Sub
Edit: Usage Examples
Five years later and the 'official' documentation is still a mess on this... I figured I'd add some examples of usage:
Set Custom Properties
Sub test_setProperties()
updateCustomDocumentProperty "my_API_Token", "AbCd1234", msoPropertyTypeString
updateCustomDocumentProperty "my_API_Token_Expiry", #1/31/2019#, msoPropertyTypeDate
End Sub
Get Custom Properties
Sub test_getProperties()
MsgBox ActiveWorkbook.CustomDocumentProperties("my_API_Token") & vbLf _
& ActiveWorkbook.CustomDocumentProperties("my_API_Token_Expiry")
End Sub
List All Custom Properties
Sub listCustomProps()
Dim prop As DocumentProperty
For Each prop In ActiveWorkbook.CustomDocumentProperties
Debug.Print prop.Name & " = " & prop.Value & " (" & Choose(prop.Type, _
"msoPropertyTypeNumber", "msoPropertyTypeBoolean", "msoPropertyTypeDate", _
"msoPropertyTypeString", "msoPropertyTypeFloat") & ")"
Next prop
End Sub
Delete Custom Properties
Sub deleteCustomProps()
ActiveWorkbook.CustomDocumentProperties("my_API_Token").Delete
ActiveWorkbook.CustomDocumentProperties("my_API_Token_Expiry").Delete
End Sub

I figured I should extend the above answer from 2013 to work without having to pass in the docType argument:
Private Function getMsoDocProperty(v As Variant) As Integer
'VB TYPES:
'vbEmpty 0 Empty (uninitialized)
'vbNull 1 Null (no valid data)
'vbInteger 2 Integer
'vbLong 3 Long integer
'vbSingle 4 Single-precision floating-point number
'vbDouble 5 Double-precision floating-point number
'vbCurrency 6 Currency value
'vbDate 7 Date value
'vbString 8 String
'vbObject 9 Object
'vbError 10 Error value
'vbBoolean 11 Boolean value
'vbVariant 12 Variant (used only with arrays of variants)
'vbDataObject 13 A data access object
'vbDecimal 14 Decimal value
'vbByte 17 Byte value
'vbUserDefinedType 36 Variants that contain user-defined types
'vbArray 8192 Array
'OFFICE.MSODOCPROPERTIES.TYPES
'msoPropertyTypeNumber 1 Integer value.
'msoPropertyTypeBoolean 2 Boolean value.
'msoPropertyTypeDate 3 Date value.
'msoPropertyTypeString 4 String value.
'msoPropertyTypeFloat 5 Floating point value.
Select Case VarType(v)
Case vbInteger, vbLong
getMsoDocProperty = Office.MsoDocProperties.msoPropertyTypeNumber
Case vbBoolean
getMsoDocProperty = Office.MsoDocProperties.msoPropertyTypeBoolean
Case vbDate
getMsoDocProperty = Office.MsoDocProperties.msoPropertyTypeDate
Case vbString, vbByte
getMsoDocProperty = Office.MsoDocProperties.msoPropertyTypeString
Case vbSingle, vbDouble, vbCurrency,vbDecimal
getMsoDocProperty = Office.MsoDocProperties.msoPropertyTypeFloat
Case Else
getMsoDocProperty = 0
End Select
End Function
Public Sub subUpdateCustomDocumentProperty(ByVal doc as object, ByVal strPropertyName As String, _
ByVal varValue As Variant, Optional ByVal docType As Office.MsoDocProperties = 0)
If docType = 0 Then docType = getMsoDocProperty(varValue)
If docType = 0 Then
MsgBox "An error occurred in ""subUpdateCustomDocumentProperty"" routine", vbCritical
Exit Sub
End If
On Error Resume Next
doc.CustomDocumentProperties(strPropertyName).Value _
= varValue
If Err.Number > 0 Then
doc.CustomDocumentProperties.Add _
Name:=strPropertyName, _
LinkToContent:=False, _
Type:=docType, _
Value:=varValue
End If
End Sub

Related

Parse Backwards in VBA for a Valid Function Name

Update
This excel-names project is contained in VBA, and it might offer some insights on the validity of various names in Excel and VBA. Unfortunately, it cannot fully guarantee validity here:
Names_IsValidName(sNameToTest As String) As Boolean
Check if the name is valid:
true: Excel name is probably valid
false: Excel name is for sure not valid
Additionally, the XLParser project in C# specifies here many of the special operators and characters in Excel formulae.
However, I am still trying to determine exactly how these might apply to my solution, which must be contained in VBA.
Background
I am writing in VBA a lightweight parser for Excel formulae, which assumes that any input is syntactically valid (as validated by Excel). My goal is to properly pinpoint an entire function call within a formula.
For this, I need a function FindValidName(). This function accepts a String ending at the open parenthesis (() of a function call, and searches backward to locate the beginning of the valid formula name; or it returns -1 if no such name is found.
Question
What are the syntactic criteria for a valid function name, accessible in an Excel formula? This does not encompass every function like VBA.Mid() accessible in VBA, since those are not accessible in Excel itself. Neither does it encompass user-defined functions (UDFs) in VBA, which are accessible in Excel and have their nomenclature defined here.
More generally, how might one algorithmize in VBA a backwards search by FindValidName() from the end of the name? Mind you, this must encompass ๐‘‹ โˆช ๐‘ˆ, where: ๐‘‹ is the set of names for all native functions that could ever be accessed in an Excel formula; and ๐‘ˆ is the set of (syntactically valid) names for all UDFs that could ever be defined in VBA and accessed in Excel.
Note
We can assume that the formula is syntactically valid. So if we encounter (searching "backwards" from right to left) a character like + that is illegal (?) for a function name, we know that this is not simply a typo. Rather, we know that any name must start to the right of ("before") that +
= A1+SUM(B1,C1)
|<-|
and if there are no characters "before" the +, then the ( is simply a grouping operation:
= A1+(B1*C1)
|
What I Have
Given the below formula extracted as a String via .Formula, I can already pinpoint where a call begins.
= A1 & CONCAT(B1, C1)
^ ^
14 21
I have already generalized this to FindFormulaCall(fml, [n]), which can locate the nth call (here the 2nd) in an arbitrarily complex formula (below).
= SUM(ABS(A1:A5)) + ABS(My_Table[#[Column Name With Punctuation: ", '#, (, ), '[, and '].]]) + ABS(B1)
^ ^
24 92
What I Need
Now I need FindValidName("= A1 & CONCAT") to "search backwards" until the start of "CONCAT"
= A1 & CONCAT(B1, C1)
|<----|
6
and return the Long number of characters (measured from the end) at which that start occurs: here 6. Alternatively, it should return -1 if no valid name is encountered.
Examples
For FindValidName("= A1 & CONCAT") the result should be 6:
= A1 & CONCAT(B1, C1)
|<----|
6
For FindValidName("= A1+SUM(B1)+ABS") the result should be 3:
= A1+SUM(B1)+ABS(C1)
|<-|
3
For FindValidName("= A1*SUM(B1)*") the result should be -1 for no valid name:
= A1*SUM(B1)*(C1 + D1)
|
I don't know if the two code samples below are of any use to you. I wrote them some time ago for a teacher friend who was trying to show how Excel worked.
The first parses the formula of a cell into their individual formulas and shows how each formula resolves to the next.
The second gets a list of every formula available in a particular workbook.
Formula Parser
For the first, you'll need two helper classes, called clsParenthetical
Option Explicit
Public IsFormula As Boolean
Public FormulaName As String
Public Expression As String
Public Result As Variant
Public Index As Long
and clsParsedFormula
Option Explicit
Public KeyText As String
Public Perentheticals As Collection
Then, the main class, called clsFormulaHelper, is this:
Option Explicit
'===========================================================================
'DECLARATIONS
'===========================================================================
'---------------------------------------------------------------------------
'region PRIVATE TYPES
Private Type Points
IsValid As Boolean
OpenAt As Long
CloseAt As Long
End Type
Private Type ExprKVP
Index As Long
Value As String
End Type
'end region PRIVATE TYPES
'---------------------------------------------------------------------------
'---------------------------------------------------------------------------
'region PRIVATE CONSTANTS
Private Const ARITHMETIC_OPR_TAG As String = "Arith"
Private Const COMPARISON_OPR_TAG As String = "Comp"
Private Const TEXT_OPR_TAG As String = "Text"
Private Const REFERENCE_OPR_TAG As String = "Ref"
'end region PRIVATE CONSTANTS
'---------------------------------------------------------------------------
'---------------------------------------------------------------------------
'region MODULE-LEVEL VARIABLES
Private mOperators As Collection
Private mRngOperators As Variant
'end region MODULE-LEVEL VARIABLES
'---------------------------------------------------------------------------
'===========================================================================
'METHODS
'===========================================================================
Public Function ParseFormula(rng As Range) As clsParsedFormula
Const ARRAY_INC As Long = 100
Dim pts As Points
Dim quotes() As ExprKVP
Dim parenthetical As clsParenthetical, p As clsParenthetical
Dim ex As String
Dim c As Long, i As Long
Dim v As Variant
'Validation check.
If rng Is Nothing Then Exit Function
If rng.Cells.Count > 1 Then Exit Function
If Not rng.HasFormula Then Exit Function
Set ParseFormula = New clsParsedFormula
With ParseFormula
.KeyText = rng.Formula
'Remove any strings from the formula
'in case target characters (eg '(' or ')')
'are contained within the strings.
ReDim quotes(ARRAY_INC)
c = -1
Do
pts = FindQuotation(.KeyText)
If pts.IsValid Then
c = c + 1
'Resize the array if limit is reached.
If c > UBound(quotes) Then
ReDim Preserve quotes(UBound(quotes) + ARRAY_INC)
End If
'Populate the quote item with the quotation
'and index value.
With quotes(c)
.Value = Mid(ParseFormula.KeyText, _
pts.OpenAt, _
pts.CloseAt - pts.OpenAt + 1)
.Index = c
End With
'Replace the quotation with the key.
.KeyText = Left(.KeyText, pts.OpenAt - 1) & _
StringKeyBuilder(c) & _
Right(.KeyText, Len(.KeyText) - pts.CloseAt)
End If
'Coninue until no further quotations found.
Loop Until Not pts.IsValid
'Reduce array to correct size.
If c = -1 Then
Erase quotes
Else
ReDim Preserve quotes(c)
End If
Set .Perentheticals = New Collection
c = -1
Do
pts = FindDeepestOpenAndClose(.KeyText)
If pts.IsValid Then
c = c + 1
ex = ExtractBackToOperator(pts.OpenAt, .KeyText)
'Populate the parenthesis item.
Set parenthetical = New clsParenthetical
With parenthetical
.Index = c
.IsFormula = Len(ex) > 0
If .IsFormula Then
.FormulaName = ex
pts.OpenAt = pts.OpenAt - Len(ex)
End If
.Expression = Mid(ParseFormula.KeyText, _
pts.OpenAt, _
pts.CloseAt - pts.OpenAt + 1)
End With
.Perentheticals.Add parenthetical, CStr(c)
'Replace the parenthesis with the key.
.KeyText = Left(.KeyText, pts.OpenAt - 1) & _
FormulaKeyBuilder(c) & _
Right(.KeyText, Len(.KeyText) - pts.CloseAt)
End If
Loop Until Not pts.IsValid
'Calculate the results.
For Each parenthetical In .Perentheticals
ex = parenthetical.Expression
pts.OpenAt = 1
pts.CloseAt = 0
'Replace the string expressions.
Do While True
pts.OpenAt = InStr(pts.OpenAt, parenthetical.Expression, "{str")
If pts.OpenAt = 0 Then Exit Do
pts.CloseAt = InStr(pts.OpenAt, parenthetical.Expression, "}")
If pts.CloseAt = 0 Then Exit Do
i = ExtractIndexFromStringExpression(parenthetical.Expression, pts)
If i >= 0 And i <= UBound(quotes) Then
ex = Replace(ex, "{str" & i & "}", quotes(i).Value)
End If
pts.OpenAt = pts.CloseAt + 1
Loop
'Replace the function expressions.
pts.OpenAt = 1
pts.CloseAt = 0
Do While True
pts.OpenAt = InStr(pts.OpenAt, parenthetical.Expression, "{f")
If pts.OpenAt = 0 Then Exit Do
pts.CloseAt = InStr(pts.OpenAt, parenthetical.Expression, "}")
If pts.CloseAt = 0 Then Exit Do
i = ExtractIndexFromFunctionExpression(parenthetical.Expression, pts)
If i > -1 Then
Set p = .Perentheticals(CStr(i))
If Not p Is Nothing Then
ex = Replace(ex, "{f" & i & "}", p.Result)
End If
End If
pts.OpenAt = pts.CloseAt + 1
Loop
parenthetical.Expression = ex
v = Evaluate(ex)
If VarType(v) = vbString Then
v = chr(34) & v & chr(34)
End If
parenthetical.Result = v
Next
End With
End Function
'===========================================================================
'PRIVATE HELPER FUNCTIONS
'===========================================================================
'---------------------------------------------------------------------------
'Purpose: Converts a Long to a string key in the format {f}.
'#i: Long to be turned into formula key.
'---------------------------------------------------------------------------
Private Function FormulaKeyBuilder(i As Long) As String
FormulaKeyBuilder = "{f" & i & "}"
End Function
'---------------------------------------------------------------------------
'Purpose: Converts a Long to a string key in the format {strn}.
'#i: Long to be turned into string key.
'---------------------------------------------------------------------------
Private Function StringKeyBuilder(i As Long) As String
StringKeyBuilder = "{str" & i & "}"
End Function
'---------------------------------------------------------------------------
'Purpose: Finds position of open and close inverted commas.
'#txt: string to be searched.
'Note 1: finds closest quotation to start of string.
'Returns: Points type. If unsuccessful IsValid = False.
'---------------------------------------------------------------------------
Private Function FindQuotation(txt As String) As Points
Dim openPt As Long, closePt As Long, tmp As Long
On Error GoTo EH
openPt = InStr(txt, """")
If openPt = 0 Then GoTo EH
tmp = openPt
Do While True
tmp = InStr(tmp + 1, txt, """")
If tmp = 0 Then GoTo EH
If tmp = Len(txt) Then closePt = tmp: Exit Do
If Mid(txt, tmp + 1, 1) <> """" Then closePt = tmp: Exit Do
tmp = tmp + 1
Loop
With FindQuotation
.IsValid = True
.OpenAt = openPt
.CloseAt = closePt
End With
Exit Function
EH:
FindQuotation.IsValid = False
End Function
'---------------------------------------------------------------------------
'Purpose: Finds position of open and close parenthensis at deepest level.
'#txt: string to be searched.
'Note 1: finds deepest Expr closest to start of string.
'Returns: Points type. If unsuccessful IsValid = False.
'---------------------------------------------------------------------------
Private Function FindDeepestOpenAndClose(txt As String) As Points
Dim openPt As Long, closePt As Long
On Error GoTo EH
closePt = InStr(txt, ")")
If closePt = 0 Then GoTo EH
openPt = InStrRev(txt, "(", closePt)
If openPt = 0 Then GoTo EH
With FindDeepestOpenAndClose
.IsValid = True
.OpenAt = openPt
.CloseAt = closePt
End With
Exit Function
EH:
FindDeepestOpenAndClose.IsValid = False
End Function
'---------------------------------------------------------------------------
'Purpose: Determines if char is any Excel operator.
'#char: Character to be evaluated.
'Returns: True if successful.
'---------------------------------------------------------------------------
Private Function IsOperator(char As String, _
Optional arithOpr As Boolean = True, _
Optional compOpr As Boolean = True, _
Optional textOpr As Boolean = True, _
Optional refOpr As Boolean = True) As Boolean
Dim exists As Boolean
Dim opr As Collection
On Error Resume Next
If arithOpr Then
Set opr = mOperators(ARITHMETIC_OPR_TAG)
If Not opr Is Nothing Then
exists = opr(char)
If exists Then
IsOperator = True
Exit Function
End If
End If
End If
If compOpr Then
Set opr = mOperators(COMPARISON_OPR_TAG)
If Not opr Is Nothing Then
exists = opr(char)
If exists Then
IsOperator = True
Exit Function
End If
End If
End If
If textOpr Then
Set opr = mOperators(TEXT_OPR_TAG)
If Not opr Is Nothing Then
exists = opr(char)
If exists Then
IsOperator = True
Exit Function
End If
End If
End If
If refOpr Then
Set opr = mOperators(REFERENCE_OPR_TAG)
If Not opr Is Nothing Then
exists = opr(char)
If exists Then
IsOperator = True
Exit Function
End If
End If
End If
End Function
'---------------------------------------------------------------------------
'Purpose: Extracts string from a nominated position backwards as far as
' any operator or start of string.
'#pt: position in string to commence extraction.
'#str: string for extraction.
'Note 1: Target string length must be greater than 1.
'Returns: Extracted text or null string if no operator is found.
'---------------------------------------------------------------------------
Private Function ExtractBackToOperator(pt As Long, str As String) As String
Dim i As Long
Dim chr As String
If pt < 2 Then Exit Function
For i = pt - 1 To 1 Step -1
chr = Mid(str, i, 1)
If IsOperator(chr) Then
ExtractBackToOperator = Mid(str, i + 1, pt - (i + 1))
Exit Function
End If
Next
End Function
'---------------------------------------------------------------------------
'Purpose: Extracts index number from function expression.
'#expr: Target expression.
'#pts: start and end point of function.
'Returns: Index number or -1 of failed.
'---------------------------------------------------------------------------
Private Function ExtractIndexFromFunctionExpression(expr As String, pts As Points) As Long
On Error GoTo EH
ExtractIndexFromFunctionExpression = Mid(expr, pts.OpenAt + 2, pts.CloseAt - pts.OpenAt - 2)
Exit Function
EH:
ExtractIndexFromFunctionExpression = -1
End Function
'---------------------------------------------------------------------------
'Purpose: Extracts index number from string expression.
'#expr: Target expression.
'#pts: start and end point of function.
'Returns: Index number or -1 of failed.
'---------------------------------------------------------------------------
Private Function ExtractIndexFromStringExpression(expr As String, pts As Points) As Long
On Error GoTo EH
ExtractIndexFromStringExpression = Mid(expr, pts.OpenAt + 4, pts.CloseAt - pts.OpenAt - 4)
Exit Function
EH:
ExtractIndexFromStringExpression = -1
End Function
Private Sub Class_Initialize()
Dim opr As Collection
Set mOperators = New Collection
Set opr = New Collection
opr.Add True, "+"
opr.Add True, "-"
opr.Add True, "*"
opr.Add True, "/"
opr.Add True, "%"
opr.Add True, "^"
mOperators.Add opr, ARITHMETIC_OPR_TAG
Set opr = New Collection
opr.Add True, "="
opr.Add True, ">"
opr.Add True, "<"
mOperators.Add opr, COMPARISON_OPR_TAG
Set opr = New Collection
opr.Add True, "&"
mOperators.Add opr, TEXT_OPR_TAG
Set opr = New Collection
opr.Add True, ":"
opr.Add True, ","
opr.Add True, " "
mOperators.Add opr, REFERENCE_OPR_TAG
End Sub
You would call it in a module like this:
Dim helper As clsFormulaHelper
Dim parsedFormula As clsParsedFormula
Dim parenthetical As clsParenthetical
Set helper = New clsFormulaHelper
Set parsedFormula = helper.ParseFormula(Sheet1.Range("A4"))
Here are the outputs:
For formula: =IF(A1*(A1 + 'AB + CD'!A2) = 3, SUM(A1,A2,'AB + CD'!A3),
IF(A1 = 2, AVERAGE(A1:A3),"c" & COUNT(A1:A3)))
Nested index: 0, Expr: (A1 + 'AB + CD'!A2), Result: 3
Nested index: 1, Expr: SUM(A1,A2,'AB + CD'!A3), Formula: SUM, Result:
6
Nested index: 2, Expr: AVERAGE(A1:A3), Formula: AVERAGE, Result: 2
Nested index: 3, Expr: COUNT(A1:A3), Formula: COUNT, Result: 3
Nested index: 4, Expr: IF(A1 = 2, 2,"c" & 3), Formula: IF, Result:
"c3"
Nested index: 5, Expr: IF(A1*3 = 3, 6, "c3"), Formula: IF, Result: 6
Available Formulas
The second code sample gets a list of all formulas available in the workbook. It will only work on 64-bit.
Option Explicit
'---------------------------------------------------------
'Hook APIs and Constants
'---------------------------------------------------------
Private Declare PtrSafe Function SetWinEventHook _
Lib "user32" _
(ByVal eventMin As Long, _
ByVal eventMax As Long, _
ByVal hmodWinEventProc As LongPtr, _
ByVal pfnWinEventProc As LongPtr, _
ByVal idProcess As Long, _
ByVal idThread As Long, _
ByVal dwFlags As Long) As LongPtr
Private Declare PtrSafe Function UnhookWinEvent _
Lib "user32" _
(ByVal hWinEventHook As LongPtr) As Long
Private Declare PtrSafe Function GetCurrentProcessId _
Lib "kernel32" () As Long
Private Declare PtrSafe Function GetWindowThreadProcessId _
Lib "user32" _
(ByVal hWnd As LongPtr, _
lpdwProcessId As Long) As Long
Private Declare PtrSafe Function GetClassName _
Lib "user32" Alias "GetClassNameA" _
(ByVal hWnd As LongPtr, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Private Const EVENT_OBJECT_CREATE As Long = 32768
Private Const WINEVENT_OUTOFCONTEXT As Long = 0
'---------------------------------------------------------
'Subclassing APIs and Constants
'---------------------------------------------------------
Private Declare PtrSafe Function SetWindowLongPtr _
Lib "user32" Alias "SetWindowLongPtrA" _
(ByVal hWnd As LongPtr, _
ByVal nIndex As Long, _
ByVal dwNewLong As LongPtr) As LongPtr
Private Declare PtrSafe Function CallWindowProc _
Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As LongPtr, _
ByVal hWnd As LongPtr, _
ByVal iMsg As Long, _
ByVal wParam As LongPtr, _
ByVal lParam As LongPtr) As LongPtr
Private Declare PtrSafe Sub CopyMemory _
Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)
Private Declare PtrSafe Function lstrlenW _
Lib "kernel32" _
(ByVal lpString As LongPtr) As Long
Private Const GWLP_WNDPROC As Long = (-4)
Private Const WM_NOTIFY As Long = &H4E
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_GETTOPINDEX As Long = (LVM_FIRST + 39)
Private Const LVM_GETSTRINGWIDTHW = (LVM_FIRST + 87)
'---------------------------------------------------------
'Timer APIs
'---------------------------------------------------------
Private Declare PtrSafe Function SetTimer _
Lib "user32" _
(ByVal hWnd As LongPtr, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As LongPtr) As Long
Public Declare PtrSafe Function KillTimer Lib _
"user32" _
(ByVal hWnd As Long, _
ByVal nIDEvent As Long) As Long
Private Const LISTVIEW_NAME As String = "SysListView32"
Private Const REDUNDANT_LV_STRING As String = "W"
Private mHHook As LongPtr
Private mHListView As LongPtr
Private mPrevWndProc As LongPtr
Private mTimerId As Long
Private mFormulas As Collection
Private mHasFormulas As Boolean
Private mOldA1Value As Variant
Public Property Get GrabbedFormulas() As Collection
Set GrabbedFormulas = mFormulas
End Property
Public Property Get HasGrabbedFormulas() As Boolean
HasGrabbedFormulas = mHasFormulas
End Property
Public Sub GrabEm()
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'ONLY CALL THIS ROUTINE FROM THE WORKSHEET ITSELF.
'EITHER A SHEET BUTTON OR WORKSHEET EVENT WOULD BE OKAY.
'RUNNING FROM VBA EDITOR WILL DO SERIOUS DAMAGE TO YOUR WORK.
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Dim ws As Worksheet
On Error GoTo EH
'Warning
MsgBox _
"Please do not touch the keyboard or activate another window." & vbNewLine & vbNewLine & _
"We are about to grab all of the workbook formulas by simulating key strokes on this sheet. " & vbNewLine & vbNewLine & _
"This routine will take a few seconds.", _
vbOKOnly, _
"Formula Grabber"
'Initialise.
Set mFormulas = New Collection
mHasFormulas = False
Set ws = ThisWorkbook.Worksheets(1)
With ws
.Visible = xlSheetVisible
.Activate
With .Range("A1")
mOldA1Value = .Value
.Select
End With
End With
'First task is to find the SysListView32 handle.
'We'll hook a EVENT_OBJECT_CREATE win event, which
'we can 'coerce' by entering "=a" in a cell.
If mHHook <> 0 Then DetachHook
mHHook = SetWinEventHook(EVENT_OBJECT_CREATE, _
EVENT_OBJECT_CREATE, _
0, _
AddressOf WinEventProc, _
0, _
0, _
WINEVENT_OUTOFCONTEXT)
RunKeySequence Asc("a")
Do While Not mHasFormulas
DoEvents
Loop
EH:
ws.Range("A1").Value = mOldA1Value
ReleaseAll
End Sub
Private Function WinEventProc(ByVal hWinEventHook As LongPtr, _
WinEvent As Long, _
ByVal hWnd As LongPtr, _
ByVal idObject As Long, _
ByVal idChild As Long, _
ByVal idEventThread As Long, _
ByVal dwmsEventTime As Long) As Long
On Error GoTo EH
'We don't want to handle or subclass a window created by a different process.
If Not IsSameProcess(hWnd) Then Exit Function
'Disregard any class that isn't a SysListView32.
If Not IsListView(hWnd) Then Exit Function
'We have the handle, so now we can release the hook
'and create a subclass.
'Note: store the handle for emergency hook destruction.
mHListView = hWnd
DetachHook
mPrevWndProc = SetWindowLongPtr(mHListView, _
GWLP_WNDPROC, _
AddressOf SubbedWndProc)
Exit Function
EH:
End Function
Private Function SubbedWndProc(ByVal hWnd As LongPtr, _
ByVal iMsg As Long, _
ByVal wParam As LongPtr, _
ByVal lParam As LongPtr) As LongPtr
Dim str As String
On Error Resume Next
'The two messages we're interested in are:
'1. LVM_GETSTRINGWIDTHW which is sent
' for each item to size the window.
'2. LVM_GETTOPINDEX which is sent once
' all strings are measured.
Select Case iMsg
Case LVM_GETSTRINGWIDTHW
'A pointer to the string is passed in lParam.
str = PointerToString(lParam)
If str <> REDUNDANT_LV_STRING Then
mFormulas.Add str, str
End If
Case LVM_GETTOPINDEX
'We're ready to move on to next keystroke.
RunKeySequence
End Select
'We're not overriding anything, so pass all messages
'to previous window procedure.
SubbedWndProc = CallWindowProc(mPrevWndProc, _
hWnd, _
iMsg, _
wParam, _
lParam)
End Function
Private Sub RunKeySequence(Optional startKey As Long)
Static key As Long
On Error GoTo EH
'Pause the timer.
StopTimer
'Start the sequence.
If startKey <> 0 Then
key = startKey
Application.SendKeys "="
Application.SendKeys chr(key)
Else
key = key + 1
End If
'End the sequence.
If key < Asc("a") Or key > Asc("z") Then
'Destroy the subclass.
Unsubclass
'Clear the cell.
Application.SendKeys "{BACKSPACE}"
Application.SendKeys "{BACKSPACE}"
Application.SendKeys "{ENTER}"
mHasFormulas = True
Exit Sub
End If
'Send the next keys.
StartTimer
Application.SendKeys "{BACKSPACE}"
'Note: API timer is used in case no formulas begin with
'the entered letter (ie no message would then be
'sent to our wnd proc) so we can time out that letter.
Application.SendKeys chr(key)
Exit Sub
EH:
End Sub
Private Sub StartTimer()
On Error Resume Next
mTimerId = SetTimer(0, 0, 1000, AddressOf TimerProc)
End Sub
Private Sub StopTimer()
On Error Resume Next
KillTimer 0, mTimerId
End Sub
Private Sub TimerProc(ByVal hWnd As LongPtr, ByVal uMsg As Long, ByVal nIDEvent As Long, ByVal dwTimer As Long)
RunKeySequence
End Sub
Private Sub DetachHook()
On Error Resume Next
UnhookWinEvent mHHook
mHHook = 0
End Sub
Private Sub Unsubclass()
On Error Resume Next
SetWindowLongPtr mHListView, GWLP_WNDPROC, mPrevWndProc
mPrevWndProc = 0
End Sub
Private Function IsSameProcess(hWnd) As Boolean
Dim windowId As Long, currentId As Long
On Error GoTo EH
GetWindowThreadProcessId hWnd, windowId
currentId = GetCurrentProcessId
IsSameProcess = (windowId = currentId)
Exit Function
EH:
End Function
Private Function IsListView(hWnd) As Boolean
Dim ret As Long
Dim str As String * 128
Dim clsName As String
On Error GoTo EH
ret = GetClassName(hWnd, str, Len(str))
clsName = Left(str, ret)
IsListView = (clsName = LISTVIEW_NAME)
Exit Function
EH:
End Function
Private Function PointerToString(psz As LongPtr) As String
Dim buffer As Long
Dim str As String
On Error GoTo EH
buffer = lstrlenW(psz) * 2
str = Space(buffer)
CopyMemory ByVal str, ByVal psz, buffer
PointerToString = Replace(str, chr(0), "")
Exit Function
EH:
End Function
Public Sub ReleaseAll()
StopTimer
Unsubclass
DetachHook
End Sub
Function nested index = 2 Expression = AVERAGE(A1:A3) Formula = AVERAGE Evaluated result = 2
Function nested index = 3 Expression = COUNT(A1:A3) Formula = COUNT Evaluated result = 3
Function nested index = 4 Expression = IF(A1 = 2, 2,"c" & 3) Formula = IF Evaluated result = "c3"
Function nested index = 5 Expression = IF(A1*3 = 3, 6, "c3") Formula = IF Evaluated result = 6
I have developed a compact suggestion in VBA, which is here awaiting review on Code Review. It should be easily repurposed into a direct solution for this question.
This suggestion is a single function NameIsValid(name), which evaluates a string and determines if it is a valid name in Excel. With appropriate safeguards in place, it does so by splicing the name into a call to LET(), which it then executes using Application.Evaluate(). An invalid name will yield an Error.
' Check if a name is valid: it may be "declared" in Excel using LET().
Public Function NameIsValid(name As String) As Boolean
' Invalidate names that are empty or too long.
If name = Empty Or VBA.Len(name) > 255 Then
NameIsValid = False
' Invalidate reserved names: "R" and "C".
ElseIf ( _
name = "C" Or name = "c" Or _
name = "R" Or name = "r" _
) Then
NameIsValid = False
' Invalidate names with external whitespace (or double spaces internally),
' which are invalid in names and yet could mesh syntactically with
' formulaic calls to LET() in Excel.
ElseIf name <> Application.WorksheetFunction.Clean(VBA.Trim(name)) Then
NameIsValid = False
' Invalidate names with injection characters, which are invalid in names
' and also disrupt formulaic calls to LET() in Excel.
ElseIf ( _
VBA.InStr(1, name, "(") Or _
VBA.InStr(1, name, ",") Or _
VBA.InStr(1, name, ";") Or _
VBA.InStr(1, name, ")") _
) Then
NameIsValid = False
' If we pass the above checks, we can safely splice the name into a
' formulaic declaration with LET() in Excel.
Else
' Get the result of formulaically declaring a name with LET() in Excel.
Dim eval As Variant
eval = Application.Evaluate("= LET(" & name & ", 0, 0)")
' Check if the declaration erred due to invalid nomenclature.
If IsError(eval) Then
NameIsValid = False
Else
NameIsValid = True
End If
End If
End Function

Save image from clipboard as .jpg and pass relative link to Access form

Using Access 365. I would like help building code to achieve the following please, Iโ€™m trying to streamline adding images to a record.
I would like to use VBA that on clicking a button will save an image as a .jpg from the clipboard (put there by User using Snip tool) to a subfolder of the database, then pass a relative link to this file to the form. Iโ€™d like to be able to attach multiple imagesโ€™ links to a given record in this manner.
Using the code below (without the AltPrintScreen element) Iโ€™ve gotten as far as saving from the clipboard and generating an absolute link, but only as a .bmp. (https://www.access-programmers.co.uk/forums/threads/print-screen-into-image-file.245198/). Grateful for any help getting the rest of the way, or suggestion of an entirely different way of doing it. Cheers!
Module...
'***********************************************************************************************
' *
' * Please leave any Trademarks or Credits in place.
' *
' * ACKNOWLEDGEMENT TO CONTRIBUTORS :
' * STEPHEN BULLEN, 15 November 1998 - Original PastPicture code
' * G HUDSON, 5 April 2010 - Pause Function
' * LUTZ GENTKOW, 23 July 2011 - Alt + PrtScrn
' * PAUL FRANCIS, 11 April 2013 - Putting all pieces togeather
' *
' * DESCRIPTION: Creates a standard Picture object from whatever is on the clipboard.
' * This object is then saved to a location on the disc. Please note, this
' * can also be assigned to (for example) and Image control on a userform.
' *
' * The code requires a reference to the "OLE Automation" type library.
' *
' * The code in this module has been derived from a number of sources
' * discovered on MSDN, Access World Forum, VBForums.
' *
' * To use it, just copy this module into your project, then you can use:
' * SaveClip2Bit("C:\Pics\Sample.bmp")
' * to save this to a location on the Disc.
' * (Or)
' * Set ImageControl.Image = PastePicture
' * to paste a picture of whatever is on the clipboard into a standard image control.
' *
' * PROCEDURES:
' * PastePicture : The entry point for 'Setting' the Image
' * CreatePicture : Private function to convert a bitmap or metafile handle to an OLE reference
' * fnOLEError : Get the error text for an OLE error code
' * SaveClip2Bit : The entry point for 'Saving' the Image, calls for PastePicture
' * AltPrintScreen: Performs the automation of Alt + PrtScrn, for getting the Active Window.
' * Pause : Makes the program wait, to make sure proper screen capture takes place.
'**************************************************************************************************
Option Explicit
Option Compare Text
'Declare clipboard clear
Public Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Public Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long
'Declare a UDT to store a GUID for the IPicture OLE Interface
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
'Declare a UDT to store the bitmap information
Private Type uPicDesc
Size As Long
Type As Long
hPic As Long
hPal As Long
End Type
'Windows API Function Declarations
#If Win64 = 1 And VBA7 = 1 Then
'Does the clipboard contain a bitmap/metafile?
Private Declare PtrSafe Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Long
'Open the clipboard to read
'Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
'Get a pointer to the bitmap/metafile
Private Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As Long
'Close the clipboard
'Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
'Convert the handle into an OLE IPicture interface.
Private Declare PtrSafe Function OleCreatePictureIndirect Lib "oleaut32.dll" (PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
'Create our own copy of the metafile, so it doesn't get wiped out by subsequent clipboard updates.
Declare PtrSafe Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" (ByVal hemfSrc As Long, ByVal lpszFile As String) As Long
'Create our own copy of the bitmap, so it doesn't get wiped out by subsequent clipboard updates.
Declare PtrSafe Function CopyImage Lib "user32" (ByVal handle As Long, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
'Uses the Keyboard simulation
Private Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
#Else
'Does the clipboard contain a bitmap/metafile?
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Long
'Open the clipboard to read
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
'Get a pointer to the bitmap/metafile
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As Long
'Close the clipboard
Private Declare Function CloseClipboard Lib "user32" () As Long
'Convert the handle into an OLE IPicture interface.
Private Declare Function OleCreatePictureIndirect Lib "oleaut32.dll" (PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
'Create our own copy of the metafile, so it doesn't get wiped out by subsequent clipboard updates.
Declare Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" (ByVal hemfSrc As Long, ByVal lpszFile As String) As Long
'Create our own copy of the bitmap, so it doesn't get wiped out by subsequent clipboard updates.
Declare Function CopyImage Lib "user32" (ByVal handle As Long, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
'Uses the Keyboard simulation
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
#End If
'The API format types we're interested in
Const CF_BITMAP = 2
Const CF_PALETTE = 9
Const CF_ENHMETAFILE = 14
Const IMAGE_BITMAP = 0
Const LR_COPYRETURNORG = &H4
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_SNAPSHOT = &H2C
Private Const VK_MENU = &H12
' Subroutine : AltPrintScreen
' Purpose : Capture the Active window, and places on the Clipboard.
Sub AltPrintScreen()
'keybd_event VK_MENU, 0, 0, 0
'keybd_event VK_SNAPSHOT, 0, 0, 0
'keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0
'keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
End Sub
' Subroutine : PastePicture
' Purpose : Get a Picture object showing whatever's on the clipboard.
Function PastePicture() As IPicture
'Some pointers
Dim h As Long, hPtr As Long, hPal As Long, lPicType As Long, hCopy As Long
'Check if the clipboard contains the required format
If IsClipboardFormatAvailable(CF_BITMAP) Then
'Get access to the clipboard
h = OpenClipboard(0&)
If h > 0 Then
'Get a handle to the image data
hPtr = GetClipboardData(CF_BITMAP)
hCopy = CopyImage(hPtr, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)
'Release the clipboard to other programs
h = CloseClipboard
'If we got a handle to the image, convert it into a Picture object and return it
If hPtr <> 0 Then Set PastePicture = CreatePicture(hCopy, 0, CF_BITMAP)
End If
End If
End Function
' Subroutine : CreatePicture
' Purpose : Converts a image (and palette) handle into a Picture object.
' NOTE : Requires a reference to the "OLE Automation" type library
Private Function CreatePicture(ByVal hPic As Long, ByVal hPal As Long, ByVal lPicType) As IPicture
' IPicture requires a reference to "OLE Automation"
Dim r As Long, uPicInfo As uPicDesc, IID_IDispatch As GUID, IPic As IPicture
'OLE Picture types
Const PICTYPE_BITMAP = 1
Const PICTYPE_ENHMETAFILE = 4
' Create the Interface GUID (for the IPicture interface)
With IID_IDispatch
.Data1 = &H7BF80980
.Data2 = &HBF32
.Data3 = &H101A
.Data4(0) = &H8B
.Data4(1) = &HBB
.Data4(2) = &H0
.Data4(3) = &HAA
.Data4(4) = &H0
.Data4(5) = &H30
.Data4(6) = &HC
.Data4(7) = &HAB
End With
' Fill uPicInfo with necessary parts.
With uPicInfo
.Size = Len(uPicInfo) ' Length of structure.
.Type = PICTYPE_BITMAP ' Type of Picture
.hPic = hPic ' Handle to image.
.hPal = hPal ' Handle to palette (if bitmap).
End With
' Create the Picture object.
r = OleCreatePictureIndirect(uPicInfo, IID_IDispatch, True, IPic)
' If an error occured, show the description
If r <> 0 Then Debug.Print "Create Picture: " & fnOLEError(r)
' Return the new Picture object.
Set CreatePicture = IPic
End Function
' Subroutine : fnOLEError
' Purpose : Gets the message text for standard OLE errors
Private Function fnOLEError(lErrNum As Long) As String
'OLECreatePictureIndirect return values
Const E_ABORT = &H80004004
Const E_ACCESSDENIED = &H80070005
Const E_FAIL = &H80004005
Const E_HANDLE = &H80070006
Const E_INVALIDARG = &H80070057
Const E_NOINTERFACE = &H80004002
Const E_NOTIMPL = &H80004001
Const E_OUTOFMEMORY = &H8007000E
Const E_POINTER = &H80004003
Const E_UNEXPECTED = &H8000FFFF
Const S_OK = &H0
Select Case lErrNum
Case E_ABORT
fnOLEError = " Aborted"
Case E_ACCESSDENIED
fnOLEError = " Access Denied"
Case E_FAIL
fnOLEError = " General Failure"
Case E_HANDLE
fnOLEError = " Bad/Missing Handle"
Case E_INVALIDARG
fnOLEError = " Invalid Argument"
Case E_NOINTERFACE
fnOLEError = " No Interface"
Case E_NOTIMPL
fnOLEError = " Not Implemented"
Case E_OUTOFMEMORY
fnOLEError = " Out of Memory"
Case E_POINTER
fnOLEError = " Invalid Pointer"
Case E_UNEXPECTED
fnOLEError = " Unknown Error"
Case S_OK
fnOLEError = " Success!"
End Select
End Function
' Routine : SaveClip2Bit
' Purpose : Saves Picture object to desired location.
' Arguments : Path to save the file
Public Sub SaveClip2Bit(savePath As String)
On Error GoTo errHandler:
AltPrintScreen
Pause (2)
SavePicture PastePicture, savePath
errExit:
Exit Sub
errHandler:
Debug.Print "Save Picture: (" & Err.Number & ") - " & Err.Description
Resume errExit
End Sub
' Routine : Pause
' Purpose : Gives a short intreval for proper image capture.
' Arguments : Seconds to wait.
Public Function Pause(NumberOfSeconds As Variant)
On Error GoTo Err_Pause
Dim PauseTime As Variant, start As Variant
PauseTime = NumberOfSeconds
start = Timer
Do While Timer < start + PauseTime
DoEvents
Loop
Exit_Pause:
Exit Function
Err_Pause:
MsgBox Err.Number & " - " & Err.Description, vbCritical, "Pause()"
Resume Exit_Pause
End Function
I've created a solution that does what I want. I've used four Modules I found on various sites and then created a bit of VBA to pull the tasks together in the background.
Module 1 is the one I posted in the question above. I bypass the
AltPrintScreen part of it so it doesn't overwrite the image already in the
clipboard.
Module 2 Converts the BMP created by Modules 1 into a JPG.
Module 3 Scans a folder and populates an unbound listbox with a list
of the files found there.
Module 4 is used to make the filepaths in the listbox act as
hyperlinks to open the images on clicking.
I then created a button on a form that calls the first two Module to create a jpg from the image in the clipboard as follows...
Private Sub Command12_Click()
Dim Foldername As String
Dim FileRoot As String
Dim FilePathBMP As String
Dim FilePathJPG As String
Dim FilePathJPG2 As String
' Fist check to see if a unique folder for the open record exists, create if not
Foldername = CurrentProject.Path & "\xrays\" & Format(record_id.Value)
If Len(Dir(Foldername, vbDirectory)) = 0 Then
MkDir CurrentProject.Path & "\xrays\" & Format(record_id.Value)
End If
On Error GoTo reportErr
'The filename will begin with a date\time stamp then pull text from drop down lists the user can pick. This first creates a base filename without filtype extensions.
FileRoot = CurrentProject.Path & "\xrays\" & Format(record_id.Value) & "\" & Format(Now, "yyyymmddhhmmss") & "_" & Format(Combo14.Value) & "_" & Format(Combo21.Value)
'Creates a BMP and JPG version of the filename
FilePathBMP = FileRoot & ".bmp"
FilePathJPG = FileRoot & ".jpg"
'Save a BMP
SaveClip2Bit FilePathBMP
'Convert to JPG
WIA_ConvertImage FilePathBMP, FilePathJPG, JPEG, 85
'Delete the BMP
Kill (FilePathBMP)
Exit Sub
reportErr:
MsgBox "No image in Clipboard"
Resume Next
End Sub
On the form that will display the file list I've placed an unbound listbox "FileList". The following code calls on Module 3 and is in the OnLoad event of the form...
Call ListFiles(CurrentProject.Path & "\xrays\" & Format(record_id.Value), , , Me.FileList)
The following code is used to refresh the listbox "FileList". It can be put on an OnClick event of a button or added to the end of the above code to automatically refresh after adding a new image
Me.FileList.RowSource = ""
Call ListFiles(CurrentProject.Path & "\xrays\" & Format(record_id.Value), , , Me.FileList)
Me.FileList.Requery
Finally I put the following code in the OnDblClick event of the listbox, which calls Module 4...
Dim sPath As String
Dim sFile As String
sPath = FileList.Column(0)
' The line below extracts the filename from the full path (everything to the right of the last /)
' sFile = Right(sPath, Len(sPath) - InStrRev(sPath, "\"))
GoHyperlink (sPath)
Module 2...
Public Enum wiaFormat
BMP = 0
GIF = 1
JPEG = 2
PNG = 3
TIFF = 4
End Enum
'---------------------------------------------------------------------------------------
' Procedure : WIA_ConvertImage
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Convert an image's format using WIA
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Windows Image Acquisition (WIA)
' https://msdn.microsoft.com/en-us/library/windows/desktop/ms630368(v=vs.85).aspx
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sInitialImage : Fully qualified path and filename of the original image to resize
' sOutputImage : Fully qualified path and filename of where to save the new image
' lFormat : Format to convert the image into
' lQuality : Quality level to be used for the conversion process (1-100)
'
' Usage:
' ~~~~~~
' Call WIA_ConvertImage("C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg", _
' "C:\Users\MyUser\Desktop\Chrysanthemum_2.jpg", _
' JPEG)
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2017-01-18 Initial Release
' 2 2018-09-20 Updated Copyright
'---------------------------------------------------------------------------------------
Public Function WIA_ConvertImage(sInitialImage As String, _
sOutputImage As String, _
lFormat As wiaFormat, _
Optional lQuality As Long = 85) As Boolean
On Error GoTo Error_Handler
Dim oWIA As Object 'WIA.ImageFile
Dim oIP As Object 'ImageProcess
Dim sFormatID As String
Dim sExt As String
'Convert our Enum over to the proper value used by WIA
Select Case lFormat
Case 0
sFormatID = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"
sExt = "BMP"
Case 1
sFormatID = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}"
sExt = "GIF"
Case 2
sFormatID = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
sExt = "JPEG"
Case 3
sFormatID = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
sExt = "PNG"
Case 4
sFormatID = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}"
sExt = "TIFF"
End Select
If lQuality > 100 Then lQuality = 100
'Should check if the output file already exists and if so,
'prompt the user to overwrite it or not
Set oWIA = CreateObject("WIA.ImageFile")
Set oIP = CreateObject("WIA.ImageProcess")
oIP.Filters.Add oIP.FilterInfos("Convert").FilterID
oIP.Filters(1).Properties("FormatID") = sFormatID
oIP.Filters(1).Properties("Quality") = lQuality
oWIA.LoadFile sInitialImage
Set oWIA = oIP.Apply(oWIA)
'Overide the specified ext with the appropriate one for the choosen format
oWIA.SaveFile Left(sOutputImage, InStrRev(sOutputImage, ".")) & LCase(sExt)
WIA_ConvertImage = True
Error_Handler_Exit:
On Error Resume Next
If Not oIP Is Nothing Then Set oIP = Nothing
If Not oWIA Is Nothing Then Set oWIA = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: WIA_ConvertImage" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
Module 3...
Option Compare Database
Public Function ListFiles(strPath As String, Optional strFileSpec As String, _
Optional bIncludeSubfolders As Boolean, Optional lst As ListBox)
On Error GoTo Err_Handler
'Purpose: List the files in the path.
'Arguments: strPath = the path to search.
' strFileSpec = "*.*" unless you specify differently.
' bIncludeSubfolders: If True, returns results from subdirectories of strPath as well.
' lst: if you pass in a list box, items are added to it. If not, files are listed to immediate window.
' The list box must have its Row Source Type property set to Value List.
'Method: FilDir() adds items to a collection, calling itself recursively for subfolders.
Dim colDirList As New Collection
Dim varItem As Variant
Call FillDir(colDirList, strPath, strFileSpec, bIncludeSubfolders)
'Add the files to a list box if one was passed in. Otherwise list to the Immediate Window.
If lst Is Nothing Then
For Each varItem In colDirList
Debug.Print varItem
Next
Else
For Each varItem In colDirList
lst.AddItem varItem
Next
End If
Exit_Handler:
Exit Function
Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_Handler
End Function
Private Function FillDir(colDirList As Collection, ByVal strFolder As String, strFileSpec As String, _
bIncludeSubfolders As Boolean)
'Build up a list of files, and then add add to this list, any additional folders
Dim strTemp As String
Dim colFolders As New Collection
Dim vFolderName As Variant
'Add the files to the folder.
strFolder = TrailingSlash(strFolder)
strTemp = Dir(strFolder & strFileSpec)
Do While strTemp <> vbNullString
colDirList.Add strFolder & strTemp
strTemp = Dir
Loop
If bIncludeSubfolders Then
'Build collection of additional subfolders.
strTemp = Dir(strFolder, vbDirectory)
Do While strTemp <> vbNullString
If (strTemp <> ".") And (strTemp <> "..") Then
If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0& Then
colFolders.Add strTemp
End If
End If
strTemp = Dir
Loop
'Call function recursively for each subfolder.
For Each vFolderName In colFolders
Call FillDir(colDirList, strFolder & TrailingSlash(vFolderName), strFileSpec, True)
Next vFolderName
End If
End Function
Public Function TrailingSlash(varIn As Variant) As String
If Len(varIn) > 0& Then
If Right(varIn, 1&) = "\" Then
TrailingSlash = varIn
Else
TrailingSlash = varIn & "\"
End If
End If
End Function
Module 4...
Option Compare Database
Option Explicit
'Purpose: Avoid warning and error messages when opening files with FollowHyperlink
'Author: Allen Browne (allen#allenbrowne.com)
'Release: 28 January 2008
'Usage: To open MyFile.doc in Word, use:
' GoHyperlink "MyFile.doc"
' instead of:
' FollowHyperlink "MyFile.doc"
'Rationale:
'FollowHyperlink has several problems:
' a) It errors if a file name contains characters such as #, %, or &.
' b) It can give unwanted warnings, e.g. on a fileame with "file:///" prefix.
' c) It yields errors if the link did not open.
'This replacement:
' a) escapes the problem characters
' b) prepends the prefix
' c) returns True if the link opened (with an optional error message if you care.)
'Limitations:
' - If a file name contains two # characters, it is treated as a hyperlink.
' - If a file name contains % followed by 2 hex digits, it assumes it is pre-escaped.
' - File name must include path.
'Documentation: http://allenbrowne.com/func-GoHyperlink.html
Public Function GoHyperlink(FullFilenameOrLink As Variant) As Boolean
On Error GoTo Err_Handler
'Purpose: Replacement for FollowHyperlink.
'Return: True if the hyperlink opened.
'Argument: varIn = the link to open
Dim strLink As String
Dim strErrMsg As String
'Skip error, null, or zero-length string.
If Not IsError(FullFilenameOrLink) Then
If FullFilenameOrLink <> vbNullString Then
strLink = PrepHyperlink(FullFilenameOrLink, strErrMsg)
If strLink <> vbNullString Then
FollowHyperlink strLink
'Return True if we got here without error.
GoHyperlink = True
End If
'Display any error message from preparing the link.
If strErrMsg <> vbNullString Then
MsgBox strErrMsg, vbExclamation, "PrepHyperlink()"
End If
End If
End If
Exit_Handler:
Exit Function
Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "GoHyperlink()"
Resume Exit_Handler
End Function
Public Function PrepHyperlink(varIn As Variant, Optional strErrMsg As String) As Variant
On Error GoTo Err_Handler
'Purpose: Avoid errors and warnings when opening hyperlinks.
'Return: The massaged link/file name.
'Arguments: varIn = the link/file name to massage.
' strErrMsg = string to append error messages to.
'Note: Called by GoHyperlink() above.
' Can also be called directly, to prepare hyperlinks.
Dim strAddress As String 'File name or address
Dim strDisplay As String 'Display part of hyperlink (if provided)
Dim strTail As String 'Any remainding part of hyperlink after address
Dim lngPos1 As Long 'Position of character in string (and next)
Dim lngPos2 As Long
Dim bIsHyperlink As Boolean 'Flag if input is a hyperlink (not just a file name.)
Const strcDelimiter = "#" 'Delimiter character within hyperlinks.
Const strcEscChar = "%" 'Escape character for hyperlinks.
Const strcPrefix As String = "file:///" 'Hyperlink type if not supplied.
If Not IsError(varIn) Then
strAddress = Nz(varIn, vbNullString)
End If
If strAddress <> vbNullString Then
'Treat as a hyperlink if there are two or more # characters (other than together, or at the end.)
lngPos1 = InStr(strAddress, strcDelimiter)
If (lngPos1 > 0&) And (lngPos1 < Len(strAddress) - 2&) Then
lngPos2 = InStr(lngPos1 + 1&, strAddress, strcDelimiter)
End If
If lngPos2 > lngPos1 + 1& Then
bIsHyperlink = True
strTail = Mid$(strAddress, lngPos2 + 1&)
strDisplay = Left$(strAddress, lngPos1 - 1&)
strAddress = Mid$(strAddress, lngPos1 + 1&, lngPos2 - lngPos1)
End If
'Replace any % that is not immediately followed by 2 hex digits (in both display and address.)
strAddress = EscChar(strAddress, strcEscChar)
strDisplay = EscChar(strDisplay, strcEscChar)
'Replace special characters with percent sign and hex value (address only.)
strAddress = EscHex(strAddress, strcEscChar, "&", """", " ", "#", "<", ">", "|", "*", "?")
'Replace backslash with forward slash (address only.)
strAddress = Replace(strAddress, "\", "/")
'Add prefix if address doesn't have one.
If Not ((varIn Like "*://*") Or (varIn Like "mailto:*")) Then
strAddress = strcPrefix & strAddress
End If
End If
'Assign return value.
If strAddress <> vbNullString Then
If bIsHyperlink Then
PrepHyperlink = strDisplay & strcDelimiter & strAddress & strcDelimiter & strTail
Else
PrepHyperlink = strAddress
End If
Else
PrepHyperlink = Null
End If
Exit_Handler:
Exit Function
Err_Handler:
strErrMsg = strErrMsg & "Error " & Err.Number & ": " & Err.Description & vbCrLf
Resume Exit_Handler
End Function
Private Function EscChar(ByVal strIn As String, strEscChar As String) As String
'Purpose: If the escape character is found in the string,
' escape it (unless it is followed by 2 hex digits.)
'Return: Fixed up string.
'Arguments: strIn = the string to fix up
' strEscChar = the single character used for escape sequqnces. (% for hyperlinks.)
Dim strOut As String 'output string.
Dim strChar As String 'character being considered.
Dim strTestHex As String '4-character string of the form &HFF.
Dim lngLen As Long 'Length of input string.
Dim i As Long 'Loop controller
Dim bReplace As Boolean 'Flag to replace character.
lngLen = Len(strIn)
If (lngLen > 0&) And (Len(strEscChar) = 1&) Then
For i = 1& To lngLen
bReplace = False
strChar = Mid(strIn, i, 1&)
If strChar = strEscChar Then
strTestHex = "&H" & Mid(strIn, i + 1&, 2&)
If Len(strTestHex) = 4& Then
If Not IsNumeric(strTestHex) Then
bReplace = True
End If
End If
End If
If bReplace Then
strOut = strOut & strEscChar & Hex(Asc(strEscChar))
Else
strOut = strOut & strChar
End If
Next
End If
If strOut <> vbNullString Then
EscChar = strOut
ElseIf lngLen > 0& Then
EscChar = strIn
End If
End Function
Private Function EscHex(ByVal strIn As String, strEscChar As String, ParamArray varChars()) As String
'Purpose: Replace any characters from the array with the escape character and their hex value.
'Return: Fixed up string.
'Arguments: strIn = string to fix up.
' strEscChar = the single character used for escape sequqnces. (% for hyperlinks.)
' varChars() = an array of single-character strings to replace.
Dim i As Long 'Loop controller
If (strIn <> vbNullString) And IsArray(varChars) Then
For i = LBound(varChars) To UBound(varChars)
strIn = Replace(strIn, varChars(i), strEscChar & Hex(Asc(varChars(i))))
Next
End If
EscHex = strIn
End Function
I hope all that helps others!

Find task in MS Project by custom field value

i'm currently using below function to find a MS project task based on custom field value, it works perfectly when a parent task object is provided, it only loops through children tasks. The problem is that now in plan target task could be placed any where in plan and it takes some time looping through all the ~3k tasks in plan trying to find the correct task based on custom field value. Is there a way i could do this faster?
**'sub to test Function**
Public Sub TestFunction()
Dim TaskObject as Object
Dim MSapp as Object '<- MS project application
Dim ErrMsg as String
set TaskObject = funGetTaskByFieldRef(objMSPapp:=MSapp,
fieldValue:="Key1234",
fieldName:= "customForeingKeyField",
ErrMsg:= ErrMsg)
if TaskObject is nothing then
MsgBox ErrMsg
else
Debug.Print TaskObject.UniqueID & " - " & TaskObject.Name
end if
End Sub
**'Function - - -**
Public Function funGetTaskByFieldRef(ByRef objMSPapp As Object, ByVal fieldValue As String, _
ByVal fieldName As String, _
Optional ByRef objParentTask As Object, _
Optional ByRef ErrMsg As String = vbNullString) As Object
'<VARIABLES>
Dim obMSPprj As Object
Dim tsk As Object
Dim tmpValue As String
'</VARIABLES>
'<FUN> ---
'set temporal var Microsoft Project
Set obMSPprj = objMSPapp.ActiveProject
'using project
With obMSPprj
'check if parent task has been provided
If Not objParentTask Is Nothing Then
'loop through each child
For Each tsk In objParentTask.OutlineChildren
tmpValue = funSetGetMSPval(objMSPapp, tsk, 0, "Get", fieldName)
If tmpValue = fieldValue Then
'retunr UID
Set funGetTaskByFieldRef = tsk
'exit function
Exit Function
End If
Next tsk
Else
'loop through each task
For Each tsk In .Tasks
tmpValue = funSetGetMSPval(objMSPapp, tsk, 0, "Get", fieldName)
If tmpValue = fieldValue Then
'retunr UID
Set funGetTaskByFieldRef = tsk
'exit function
Exit Function
End If
Next tsk
End If
End With
'if there is no exact match for task name return -1
Set funGetTaskByFieldRef = Nothing
ErrMsg = "Task not found"
'<FUN> ---
End Function
**'Encapsulated sub-function **
Public Function funSetGetMSPval(ByRef objMSPapp As Object, ByRef objEntObj As Object, _
ByVal intPjFieldType As Integer, ByVal strAction As String, _
Optional ByVal strFldName As String, _
Optional ByVal strVal As String) As Variant
'pjProject = 2
'pjResource =1
'pjTask = 0
With objMSPapp
Select Case strAction
Case "Set"
On Error Resume Next
objEntObj.SetField .FieldNameToFieldConstant(strFldName, intPjFieldType), strVal
If Not Err.Number <> 0 Then
'catch error
End If
On Error GoTo 0
funSetGetMSPval = True
Case "Get"
funSetGetMSPval = objEntObj.getfield(.FieldNameToFieldConstant(strFldName, intPjFieldType))
End Select
End With
End Function
Hope there is someone that has a better way to do this.
thank you.
regards.
Is there a way i could do this faster?
Yes, use the Find method.
Public Sub FindTask()
Dim TaskObject As Object
Dim MSapp As Object
Set MSapp = Application
Dim found As Boolean
found = MSapp.Find(Field:="customForeingKeyField", Test:="equals", Value:="Key1234")
If found Then
Set TaskObject = MSapp.ActiveCell.Task
Debug.Print TaskObject.UniqueID & " - " & TaskObject.Name
Else
MsgBox "Task not found"
End If
End Sub

Writing a string to a new .csv in VB.net

I am trying to write a string to a .csv file, but unable to get it to display.
I have managed to do it in VBA, but when writing in VB.net it's not working.
I first create the file and set the headers for each column. After this I am getting information on each required attribute and writing it to a string s.
All i want to do now is write the string to the .csv file so that each attribute is in the right column under the right header.
Each time the string s simply needs to be on a new row.
This is what I have so far (I have cut out some bits of code so some syntax may look incorrect). What am i doing wrong or missing?
Sub Main()
Dim sOutput As String
' Create a header for the output file
sOutput = ("Level,Occurrence Name,Reference Name,Object type, Visibility, Path" & vbLf)
If Occs.Count > 0 Then
For i = 1 To Occs.Count
iLevel = 0
curOcc = Occs.Item(i)
GetOccurrenceData(curOcc, sOutput, oSel, False, iLevel)
Next
End If
' Write the output string to a file
Dim sPath As String
Dim bWrite As Boolean
sPath = ("C:\temp\data3.csv")
bWrite = WriteFile(sPath, sOutput)
End Sub
Sub GetOccurrenceData(curOcc As VPMOccurrence, s As String, sel As Selection, ByVal bParentHidden As Boolean, ByVal iParentLevel As Integer)
'CODE TO GET DATA REMOVED AS IRRELEVANT
' Append the output string with the data for the current occurrence.
s = (s & curLevel & "," & sName & "," & sRefName & "," & sType & "," & sVisibility & vbLf)
' Repeat this data gathering procedure on any children the current occurrence may have.
Occs = curOcc.Occurrences
If Occs.Count > 0 Then
For i = 1 To Occs.Count
GetOccurrenceData(Occs.Item(i), s, sel, bChildrenInheritNoShow, curLevel)
Next
End If
In GetOccurrenceData you pass in a string and change it in the method, but you did not pass it in as a ByRef so anything done to the string in the method stays in the method.
Change the header of your method to read
Sub GetOccurrenceData(curOcc As VPMOccurrence,ByRef s As String, sel As Selection, ByVal bParentHidden As Boolean, ByVal iParentLevel As Integer)
I would however recommend using a StringBuilder to accomplish what you are doing.
Like This:
Sub Main()
Dim sb As New Text.StringBuilder()
sb.AppendLine("Level,Occurrence Name,Reference Name,Object type, Visibility, Path")
If Occs.Count > 0 Then
For i = 1 To Occs.Count
iLevel = 0
curOcc = Occs.Item(i)
GetOccurrenceData(curOcc, sb, oSel, False, iLevel)
Next
End If
' Write the output string to a file
Dim sPath As String
Dim bWrite As Boolean
sPath = ("C:\temp\data3.csv")
bWrite = WriteFile(sPath, sb.ToString())
End Sub
Sub GetOccurrenceData(curOcc As VPMOccurrence, sb As Text.StringBuilder, sel As Selection, ByVal bParentHidden As Boolean, ByVal iParentLevel As Integer)
'CODE TO GET DATA REMOVED AS IRRELEVANT
' Append the output string with the data for the current occurrence.
sb.Append(curLevel).Append(",").Append(sName).Append(",").Append(sRefName).Append(",").Append(sType).Append(",").AppendLine(sVisibility)
' Repeat this data gathering procedure on any children the current occurrence may have.
Occs = curOcc.Occurrences
If Occs.Count > 0 Then
For i = 1 To Occs.Count
GetOccurrenceData(Occs.Item(i), sb, sel, bChildrenInheritNoShow, curLevel)
Next
End If
End Sub

Unable to iterate VBA Collection

I am unable to iterate completely through an excel VBA Collection of custom objects. I receive an Invalid procedure call or argument error on the third object.
When examining each object and stepping line-by-line through the procedure, it works on the first two objects, then fails on the third. I've tried executing the code alone, calling the third object instance manually, with no error.
Why would the procedure execute the calculation and set the first two object instance's property correctly, then fail the third time?
I'm new to VBA - what obvious thing am I missing?
The Procedure:
Sub calculateMaintenancePlan()
'Calculates next x maintenance cycles for a number aircraft
'Assumes the spreadsheet defines a named range containing the following columns:
'1 2 3 4 5 6
'Tail Number Current Hours Next Cycle Next Cycle DUE In Heavy Week Id Cycle Start
'6004 11265.0 4 11333.7 FALSE [Integer or -10 if not in heavy]
'debugging/utility variables
Dim temp As Variant
Dim i As Integer, j As Integer
'Assumes the spreadsheet defines a named range containing the following columns:
'Cycle ID Cycle Type Duration Text ID
'1 200 hour 1 HMT1
ReDim mx_cycles(1 To Worksheets("Matrix Inputs").Range("maintenance_cycles").Rows.Count, 1 To 3)
For i = 1 To Worksheets("Matrix Inputs").Range("maintenance_cycles").Rows.Count
mx_cycles(i, 1) = Worksheets("Matrix Inputs").Range("maintenance_cycles").Cells(i, 2)
mx_cycles(i, 2) = CInt(Worksheets("Matrix Inputs").Range("maintenance_cycles").Cells(i, 3))
mx_cycles(i, 3) = Worksheets("Matrix Inputs").Range("maintenance_cycles").Cells(i, 4)
'Debug.Print i & " : " & mx_cycles(i, 1) & " : " & mx_cycles(i, 2) & " : " & mx_cycles(i, 3)
Next i
'an array containing each aircraft tail number, assume spreadsheet
'contains named range called "aircraft"
Dim aircraft As Collection, s_Aircraft As Collection
Dim acft As c_Aircraft
Set aircraft = New Collection
Set s_Aircraft = New Collection
'Set the Collection size to the total number of aircraft on station
'and create a c_Aircraft instance representing each airframe
For i = 1 To Range("aircraft").Count
Set acft = New c_Aircraft
acft.init_aircraft Worksheets("Matrix Inputs").Range("inputs"), i, mx_cycles
aircraft.Add acft, CStr(acft.tailNumber)
Next i
'Sort the aircraft
Set s_Aircraft = sortedAircraft(aircraft)
End Sub
'Sort a Collection of c_aircraft objects
Private Function sortedAircraft(unsortedAircraft As Collection) As Collection
Set sortedAircraft = New Collection
Dim acft As c_Aircraft
Dim temp_acft As c_Aircraft
Dim i As Long, j As Long
Dim next_acft_cycle_start_week_id As Integer
Dim previous_acft As String
Dim t_tailNum
'copy the Collection to a new collection
For Each acft In unsortedAircraft
sortedAircraft.Add acft
Next acft
'Sort the aircraft
For i = 1 To sortedAircraft.Count
For j = i + 1 To sortedAircraft.Count
If sortedAircraft.Item(i).hoursToDUE > sortedAircraft.Item(j).hoursToDUE Then
Set temp_acft = sortedAircraft.Item(j)
sortedAircraft.Remove j
t_tailNum = CStr(temp_acft.tailNumber)
sortedAircraft.Add temp_acft, t_tailNum, i
End If
Next j
Next i
previous_acft = CStr(sortedAircraft.Item(1).tailNumber)
For Each acft In sortedAircraft
If acft.inHeavy = False Then
'******* FAILS here on the third item of six items in the collection
Debug.Print sortedAircraft.Item(previous_acft).weekIdCycleStart + sortedAircraft.Item(previous_acft).nextCycleDuration + 1
previous_acft = CStr(acft.tailNumber)
Else
previous_acft = CStr(acft.tailNumber)
End If
Next acft
End Function
The Object: Represents an aircraft with various properties
Option Explicit
'Class c_Aircraft
'Requires call to init_aircraft() with the Range and "row" of the aircraft instance to be created.
'Assumes the Range contains the following columns:
'1 2 3 4 5 6
'Tail Number Current Hours Next Cycle Next Cycle DUE In Heavy Week Id Cycle Start
'6004 11265.0 4 11333.7 FALSE 0
'Attributes
Private p_tailNumber As Long
Private p_tailNumStr As String
Private p_initialAircraftHours As Double
Private p_currentAircraftHours As Double
Private p_initialNextCycleType As Integer
Private p_nextCycleType As Integer
Private p_initialNextCycleDuration As Integer
Private p_nextCycleDuration As Integer
Private p_initialHoursNextCycleDue As Double
Private p_hoursNextCycleDue As Double
Private p_initialInHeavy As Boolean
Private p_inHeavy As Boolean
'An integer representing the Week Id the current cycle
'started, if in heavy maintenance
Private p_initialWeekIdCycleStart As Integer
Private p_weekIdCycleStart As Integer
Private p_initialHoursToDUE As Double
Private p_hoursToDUE As Double
Private p_initialHoursToDNE As Double
Private p_hoursToDNE As Double
'General Methods
'Custom Initialize
Public Sub init_aircraft(data_range As Range, asset_number As Integer, mxCycles() As Variant)
p_tailNumber = data_range(asset_number, 1)
p_tailNumStr = CStr(data_range(asset_number, 1))
p_initialAircraftHours = data_range(asset_number, 2)
p_currentAircraftHours = p_initialAircraftHours
p_initialNextCycleType = data_range(asset_number, 3)
p_nextCycleType = p_initialNextCycleType
p_initialNextCycleDuration = mxCycles(p_nextCycleType, 2)
p_nextCycleDuration = p_initialNextCycleDuration
p_initialHoursNextCycleDue = data_range(asset_number, 4)
p_hoursNextCycleDue = p_initialHoursNextCycleDue
p_initialInHeavy = data_range(asset_number, 5)
p_inHeavy = p_initialInHeavy
If p_inHeavy Then
p_initialWeekIdCycleStart = data_range(asset_number, 6)
p_weekIdCycleStart = p_initialWeekIdCycleStart
Else
'set to a week prior more than the longest cycle duration
p_initialWeekIdCycleStart = -10
p_weekIdCycleStart = -10
End If
p_initialHoursToDUE = Round(p_hoursNextCycleDue - p_currentAircraftHours, 1)
p_hoursToDUE = p_initialHoursToDUE
p_initialHoursToDNE = Round(p_hoursNextCycleDue - p_currentAircraftHours + 15, 1)
p_hoursToDNE = p_initialHoursToDNE
End Sub
'Return the aircraft objects properties as String
Public Function print_aircraft() As String
print_aircraft = p_tailNumber & vbCrLf & _
"Current Hours: " & p_currentAircraftHours & vbCrLf & _
"Next Cycle: " & p_nextCycleType & vbCrLf & _
"Next Cycle Duration: " & p_nextCycleDuration & vbCrLf & _
"Hours Next Cycle Due: " & p_hoursNextCycleDue & vbCrLf & _
"In Heavy: " & p_inHeavy & vbCrLf & _
"Week Id Cycle Start: " & p_weekIdCycleStart & vbCrLf & _
"DUE: " & p_hoursToDUE & vbCrLf & _
"DNE: " & p_hoursToDNE
End Function
'Get/Let Methods
' Hours Remaining to the DNE
Public Property Get hoursToDNE() As Double
hoursToDNE = p_hoursToDNE
End Property
Public Property Let hoursToDNE(HoursDNE As Double)
p_hoursToDNE = HoursDNE
End Property
'Hours Remaining to the DUE
Public Property Get hoursToDUE() As Double
hoursToDUE = p_hoursToDUE
End Property
Public Property Let hoursToDUE(HoursDUE As Double)
p_hoursToDUE = HoursDUE
End Property
' Aircraft in Heavy Property
Public Property Get inHeavy() As Boolean
inHeavy = p_inHeavy
End Property
Public Sub setInHeavy(Value As Boolean, weekIdCycleStarted As Integer)
p_inHeavy = Value
p_weekIdCycleStart = weekIdCycleStarted
End Sub
'p_weekIdCycleStart
Public Property Get weekIdCycleStart() As Integer
weekIdCycleStart = p_weekIdCycleStart
End Property
'p_weekIdCycleStart
Public Property Let weekIdCycleStart(weekId As Integer)
p_weekIdCycleStart = weekId
End Property
' Aircraft Hours at Next Maintenance Cycle Due Property
Public Property Get hoursNextCycleDue() As Double
hoursNextCycleDue = p_hoursNextCycleDue
End Property
Public Property Let hoursNextCycleDue(Value As Double)
p_hoursNextCycleDue = Value
End Property
' Next Maintenance Cycle Due Property
Public Property Get nextCycleType() As Integer
nextCycleType = p_nextCycleType
End Property
Public Property Let nextCycleType(cycleType As Integer)
p_nextCycleType = cycleType
End Property
' Next Maintenance Cycle Duration Property
Public Property Get nextCycleDuration() As Integer
nextCycleDuration = p_nextCycleDuration
End Property
Public Property Let nextCycleDuration(cycleDuration As Integer)
p_nextCycleDuration = cycleDuration
End Property
' Current Aircraft Hours Property
Public Property Get currentAircraftHours() As Double
currentAircraftHours = p_currentAircraftHours
End Property
Public Property Let currentAircraftHours(Value As Double)
p_currentAircraftHours = Value
End Property
' Tail Number Property
Public Property Get tailNumber() As Long
tailNumber = p_tailNumber
End Property
Public Property Let tailNumber(Value As Long)
p_tailNumber = Value
End Property