How can I delete a mail database in HCL Domino over Lotus Script? - lotus-notes

I would like to delete a mail database over a notes agent. Sometimes the mail databases have a replica.
Whats the best way to delete the mail file itself and also all other replicas (if they exist)?
My code below looks like this, but it doesn't delete the mail file in a replica.
Dim mailfile As String
mailfile = "mail\doe.nsf"
Dim db As New NotesDatabase("", mailfile)
If db.IsOpen Then
'Mark to delete it later
Call db.MarkForDelete()
Else
'Delete now
Call db.Remove
End If

You could use the built in function to do this by using NotesAdministrationProcess:
Sub Initialize
Dim session As New NotesSession
Dim adminp As NotesAdministrationProcess
Set adminp = _
session.CreateAdministrationProcess("Software_Server")
noteid$ = adminp.DeleteReplicas("Software_Server", "Guys1")
'- in case you want to open the generated adminp request
If noteid$ <> "" Then
Dim db As New NotesDatabase("Software_Server", "admin4")
Dim ws As New NotesUIWorkspace
Call ws.EditDocument(False, db.GetDocumentByID(noteid$))
End If
End Sub
If you do not want to wait for this (as it needs time: replicate admin4.nsf to all servers, execute admin process there, replicate back...), you can do this by yourself if you know the servers where the replicas are in advance:
Dim mailfile As String
mailfile = "mail\doe.nsf"
Dim otherServers(2) as String
Dim replicaID as String
Dim db as NotesDatabase
otherServers(0) = "FirstServerName/Certifier"
otherServers(1) = "SecondServerName/Certifier"
otherServers(2) = "ThirdServerName/Certifier"
Set db = New NotesDatabase("PrimaryServer/Certifier", mailfile)
If db.IsOpen Then
replicaID = db.ReplicaID
On Error Goto ErrorRemove
'Delete now
Call db.Remove
On Error Goto ErrorHandler
Forall serverName in otherServers
Set db = New NotesDatabase("", "")
Call db.OpenByReplicaID( serverName, replicaID )
If db.IsOpen Then
On Error Goto ErrorRemove
'Delete now
Call db.Remove
On Error Goto ErrorHandler
End If
End Forall
End If
EndOfRoutine:
Exit Sub
ErrorRemove:
Call db.MarkForDelete()
Resume Next
ErrorHandler:
'- do usual error handling here
REMARK: Your check for "db.IsOpen" does not help at all. As "IsOpen" does NOT return if a database is currently open somewhere. It returns, if YOUR SCRIPT was able to open the database at exaxtly that moment... I added an error handler to take this into account.

Related

VBA - How do I specify the Inbox instead of using my inbox?

In my excel spreadsheet I have column A and column B. In column A I have email addresses, in column B I have unique variables. The code below is designed to look into an inbox, compare if any of the subject lines match the unique variable in column B and if they do then forward the email to the email address from column A of that unique variable. This is the code currently:
Public Sub Forward_Email(findSubjectLike As String, forwardToEmailAddresses As String)
Dim NSession As Object
Dim NMailDb As Object
Dim NViewObj As Object
Dim NInboxView As Object
Dim NDocument As Object
Dim NUIWorkspace As Object
Dim NUIDocument As Object
Dim NFwdUIDocument As Object
Set NSession = CreateObject("Lotus.NotesSession")
Call NSession.Initialize("password")
Set NUIWorkspace = CreateObject("Notes.NotesUIWorkspace")
Set NMailDb = NSession.GetDatabase("", "TEST.nsf")
Set NViewObj = NMailDb.GetView("Inbox")
Set NDocument = Find_Document(NInboxView, findSubjectLike)
If Not NDocument Is Nothing Then
Set NUIDocument = NUIWorkspace.EditDocument(False, NDocument)
NUIDocument.Forward
Set NFwdUIDocument = NUIWorkspace.CurrentDocument
Sleep 100
NFwdUIDocument.GoToField "To"
Sleep 100
NFwdUIDocument.InsertText forwardToEmailAddresses
NFwdUIDocument.GoToField "Body"
NFwdUIDocument.InsertText "This email was forwarded at " & Now
NFwdUIDocument.InsertText vbLf
NFwdUIDocument.Send
NFwdUIDocument.Close
Do
Set NUIDocument = NUIWorkspace.CurrentDocument
Sleep 100
DoEvents
Loop While NUIDocument Is Nothing
NUIDocument.Close
Else
MsgBox vbCrLf & findSubjectLike & vbCrLf & "not found in Inbox"
End If
Set NUIDocument = Nothing
Set NFwdUIDocument = Nothing
Set NDocument = Nothing
Set NMailDb = Nothing
Set NUIWorkspace = Nothing
Set NSession = Nothing
End Sub
Private Function Find_Document(NView As Object, findSubjectLike As String) As Object
Dim NThisDoc As Object
Dim thisSubject As String
Set Find_Document = Nothing
Set NThisDoc = NView.GetFirstDocument
While Not NThisDoc Is Nothing And Find_Document Is Nothing
thisSubject = NThisDoc.GetItemValue("Subject")(0)
If LCase(thisSubject) = LCase(findSubjectLike) Then Set Find_Document = NThisDoc
Set NThisDoc = NView.GetNextDocument(NThisDoc)
Wend
End Function
The issue is that now the code looks within the user inbox of the logged in user (in this case being me). I have another inbox open (lets call it TEST) am I able to specify this code to view the information from the open TEST inbox instead. Right now it compares the information from my inbox with TEST as it triggers the error line "not found in inbox".
What it does currently is it looks for the unique variable within my finds it, then tries to compare with TEST for that subject line to forward it. I want it to both look in TEST and then compare with TEST.
You state "The issue is that now the code looks within the user inbox of the logged in use". It doesn't. It uses NSession.CurrentDatabase,, and your NotesSession is loaded into VBA using the Notes OLE classes. It's the OLE classes becuase you are using Notes.NotesSession instead of Lotus.NotesSesion. In the COM classes, that are loaded if you use Lotus.NotesSession., the CurrentDatabase property isn't defined. In the OLE classes, I honestly don't know what the expected behavior is in the OLE classes, but I know for sure that you can't rely on the current database always being the current user's mailbox database.
In any case, if you want to access another user's Inbox, first you have to open that user's mailbox database. To do that, you have to know what server that mailbox database is on, and what the path to the mailbox is on that server. You do that by writing code to read that information from that user's Person document in the Domino Directory, or you can put that information into your spreadsheet for each user. With that, you can use NotesSession.GetDatabase, open the database, and access it more or less the way you are accessing your own mailbox database.

