Insert cell content to word bookmarks doesnt delete the bookmark signs - excel

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

Related

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

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)

Bolding multiple specific words based on a read in value in VBA

I am trying to bold specific words based on a string being read into vba code.
I have the string in read into vba and now want to pick out a word or words to bold them.
If the following was read into vba code: The boy ran down the street.
In the above sentence, what code would I use if I wanted to bold just the word boy? What would I use if I wanted to bold the words boy and the?
I have tried the following code but do not know how to modify it to my specific case.
https://code.adonline.id.au/vba-format-text-microsoft-word/
In short, I want code that will read in a string, search that string for x amount of words, and bold said words to be later exported.
Let me know if you need any more details and what I am trying to accomplish.
Excel is pretty good at formatting whole cells, but it's not very good at formatting parts of cells. One way that I've been able to do this is to turn the text into very basic HTML and then paste the text into a cell. Here's an example.
Public Sub BoldCertainWords()
Dim sSentence As String
Dim dobj As DataObject
Set dobj = New DataObject
sSentence = "The boy ran down the street"
sSentence = Replace$(sSentence, "boy", "<strong>boy</strong>")
sSentence = Replace$(sSentence, "the", "<strong>the</strong>")
dobj.SetText "<html>" & sSentence & "</html>"
dobj.PutInClipboard
Sheet1.Range("A1").Select
Sheet1.PasteSpecial "Unicode Text"
End Sub
You need to set a reference to the MS Forms 2.0 library to get the DataObject if you don't already have it.
Note also that case matters. This will bold the but not The. You could repeat for typical capitalization or get clever with how you find the words and build the HTML string.
Also note that this particular PastSpecial method is Worksheet method, not a Range method. You have to select the range first.

Global Send Keys Issue

I have an object which should highlight all text in a Word document and then press CTRL SHIFT F9 to remove all links. All the text highlights but the links remain afterwards?
I am global mouse clicking the page and then global sending "^+{F9}"
Managed to get this to work by extending the Word VBO Object.
Code used
Dim d As Object = GetDocument(handle,document_name)
d.Select
With d.Fields
.Update
.Unlink
End With
d = Nothing

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

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