NotesRichTexItem : Insert text strings at the first position in existing rich text data - lotus-notes

I'd like to insert the text string into the existing rich text field data at the first position for all of documents in a DB.
NotesRichTextNavigator.FindFirstElement method - This method needs to specify the element type to search but I simply insert the text at the first position of the rich text data.
This might be very basic question, but I could not find the way and waste a few hours... Please help me!

You can do this using a workaround. Instead of working with FindFirstElement, you create a dummy richtextitem, containing the text that you need to prepend to your original item,
add the original item to the dummy item, delete the original item and recreate it.
Then add the dummy item and delete the dummy.
This sounds complex, but it is not that hard actually. Here's a small example in LotusScript on how to do this on a document:
'Get your richtext field
Set rtf = doc.getfirstItem("myRTF")
'create the dummy
Set rtDummy = doc.Createrichtextitem("rtfDummy")
'set the text that you want to insert in your richtext field
Call rtDummy.appendText("Inserting a line of text at the top")
'Add a line to make sure the inserted text is on a separate paragraph
Call rtDummy.Addnewline(1, true)
'Add the content of the original richtext item
Call rtDummy.Appendrtitem(rtf)
'Remove the original item and recreate it
Call rtf.Remove()
Set rtf = doc.Createrichtextitem("myRTF")
'Append the dummy item (including the added text)
Call rtf.Appendrtitem(rtDummy)
'Remove the dummy item
Call rtDummy.Remove()
'Save the document
Call doc.Save(True, True)

Related

Edit richtext field and paste it edited in another richtext field

I'm heaving problems with a specific task on Lotus Notes. I have to copy a rich text field edit it and paste inside another rich text field. But when I edit the content the text style disappears. I've tried to use this solution:
http://www.bobzblog.com/tuxedoguy.nsf/dx/geek-o-terica-15-easy-conversion-of-notes-documents-to-mime-format-part-1
to copy the html and then edit the content. But I got another problem with this:
java.lang.ClassCastException: lotus.domino.local.Item incompatible with lotus.domino.RichTextItem
Can anyone help me with my task?
Thank you.
You don't specify how you want to edit the richtext data. But if you by "edit" mean "programatically make changes to", you can do that in regular Lotusscript using the NotesRichTextItem class.
I wrote a mail-merge class a while back, and it is replacing content in a rich text field with other values, keeping the formatting. If you look at the code you can probably figure it out.
http://blog.texasswede.com/code-mail-mergeform-letters-in-lotuscript/
The relevant code is here:
Public Function MergedRichText() As NotesRichTextItem
Dim range As NotesRichTextRange
Dim cnt As Integer
Set tempbody = sourcefield
Set range = tempbody.CreateRange
Forall p In placeholder
Call p.ProcessPlaceHolder(sourcedoc, maindoc)
If p.text = "" Then
p.text = " -- "
End If
cnt = range.FindAndReplace(p.placeholderstring, p.text, 1+4+8+16)
End Forall
Call tempbody.Compact
Call tempbody.Update
Set targetfield = tempbody
Set MergedRichText = tempbody
End Function

Inserting into Multiple Bookmark Locations - Excel VBA

I am using an Excel macro where I am inserting the same text to several bookmarks in a Word doc. How can I do this by specifying the insert command once and apply it to all the bookmark locations?
Now I am doing the following for all bookmarks?
Dim monYear As String
monYear = Format(DateAdd("m", -1, Now), "mmmm yyyy")
wdApp.Selection.GoTo what:=-1, Name:="Front_Page_Month_Year"
wdApp.Selection.TypeText monYear
wdApp.Selection.GoTo what:=-1, Name:="Page2_Month_Year"
wdApp.Selection.TypeText monYear
And on and on....
There's no way to use a single command to write to multiple bookmark locations. What you can do, however, is reference one bookmark location in order to display that bookmark's content in multiple locations.
Create the bookmark, then insert a cross-reference to it in each location where you want to display the bookmark contents. (Or create one cross-reference, then copy/paste to the other locations.)
A tip for your code: the approach you're using with the Selection object is not optimal. As in Excel, it's better to work with the object model, rather than rely on a Selection. Thus:
Dim doc as Word.Document
Set doc = wdApp.ActiveDocument 'Note: if you're opening a document, set in the Open method
doc.Bookmarks("Front_Page_Month_Year").Range.Text = monYear
'When you're done, update the REF fields (references)
doc.Fields.Update

VBA: How to start and end a list, bulleted or numbered, in Word?

I just can't figure out how to get VBA to start a bulleted list in Word.
I've got some code that types out stuff into word, I can get font and paragraph formatting, no problem, but now I want to create a bulleted list. I've found the following code,
ListFormat.ApplyListTemplate ListTemplate:=ListGalleries(wdBulletGallery).ListTemplates(2)
which should create a bulleted list of the second standard type, but all I can determine is to use it with a 'Range' command which causes the entire document to have the list applied to it. What I'd like to do is have it applied just to the new line that I'm having the code type, and then, at some point, be able to turn the list off, to be able to continue without the list being applied.
Thanks!
This link should help you with your query:
VBA - Bullet Points
Basically this code applies it to a selection:
Selection.Range.ListFormat.ApplyBulletDefault
And this code adds it to the selected paragraph number (in this case paragraph 2):
Documents("MyDoc.doc").Paragraphs(2).Range.ListFormat _
.ApplyBulletDefault
This code applies the Bullet points to a range of paragraphs:
Set myDoc = ActiveDocument
Set myRange = myDoc.Range( _
Start:= myDoc.Paragraphs(3).Range.Start, _
End:=myDoc.Paragraphs(6).Range.End)
If myRange.ListFormat.ListType = wdListNoNumbering Then
myRange.ListFormat.ApplyBulletDefault
End If
Assuming you know the text that is being added, you can use the second example. If you don't know how many paragraphs are being added, then each time you create a new one, increment an integer by 1 and use that integer in the third example.
For Example:
Start:= myDoc.Paragraphs(2).Range.Start, _
End:=myDoc.Paragraphs(i).Range.End)

