Notes Ninjas Notes Ninjas
Lotus Notes hints, tips, error messages
Detach edit and reattach updated word file in one button

Hint Short Cut: Add this to your code & documentation to help you find this page.
http://www.notestemplates.com/#DetachSaveReattach or http://www.notestemplates.com/A555F9/nn.nsf/ByAlias/DetachSaveReattach


Users are annoyed that they have to detach, edit and then reattach a file. This code will allow you to add a button to let them do it, in one click.





In the declarations section of a button put:

Private Type STARTUPINFO
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

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


Declare Function WaitForSingleObject Lib "kernel32" (Byval _
hHandle As Long, Byval dwMilliseconds 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

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&

Add a subroutine to the button:

Sub ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO

' Initialize the STARTUPINFO structure:
start.cb = Len(start)

' Start the shelled application:
ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

' Wait for the shelled application to finish:
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
ret& = CloseHandle(proc.hProcess)
End Sub

Put this in the click event:

' This button has loads of code in to make sure the edit process has finished
Dim uiw As New notesuiworkspace
Dim uidoc As notesuidocument
Set uidoc = uiw.currentdocument
Dim doc As notesdocument
Set doc = uidoc.document

If uidoc.isnewdoc Then
Msgbox "This utility doesn't work for new documents, save the document ( Ctrl S ), and try again."
End
End If

Set rtitem = doc.GetFirstItem( "Body" )
If ( rtitem.Type = RICHTEXT ) Then


Forall o In rtitem.EmbeddedObjects
If ( o.Type = EMBED_ATTACHMENT ) Then
Call o.ExtractFile( "c:\temp\" & o.Source )
fname=o.source
If Ucase$( Right$(fname,4)) = ".DOC" Or Ucase$( Right$(fname,4)) = ".DOT" Then
exeToUse = "C:\Program Files\Microsoft Office\Office\WINWORD.EXE"
On Error Goto ErrHdlr
fdt = Filedatetime ( exetouse )
Elseif Ucase$( Right$(fname,4)) = ".XLS" Or Ucase$( Right$(fname,4)) = ".XLB" Then
exeToUse = "C:\Program Files\Microsoft Office\Office\EXCEL.EXE"
On Error Goto ErrHdlr
fdt = Filedatetime ( exetouse )
Elseif Ucase$( Right$(fname,4)) = ".POT" Or Ucase$( Right$(fname,4)) = ".PPS" Or Ucase$( Right$(fname,4)) = ".PPT" Then
exeToUse = "C:\Program Files\Microsoft Office\Office\POWERPNT.EXE"
On Error Goto ErrHdlr
fdt = Filedatetime ( exetouse )
Elseif Ucase$( Right$(fname,4)) = ".TXT" Then
exeToUse = "notepad.exe "
Else
Msgbox "Sorry don't know which application to use!"
End
End If


ExecCmd exeToUse & " " & "C:\temp\" & o.source & ""
Msgbox "File " & o.source & " saved to c:\temp, save and exit application to reattach it to this document."

Msgbox "Process Finished"
Call o.Remove
Set object = rtitem.EmbedObject _
( EMBED_ATTACHMENT, "", "c:\temp\" & fname )
Call doc.Save( True, True )
Call uidoc.close
Call uiw.editdocument( True, doc)
'only do the first attachment
End
End If
End Forall
End If





ErrHdlr:
If Err=53 Then
' ok the application hasn't been found
Msgbox "Sorry cannot find the application executable file at : " & exetouse
End
End If
Print "Error: " & Error$ & " " & Err
End



 

 Jump to:     Notes Tips
    Lotus Notes Index 
  Register for Tips
Previous Lotus Notes Tip 
    Next Lotus Notes Tip
Your company logo here?
Adam Foster
Free Cash Making Web Site
Free Revenue Earning Blog Site
 Free Revenue Earning Newsletter Site   
Free Search Pages Free Web stats
Free Contact Us page
Cheap .com domain names
 Need a website or 
   Lotus Notes
  problem solved?
Adam Foster
Lotus Notes and Domino 6 Programming Bible by Brian Benz, Rocky Oliver
Covers all the programming techniques, concepts, and languages used with Notes and Domino, as well as the many new features of Domino 6 Thoroughly examines the Notes and Domino role with Java, XML, Web services, and other IBM products and technologies Provides extensive code and examples, many geared to the needs of administrators High-profile authors, known to their audience through conferences and articles Companion Web site shows example applications from the book running on a live Domino server and provides code download.
See at: Amazon.co.uk | Amazon.com |