(Domino Notes)How to move the document back to the original NSF?

I already ask "How to move documents from the original NSF to another NSF?"
(Domino Notes)How to move documents from the original NSF to another NSF?
Now, I still want to ask "How to move the document back to the original NSF?"
I want to make the form that can be moved back to the original database, but there is an error in the command, and cannot be moved back.
The following instructions are written in the buttons of the NOTES form.
How can I modify them?
Sub Click(Source As Button)
Dim ws As New notesuiworkspace
Dim uipr As NotesUIDocument
Dim ask_me As Variant
Set uipr = ws.CurrentDocument
data(0) = "Back original NSF"
data(1) = "Non-person case"
ask_me = ws.Prompt(PROMPT_OKCANCELEDITCOMBO,"Reset Reason","Choose a reason...",data(0),data())
If ask_me = False Then Exit Sub
If uipr.editmode=False Then uipr.editmode=True
If ask_me = data(0) Then
Dim achiveDB As New NotesDatabase("fcpnotesM" , "EFA00B7.nsf")
Dim doc As NotesDocument
Set doc = uipr.Document
Call ChangeField
Msgbox "Change field OK"
Call doc.CopyToDatabase(achiveDB)
Msgbox "Copy success"
Call doc.Remove(True)
Msgbox "Move success"
End If
End Sub
uipr seems to be used before it's defined/asigned
If uipr.editmode=False Then uipr.editmode=True //used here
If ask_me = data(0) Then
Dim achiveDB As New NotesDatabase("fcpnotesM" , "EFA00B7.nsf")
Dim uipr As NotesUIDocument //defined here, but no asignment

How to stop password prompt using DAO in Excel VBA and Access

I am using DAO to run queries on a password protected Access database using vba in Excel, occasionly while running the sub an instance of Access is opened up along with a window asking for the database password, not entering a password and pressing cancel makes no difference, the query still runs with the output displayed, is there any way to stop access opening up and asking for a password?
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim DB_Name As String
Dim cond As String
Dim pWord As String
Dim wb As Workbook: Set wb = ThisWorkbook
With wb
On Error GoTo ErrHandler:
clearRange.Value = ""
DB_Name = DataBname()
pWord = pwd()
Set MyDatabase = DBEngine.Workspaces(0).OpenDatabase(DB_Name, False, True, pWord)
Set MyQueryDef = MyDatabase.QueryDefs(queryName) 'Query Name
Set MyRecordset = MyQueryDef.OpenRecordset 'Open the query
pasteRange.CopyFromRecordset MyRecordset
failRange.Value = False
My_Exit:
If MyRecordset Is Nothing Then
'Do Nothing
Else
MyRecordset.Close
Set MyRecordset = Nothing
End If
If MyDatabase Is Nothing Then
'Do Nothing
Else
MyDatabase.Close
Set MyDatabase = Nothing
End If
End With
Exit Sub
ErrHandler:
MsgBox Err.Description
failRange.Value = True
Resume My_Exit
End Sub
Function pwd() As String
pwd = "MS Access;PWD=password"
End Function
To fix the issue, you must change the reference in your workbook from DAO.36 to Microsoft Office XX.0 Access database engine Object Library.
I have tested the code with Access 2010 and Access 2013, and it works fine.
best regards
There is nothing wrong inside your code.
Tested with Access 2003 Backend.
May be if you use Access 2007 and higher, you must set in the DB options the handling for password to legacy.
best regards

