Manage Learn to apply best practices and optimize your operations.

Automatically reattach after modification of an attachment - Part I

This script gives your databases the functionality to detach / edit / attach an attachment with one single click. Part I

This is Part I of script that gives your databases the functionality to detach // edit // attach an attachment with one single click.
Here is a downloadable sample database.

Option Public


2 new functions are added
   - Public Function RenameFile(SourceFile As String, DestFile As String, AFlag As Integer) As Long     
   - Private Function ReplaceSubstring(sSource As String, sFrom As String, sTo As String) As String     

Stuart Blake/TOWER/LondonStockEx@LondonStockEx reported the following:
    > I've tried this on a Word document with spaced in the filename, and
    > it doesn't like it.  if, for example, I try to launch a file called "Filename
    > With Spaces.doc", Word is activated correctly but then I get the message "Word
    > can not open the document: (C:TEMPFILENAME.DOC)".

Before opening the file with spaces in filename the file is now temporarily renamed and all spaces are replaced by "_" (underscore).
When reattaching, the file is renamed to its original filename


The "IsModified"-flag is alsways set to TRUE, even if there is no modification in the file itself.



The  FindExecutable - Function did not work under Win2000.
In case of DOC and XLS suffix, executables are now retrieved from the Windows registry

Added new method :

    Public Sub Modify ( AttName As String , InItem As String )   


Added New function "GetShortFileName"

We always use the short FileName when launching it with an application.
We do not need to rename the file before launching.
This also fixes the bug reported by Stuart Blake 19.08.2000.
The functions added on 19.08.2000 are removed.

Added registry search for "MSProject98"

No value was returned, when calling the FindExecutableByExtension-function with ".mpp" suffix.
We use registry settings instead.


PopMenu integrated in script library


Sub CreateFromTemplate added


There seem do be a problem with 4.5.x clients
When removing the attachment an error occurs "Object variable not set"
Bug reported by Donna Fischer

View ($UNID) is no longer needed


' C L A S S "Attachment"  (frontend)
' written by Heinz Ulrich Krause, Scholler Holding, 2000     
' copy to the declaration section of action button or script library
' Thanks to Stuart Blake for bug reports 

     S A M P L E:
Sub Click(Source As Button)
     WorkDir$ = "C:Temp"  ' The WorkDir
     Field$ = "Code"              ' RichText Field containing the attachment
     Dim A As New Attachment ( WorkDir$ )    ' Create a new instance of the class
     FileName$ = A.SelectAttachment ( Field$ ) ' Select an attachment
     If FileName$ = "" Then  
          Exit Sub
          Call A.DetachAndEdit( FileName$ , Field$ ) ' Detach the attachment in Field$ to        ' the workdir and open for edit in the
                               ' application associated with the file
          If  A.IsModified ( FileName$ ) Then    ' if the is any modification in the file
                               ' BEFORE removing the old attachment from the document, do your own stuff here....
               Call A.RemoveByName ( FileName$ , Field$ )  ' Delete the attachment from the doc
                              ' AFTER removing the old attachment from the document, do your own stuff here....
               Call A.Attach ( FileName$ , Field$ )                  ' and attach the file from the workdir
                              ' AFTER the new attachment is attached, do your own stuff here....
          End If
     End If
     Kill workdir$ & "" & FileName$  ' and KILL that cat
End Sub

' --------------------------------------------------------------
'  OR do it the simple ( all in one ) way
' --------------------------------------------------------------
Sub Click(Source As Button)
         WorkDir$ = "C:Temp1"
         Field$ = "Code"
         Dim A As New Attachment ( WorkDir$ )
         Call A.Modify ( Field$ )
End Sub

' Registry Reserved Key Handles
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005
Const HKEY_DYN_DATA = &H80000006
Const REG_SZ = 1           ' Unicode nul terminated string
Const REG_BINARY = 3  ' Free form binary
Const REG_DWORD = 4 ' 32-bit number
Const WINWORD = "SOFTWAREMicrosoftWindowsCurrentVersionApp PathsWinword.exe"
Const EXCEL = "SOFTWAREMicrosoftWindowsCurrentVersionApp PathsExcel.exe"
Const PROJEKT = "SOFTWAREMicrosoftWindowsCurrentVersionApp PathsWinproj.exe"
Const WIN_VERSION = "SOFTWAREMicrosoftWindows NTCurrentVersion"
Const WIN_VERSION_KEY = "ProductName"

Const MAX_PATH = 260

Const cMAXLEN_BUFFER = 255
Const INFINITE = -1&

	Left As Long
	Top As Long
	Right As Long
	Bottom As Long
