Hi,
ich versuche gerade ein Feld per Script zu verändern. Ich frage dazu den alten und den neuen Namen ab und versuche dann den Inhalt des Feldes zu verändern, falls der Name darin auftaucht.
Dim ws As New NotesUIWorkspace
Dim session As New NotesSession
Dim db As notesdatabase
Dim collection As NotesDocumentCollection
Dim doc As NotesDocument
Set db = session.CurrentDatabase
Set collection = db.UnprocessedDocuments
Set doc = collection.GetFirstDocument()
Dim oldname As String
Dim newname As String
oldname = ws.Prompt (PROMPT_OKCANCELEDIT, "Old user name", "Type or change the old name in the box below.")
newname = ws.Prompt (PROMPT_OKCANCELEDIT, "New user name", "Type or change the new name in the box below.")
While Not(doc Is Nothing)
Dim oldAgents As Variant
oldAgents = doc.Agents
newAgents = Replace (oldAgents, oldname, newname)
doc.Agents = newAgents
Call doc.Save( True, True )
Set doc = collection.GetNextDocument(doc)
Leider gelingt mir das ganze nicht so recht.
Der Debugger zeigt mir zwar die Daten an aber Replace macht garnichts. :(
Hat jemand von Euch vielleicht eine Idee?
cu
Sebastian
Es kann sein, dass oldname und newname explizit Arrays sein müssen.
Die Funktion replace ist wie folgt spezifiziert:
Replace(sourceArray as Variant, findArray as Variant, replacementArray as Variant[, start as Integer[, count as Integer[, compMethod as Integer]]]) as Variant
findArray und replacementArray sind bei dir aber Strings.
Vielleicht so:
Dim oldName(0) as String
Dim newName(0) As String
oldname(0) = ws.Prompt (PROMPT_OKCANCELEDIT, "Old user name", "Type or change the old name in the box below.")
newname(0) = ws.Prompt (PROMPT_OKCANCELEDIT, "New user name", "Type or change the new name in the box below.")
Ja. Habs ausprobiert:
1. Version:
Sub Initialize
Dim arExist (1) As String
Dim arFind(0) As String
Dim arReplace(0) As String
Dim res As Variant
arExist(0) = "rot"
arExist(1) = "blau"
'arFind(0) = "blau"
'arReplace(0) = "gelb"
res = Replace(arExist, arFind, arReplace)
Forall x In res
Print x
End Forall
End Sub
funktioniert.
2. Kein Fehler vom Kompiler oder Runtime gemeldet. Trotzdem Effekt wie bei dir:
Sub Initialize
Dim arExist (1) As String
'Dim arFind(0) As String
'Dim arReplace(0) As String
Dim arFind As String
Dim arReplace As String
Dim res As Variant
arExist(0) = "rot"
arExist(1) = "blau"
'arFind(0) = "blau"
'arReplace(0) = "gelb"
arFind = "blau"
arFind="gelb"
res = Replace(arExist, arFind, arReplace)
Forall x In res
Print x
End Forall
End Sub
Das generiert einen Kompilierfehler:
Sub Initialize
Dim arExist (1) As String
Dim arFind As Variant
Dim arReplace As Variant
Dim res As Variant
arExist(0) = "rot"
arExist(1) = "blau"
arFind = "blau"
arFind="gelb"
res = Replace(arExist, arFind, arReplace)
Forall x In res
Print x
End Forall
End Sub
Ich finde das übrigens ziemlich unlogisch von der Sprache Basic her.
Ich kann einer Variant-Variablen kein String zuweisen. Weil String kein Objekt ist? und Variant nur Objekttypen aufnimmt?
Wenn dies so ist.
Dann müsste schon der Compiler einen Funktionsaufruf
replace (Variant, string, String)
zurückweisen,
wenn die korrekte Funktionssignatur
replace (variant, variant, variant)
heisst
und es eine Funktion mit der Signatur:
replace (Variant, string, String)
nicht gibt.
Habe jetzt mal das Script von Glombi genommen.
Dim ws As New NotesUIWorkspace
Dim session As New NotesSession
Set db = session.CurrentDatabase
Set collection = db.UnprocessedDocuments
Set doc = collection.GetFirstDocument()
'Get the names from the user
oldname(0) = ws.Prompt (PROMPT_OKCANCELEDIT, "Old user name", "Type or change the old name in the box below.")
newname(0) = ws.Prompt (PROMPT_OKCANCELEDIT, "New user name", "Type or change the new name in the box below.")
While Not(doc Is Nothing)
'Get the old Agents field
oldAgents = doc.Agents
'Change the username
newAgents = Replace (oldAgents, oldname, newname)
doc.Agents = newAgents
Call doc.Save( True, True )
Set doc = collection.GetNextDocument(doc)
Wend
Funktioniert so wie gewünscht. Und ich hoffe es auch verstanden zu haben.
Müssen die Arrays nicht dimensioniert werden?
oldname und newname enthalten ja jeweils nur ein Element. Was aber, wenn oldAgents mehrere Elemente enthält? Werde dann alle überprüft oder nur das Erste?
Ganz schön schwierig so ein Array. ;)
cu
Sebastian
Du hast meine Dimensionierungen weggelassen ;)
Sorry, waren unter Declarations und ich hatte vergessen die zu kopieren.
Sub Initialize
Dim ws As New NotesUIWorkspace
Dim session As New NotesSession
Dim db As notesdatabase
Dim collection As NotesDocumentCollection
Dim doc As NotesDocument
Set db = session.CurrentDatabase
Set collection = db.UnprocessedDocuments
Set doc = collection.GetFirstDocument()
'Get the names from the user
Dim oldname(0) As String
Dim newname(0) As String
oldname(0) = ws.Prompt (PROMPT_OKCANCELEDIT, "Old user name", "Type or change the old name in the box below.")
newname(0) = ws.Prompt (PROMPT_OKCANCELEDIT, "New user name", "Type or change the new name in the box below.")
While Not(doc Is Nothing)
'Get the Agents field
Dim oldAgents As Variant
oldAgents = doc.Agents
'Change the username
newAgents = Replace (oldAgents, oldname, newname)
'Set the Agents field
doc.Agents = newAgents
Call doc.Save( True, True )
Set doc = collection.GetNextDocument(doc)
Wend
End Sub
Hilfe habe ich gelesen und so verstanden, dass das gesamte Array verglichen wird. Egal wieviele Elemente und dass dann einzelne Elemente replaced werden.
Aber es werden die Arrays als Variant erwartet. Warum dann Dim oldname as String?
Sebastian