Create a document in another database and set the field values

My goal is to create a document from one database in another database and fill the fields with values from the source database. I was able to connect to the other database and compose a document but editing is not possible.
Sub Click(Source As Button)
Dim mydb As NotesDatabase
Dim workspace As NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Dim Server As String
Dim DBPath As String
Dim DBForm As String
'-----------------------------------------------------------------
' Set target database information
'-----------------------------------------------------------------
Server = "Server"
DBPath = "Path"
DBForm = "Form"
'-----------------------------------------------------------------
' Attempt connection to target server
'-----------------------------------------------------------------
Print "Connecting to target database"
Set mydb = New NotesDatabase("", "")
Call mydb.Open(Server, DBPath)
If (mydb.IsOpen) Then
'-----------------------------------------------------------------
' Create new document
'-----------------------------------------------------------------
Print "Connection established to: " + mydb.FileName
Set workspace = New NotesUIWorkspace
Print "Composing change management record"
Set uidoc = workspace.ComposeDocument (Server, DBPath, DBForm)
Call uidoc.FieldSetText("FIELD", "12345")
Else
Msgbox "Warning: unable to open target database."
End If
End Sub
When I'm calling
Call uidoc.FieldSetText("FIELD", "12345")
I'm getting an error message saying that I have to open the document in edit mode. When I'm trying to change the edit mode with
uidoc.EditMode = True
I'm getting the error message "Document command is not available". Can someone please help me here?
it is possible, take the backend not the frontend classes
Dim workspace As New Notesuiworkspace
Dim session As New NotesSession
Dim myDb As NotesDatabase
Dim doc As notesdocument
Set myDb= session.Getdatabase("Server", "Path", false)
If (mydb.IsOpen) Then
Set doc = myDb.Createdocument()
doc.field="12345"
Call workspace.Editdocument(true, doc)
Else
Msgbox "Warning: unable to open target database."
End If

Remainder Mail agent

I have form with 3 fields adress,status,ReportingDate.
Adress field contains the ID where the mil has to be sent.
Now I have created an agent where it should mail to the data present in adress field when status is incomplete and reporting date is exactly 7 days before todays date.
My Code:
Option Public
Option Declare
Sub Initialize
Dim sess As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim timestamp As New NotesDateTime("Today")
Dim Noresponsedate As NotesDateTime
Dim vw As NotesView
Dim diff As Integer
Dim SendTo As Variant
Call timestamp.SetNow()
Set db = sess.CurrentDatabase
Set vw = db.GetView( "data" )
Set doc = vw.GetFirstDocument()
While Not doc Is Nothing
If doc.Status(0) = "Incomplete" Then
Call checktimedifference(doc)
End if
Set doc = vw.GetNextDocument(doc)
wend
End Sub
Sub checktimedifference(doc As NotesDocument)
Dim due As NotesDateTime
Dim present As NotesDateTime
Dim timecheck As variant
Set due = New NotesDateTime ( "" )
Set present = New NotesDateTime ( "Today" )
timecheck = doc.ReportingDate(0)
due.LSLocalTime = timecheck
If due.TimeDifference (present) = -604800 Then
Call sendmailtouser(doc)
End If
End Sub
Sub sendmailtouser(doc As NotesDocument)
Dim db As NotesDatabase
Dim rtiitem As NotesRichTextItem
Dim maildoc As NotesDocument
dim recepient As variant
Set maildoc = New NotesDocument( db )
Set rtiitem = New NotesRichTextItem( maildoc, "Body" )
recepient = doc.adress(0)
maildoc.from = db.Title
maildoc.form = "memo"
maildoc.subject = "A Minor Injury Report:" & doc.Seq_No(0) & " needs your response"
maildoc.SendTo = recepient
Call rtiitem.AppendText( "Please respond to this Minor Injury Report" )
Call rtiitem.AddNewline( 2 )
Call rtiitem.AppendText( "Your response for the Minor Injury Report: " & doc.Seq_No(0) & " is required" )
Call rtiitem.AddNewline( 2 )
Call rtiitem.AppendText( "Please click on the following link to access the document.")
Call rtiitem.AppendDocLink( doc, db.Title )
Call maildoc.Send(False)
End Sub
When I am running the agent on client I am getting the following error:
Please help me to solve the error and send mail to the recepients.
Not using any error handling is very bad practice. But your error will most probably happen in the sendmailtouser- sub, where you dim a local notesdatabase- object named db without actually initializing it.
The line
set maildoc = New NotesDocument( db )
will fail.
Either declare db globally and set it in your initialize or dim ses in that sub again and set db again (worst case as you have to do it for every document)

Resources