End Type

	x As Long
	y As Long
End Type

Type MSG
	hwnd As Long
	message As Long
	wParam As Long
	lParam As Long
	time As Long
End Type

	dwLowDateTime As Long
	dwHighDateTime As Long
End Type

	dwFileAttributes As Long 
	ftCreationTime As FILETIME 
	ftLastAccessTime As FILETIME 
	ftLastWriteTime As FILETIME 
	nFileSizeHigh As Long 
	nFileSizeLow As Long 
	dwReserved0 As Long 
	dwReserved1 As Long 
	cFileName As String * MAX_PATH 
	cAlternate As String * 14 
End Type

	wYear As Integer
	wMonth As Integer
	wDayOfWeek As Integer
	wDay As Integer
	wHour As Integer
	wMinute As Integer
	wSecond As Integer
	wMilliseconds As Integer
End Type

	cb As Long
	lpReserved As String
	lpDesktop As String
	lpTitle As String  
	dwX As Long 
	dwY As Long
	dwXSize As Long
	dwYSize As Long
	dwXCountChars As Long
	dwYCountChars As Long  
	dwFillAttribute As Long
	dwFlags As Long
	wShowWindow As Integer
	cbReserved2 As Integer
	lpReserved2 As Long 
	hStdInput As Long
	hStdOutput As Long
	hStdError As Long
End  Type

	hProcess As Long
	hThread As Long
	dwProcessID As Long
	dwThreadID As Long
End Type

	hWnd As Long
	wFunc As Long
	pFrom As String
	pTo As String
	fFlags As Integer
	fAborted As Long
	hNameMaps As Long
	sProgress As String
End Type
'// API functions used for PopUp menue
Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long 
Declare Function CreatePopupMenu Lib "user32" Alias "CreatePopupMenu" () As Long
Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (Byval hMenu As Long, 
Byval wFlags As Long, Byval wIDNewItem As Integer, Byval lpNewItem As Any) As Long< Declare Function TrackPopupMenu Lib "user32" Alias "TrackPopupMenu" (Byval hMenu As
Long, Byval wFlags As Long, Byval x As Long, Byval y As Long,
Byval nReserved As Long, Byval hwnd As Long, lprc As Rect) As Long Declare Function DestroyMenu Lib "user32" Alias "DestroyMenu" (Byval hMenu As Long) As Long Declare Function GetMessage Lib "user32" Alias "GetMessageA" (lpMsg As MSG, Byval hwnd
As Long, Byval wMsgFilterMin As Long, Byval wMsgFilterMax
As Long) As Long Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow" () As Long '// API functions used in CLASSAttachment Declare Function FindExecutable Lib "shell32.dll" Alias
"FindExecutableA" (Byval lpFile As String, Byval lpDirectory As
String, Byval sResult As String) As Long Declare Function WaitForSingleObject Lib "kernel32" (Byval hHandle As
Long, Byval dwMilliseconds As Long) As Long 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 Declare Function CreateProcessA Lib "kernel32" (Byval lpApplicationName
As Long, Byval lpCommandLine As String, Byval lpProcessAttributes
As Long, Byval lpThreadAttributes As Long, Byval bInheritHandles As
Long, Byval dwCreationFlags As Long, _ Byval lpEnvironment As Long, Byval lpCurrentDirectory As Long,
lpStartupInfo As STARTUPINFO, lpProcessInformation As
PROCESS_INFORMATION) As Long Declare Function CloseHandle Lib "kernel32" (Byval hObject As Long) As Long Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As
FILETIME, lpLocalFileTime As FILETIME) As Long Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA"
(Byval lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA"
(Byval hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long Declare Function FindClose Lib "kernel32" (Byval hFindFile As Long) As Long Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As
FILETIME, lpSystemTime As SYSTEMTIME) As Long Declare Function SystemTimeToFileTime Lib "kernel32" (lpSystemTime As
SYSTEMTIME, lpFileTime As FILETIME) As Long Declare Function RegCloseKey Lib "advapi32.dll" (Byval hKey As Long) As Long Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA"
(Byval hKey As Long, Byval lpSubKey As String, phkResult As Long) As Long Declare Function RegQueryValueEx Lib "advapi32.dll" Alias
"RegQueryValueExA" (Byval hKey As Long, Byval lpValueName As
String, Byval lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long Declare Function GetShortPathName Lib "kernel32"
Alias"GetShortPathNameA" (Byval lpszLongPath As String,Byval
lpszShortPath As String, Byval cchBuffer As Long) As Long
End of part one.
Click here for Part II
Click here for Part III
This was last published in February 2002

Dig Deeper on LotusScript

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.




  • iSeries tutorials's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...