Useful functions and code fragments


Remove x% characters from the right side of a$
Function ChopRight(sOrigString, nLength)
  ChopRight = Left(sOrigString, Len(sOrigString) - nLength)
End Function


Remove x% characters from the left side of a$
Function ChopLeft(sOrigString, nLength)
  ChopLeft = Mid(sOrigString, nLength + 1)
End Function
Another way of doing the same thing:
Function ChopLeft(sOrigString, nLength)
  ChopLeft = Right(sOrigString, Len(sOrigString) - nLength)
End Function


Ensure that the first letter in a$ is uppercase
Function Capitalize(sOrigString)
  If Asc(Left(sOrigString, 1))>=97 And Asc(Left(sOrigString, 1))<=122 Then
    Mid(sOrigString, 1, 1) = UCase(Left(sOrigString, 1))
  End If
  Capitalize = sOrigString
End Function
Another way of doing the same thing:
Function Capitalize(sOrigString)
  If Left(sOrigString, 1) Like "[a-z]" Then
    sOrigString = UCase(Left(sOrigString, 1)) & Mid(sOrigString, 2)
  End If
  Capitalize = sOrigString
End Function


Split sOrigString into sFirstPart & sSecondPart after character nSplitPoint. (sFirstPart will contain everything from the first character up to and including the character at the split point, and sSecondPart will contain everything from the character after the split point to the end of sOrigString.)
Sub Split(sOrigString, nSplitPoint, sFirstPart, sSecondPart)
  sFirstPart = Left(sOrigString, nSplitPoint)
  sSecondPart = Mid(sOrigString, nSplitPoint + 1) 'could also use: Right(sOrigString, Len(sOrigString) - nSplitPoint)
End Sub


Check whether the active window contains a bibliographic record
Function IsBibRecord(CS as Object) As Integer
  Select Case CS.ItemType
    Case 0, 1, 17
      IsBibRecord = TRUE
    Case Else
      IsBibRecord = FALSE
  End Select
End Function


Get the next subfield after the cursor for the current field
Function GetNextSubfield(CS as Object)
   nCurRow = CS.CursorRow
   nCurCol = CS.CursorColumn
   retval = CS.GetFieldLine(nCurRow, sField)
   If InStr(nCurCol + 1, sField, Chr(223)) > 1 Then
      nStart = InStr(nCurCol + 1, sField, Chr(223))
   Else
      nStart = Len(sField) + 1
   End If
   If InStr(nStart + 1, sField, Chr(223)) > 1 Then
      nEnd = InStr(nStart + 1, sField, Chr(223)) - 1
   Else
      nEnd = Len(sField)
      If nEnd < 6 Then nEnd = 6
   End If
   GetNextSubfield = Mid(sField, nStart, nEnd - nStart + 1)
End Function


Edit all occurrences of a specific tag
  bool = TRUE : cc% = 0 : TagNum% = 0
  Do Until bool = FALSE
    cc% = cc% + 1
    If cc > 999 Then
      MsgBox "Error: macro linecounter is out of control. Exiting..."
      Goto Done
    End If
    bool = CS.GetFieldLine( cc%,indata$ )
    If Left(indata$, 3) = "246" Then 
      TagNum% = cc%
      '*** Put code that looks at or alters the record here.
      'Use TagNum% to indicate the line number in CS.GetFieldLine/SetFieldLine/DeleteFieldLine commands
    ElseIf Val(Left(indata$, 3)) > 246 Then
      Exit Do
    End If
  Loop


Iterate through all records in a list
bool = CS.GetFirstItem

Do

'*** Put code that looks at or alters each record here ***

Loop While CS.GetNextItem <> FALSE

bool = CS.CloseRecord(TRUE)


Iterate through only the selected records in a list
bool = CS.GetFirstSelectedItem

Do

'*** Put code that looks at or alters each record here ***

Loop While CS.GetNextSelectedItem <> FALSE

bool = CS.CloseRecord(TRUE)


Iterate through all records in the online save file
 Do 
   bool = CS.GetFirstItem
   Do

   '*** Put code that looks at or alters each record here ***
     
   Loop While CS.GetNextItem <> FALSE

   bool = CS.CloseRecord(TRUE)

 Loop While CS.GetNext100Records <> FALSE


Return to the index page.