Insert cell content to word bookmarks doesnt delete the bookmark signs

I have a Worddocument with bookmarks. From Excel I write cell content to the places where I set the bookmarks.
My problem: You can still see the bookmarks.
What I tried:
First I used a placehoder bookmark with
.item("Name1").Range.InsertAfter Rep.NName1
Second I used an enclosing bookmark with
.item("Name1").Range.InsertAfter Rep.NName1
and
.item("Name1").Range.InsertBefore Rep.NName1
I still cannot get rid of the bookmarks.
All I could do is using the sledgehammer approach and delete them but I think there should be a way to replace them during the insert.
Source
If you want to overwrite the bookmark (ie replace any text contained within the bookmark and delete the bookmark itself), you can just set the Text property of the Bookmark's range:
.Item("Bookmark1").Range.Text = "Some new text"
If you want to replace the content of the existing bookmark but identify the new text with the bookmark, you'll need to replace the text then mark the new text as the bookmark:
Dim bmRange As Range
Set bmRange = .Item("Bookmark2").Range
bmRange.Text = "Some new text"
.Add Name:="Bookmark2", Range:=bmRange

How to insert text and possible rich text as well at the current position of the cursor in a rich text field in Lotus Script?

At first, there's a rich text field in the form that has a text inputted already (for this scenario - "hello world"). I have placed the cursor just after the letter "o" from "hello". I have a button that will open up a dialog box with one text field and I was wondering how would you be able to insert the text from that field from the dialog box at the current position of the cursor in the rich text field.
So far the code I have is:
Sub Click(Source As Button)
Dim workspace As New NotesUIWorkspace
Dim s As New NotesSession
Dim db As NotesDatabase
Set db = s.currentdatabase
Dim docFill As New notesdocument(db)
Call workspace.DialogBox _
( "Test", True, True, False, False, _
False, False, "Test Insert text at current position in rich text field", docFill, True, False, True )
Dim string1 As String
string1 = docFill.sampleText1(0)
Dim rts As NotesRichTextStyle
Set rts = s.CreateRichTextStyle
End Subs
End Sub
Let's say I entered "stackoverflow" in sampleText1 text field. After clicking ok then it will be inserted at the position of the cursor in the rich text field. So the result will be "hellostackoverflow world".
Also just an additional question. Let's say I also wanted the text to be in color red or a different font so I would be using notesrichtextstyle class etc to design it. How would you be able to insert the rich text at the position of the cursor in the rich text fiel if that would be the case?
You can insert text at current cursor position with the help of the clipboard. Just let the user insert text in a dialog box, select the text after clicking "OK", copy it and then paste it at the current cursor position in RichText field back in your form.
To accomplish that, create an action "Insert text" in your form's action bar with the LotusScript code
Sub Click(Source As Button)
Dim workspace As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Set uidoc = workspace.CurrentDocument
Set doc = uidoc.Document
If workspace.DialogBox _
("Dlg", True, True, False, True, _
True, False, "Test insert text", doc, True, False, True ) Then
uidoc.Paste
End If
End Sub
Actions in action bars have the advantage that they don't change the cursor position in document on click event. So, the cursor still remains on current position e.g in RichText field clicking on an action button.
Then, create a form "Dlg" for DialogBox with a Text or RichText field "Text". Add the following formula code to form's Postrecalc event (it's executed on "OK" button click):
#Command([EditGotoField]; "Text");
#Command([EditSelectAll]);
#Command([EditCopy])
You have a lot of options with the copy-paste-approach to put content into clipboard:
Create a text in backend and put it direct into clipboard
Create a RichText item in a temp document with all content and style options you can think of, open the document in UI, copy the RichText item content to clipboard and close document without saving
Let users create their text snippet in documents. Let them choose one of it clicking on "Insert text" button - just open the selected document, copy the content to clipboard and close it.
The first problem is that when you click the action button, you will lose focus of the rich text field, and therefor there is no way to know where the cursor was.
I would also suggest that you don't use the extended notation like this:
string1 = docFill.sampleText1(0)
Use the GetItemValue method of the NotesDocument class instead (for several reasons, including performance and future-proofing your code.
If you just want to have the user enter some text, why not use the InputBox function?
Finally, there is not an easy way to insert text in the middle of a rich text. It is much easier to perform a replace of a specific text string in a rich text field. I once created a Lotusscript class to perform mailmerge (create letters based on a template and a form letter with field names and commands), you can find it here: http://blog.texasswede.com/code-mail-mergeform-letters-in-lotuscript/
Perhaps that can help you some. But it has to be done in the backend, you can't do much rich text work in the frontend, unless you use Midas LSX frpn Ben Langhinrichs (http://www.geniisoft.com). I think he got some UI functionality.
But your biggest issue will be the first problem, how to trigger the code without losing focus of the rich text field. I don't see a good solution there. You may want to rethink your design/approach.
If you use a button in the Action Bar then the focus will remain with the rich text field. You can then use uidoc.InsertText("") to insert the text at the current position of the cursor.
You could use...
call uidoc.InsertText( docFill.sampleText1(0) )

Resources