Insert highlighted sentence at bookmark in MS Word? - vba

So I have a userform that I use to populate a template with text at certain bookmarks. To make it easy to see what text has been inserted, I would like it to be highlighted with yellow. Is there a convenient way to do this for every text that is inserted without typing it, selecting it and then highlighting it? As an example, this is what part of my code looks like atm:
With ActiveDocument
Options.DefaultHighlightColorIndex = wdYellow
'[highlight=yellow].Bookmarks("Modtager").Range.Text = TxtModtager.Value[/highlight]
.Bookmarks("Modtager").Range.Text = TxtModtager.Value
.Bookmarks("KSnr1").Range.Text = txtKSnr.Value
.Bookmarks("KSnr2").Range.Text = txtKSnr.Value
The first line doesn't seem to do anything - even without the option turned on new text is still not highlighted.
The second line is something I found at another site but had to be commented as it's not working.
The last three lines insert the actual text and I guess you could choose to select the bookmark first and then Selection.TypeText Text:="Whatever value I need", followed by selecting the new phrase again (how?) and choose .HighlightColorIndex = wdYellow.
There should be a better way though, any suggestions? :)

This works for me:
SetBkmkText "Modtager", TxtModtager.Value
SetBkmkText "KSnr1", txtKSnr.Value
SetBkmkText "KSnr2", txtKSnr.Value
Sub SetBkmkText(bkmk as String, NewText as String)
With ActiveDocument.Bookmarks(bkmk).Range
.Text = NewText
.HighlightColorIndex = wdYellow
End With
End Sub

Related

Selection.style applied to wrong paragraph (and other issues)

I have multiple documents coming from the same template. Each of them starts with a chapter number, so that I can build a table of contents that includes all of them. Since chapters have sections and subsections, I created a multilevel list style called MyList. Since they are different documents and all lists would start at 1, I need to specify the chapter number manually.
This can be achieved by writing {LISTNUM MyList \l 1 \s x} on the first line, x being chapter number. I wrote a macro to prompt for the chapter number, insert a carriage return and apply a new paragraph style to type the chapter name:
Chapter 1 (style: ChapNum)
Chapter name here (style: ChapName)
Normal text blah blah
The (working) code I have right now is:
Sub ChapterNumber()
Selection.Style = ActiveDocument.Styles("ChapNum")
ActiveWindow.View.ShowFieldCodes = True
Num = InputBox("Chapter number", "Random title")
SendKeys "^{F9}"
Selection.InsertBefore Text:="LISTNUM MyList \l 1 \s " + Num
SendKeys "%{F9}{RIGHT}{ENTER}"
End Sub
I have encountered a few issues until I finally got there, which are:
I was unable to apply a different paragraph style after the last Sendkeys using Selection.Style = ActiveDocument.Styles("ChapName") since the whole thing converted to ChapName style, not only the paragraph where the cursor was pointing.
I've read Sendkeys is not that reliable so, initially, instead of SendKeys "%{F9}{RIGHT}{ENTER}", I tried
ActiveWindow.View.ShowFieldCodes = False
Selection.EndKey Unit:=wdLine
Sendkeys "{ENTER}"
Neither of the first two commands worked, and I don't know why; I have used them in other macros and never had any problem. Would you please be so kind to clarify? Please take in mind my knowledge of vba is very limited.
Thank you.
Add the text "Chapter " to your template and apply the correct style. Add empty paragraphs in the other two required styles and your template is already setup for you to start typing. All that is then required is to add the LISTNUM field which can be done as shown below.
Sub ChapterNumber()
Dim Num As Long
Num = InputBox("Chapter number", "Random title")
Dim location As Range
With ActiveDocument
Set location = .Paragraphs(1).Range.Characters.Last
location.Move wdCharacter, -1
.Fields.Add Range:=location, Text:="LISTNUM MyList \l 1 \s " & Num
End With
End Sub
EDIT IN RESPONSE TO COMMENTS:
As you appear to have defined numbered styles in your template you do need to add the LISTNUM field. Instead you should set the start at number for the list template attached to the style.
Sub ChapterNumber()
Dim Num As Long
Num = InputBox("Chapter number", "Random title")
ActiveDocument.Styles("ChapNum").ListTemplate.ListLevels(2).StartAt = Num
End Sub

How to replace Fields in Word document with their content using VBA?

Some sites use textarea to publish code in articles. If someone copy/paste the article in Word, it shows empty textarea with scrollbars and below the code in a table with numbered lines.
I want to replace it with just code (or with just the table, which I can successfully convert to text), by removing the textarea.
Did try to do it like this
Sub RemoveTextBoxes()
Dim oFld As Word.FormField
With Application.ActiveDocument
' \\ TextInput Type requires to unprotect the document
If .ProtectionType <> wdNoProtection Then .Unprotect
' \\ Loop all formfields in active document
For Each oFld In .FormFields()
' \\ Only remove Formfield textboxes that have textinput only
If oFld.Type = wdFieldFormTextInput And oFld.TextInput.Type = wdRegularText Then
' \\ Delete
oFld.Delete
End If
Next
' \\ Reprotect the document
.Protect wdAllowOnlyFormFields, True
End With
End Sub
If I press Alt+F9 (displays field codes) I do see now
{ HTMLCONTROL Forms.HTML :TextArea.1 }
above the text box with scrollbars! If I close and open up again, it's still here.
How do I get this TextArea content and remove|replace the element with the content?
Dynamic content in Word is managed using "fields". Not all fields that accept input are "form fields", as you discovered when using Alt+F9 do display the field codes.
Word's Find / Replace functionality is quite powerful: it can also be used to find fields, even specific fields. In this case, since you simply want them removed, the HTMLControl fields can be found and replaced with "nothing". (If you want to be more specific and leave some HTMLControl fields, use as much text as necessary to remove only those fields.)
Many people don't realize it, but you can search field codes without needing to display them. Find can also work with field results displayed. The trick is to set the Range.TextRetrievalMode to include field codes (and, in this case, I think also inlcuding hidden text is a good idea, but if that's a problem, comment out or delete that line).
The ^d in the search text represents the opening field bracket: { - if this were left out only what is inside the brackets would be replaced (deleted), which I don't recommend. With ^d the entire field - including the closing bracket - is affected.
Sub FindAndDeleteHtmlFields()
Dim doc As word.Document
Dim fld As word.Field
Dim rngFind As word.Range
Set doc = ActiveDocument
Set rngFind = doc.content
rngFind.TextRetrievalMode.IncludeFieldCodes = True
rngFind.TextRetrievalMode.IncludeHiddenText = True
With rngFind.Find
.Text = "^d HTMLControl"
.ClearFormatting
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
End Sub
Note that this also ports to C# - I have the impression that's actually where you're working...

Indent multiple lines the same way as plain text editor

Sometimes I have code blocks in my Word documents, and I want to work with them without copying to plain text editor.
Namely, I want to have an ability to indent/unindent multiple lines of code using "Tab" character. This task is very simple in any plain text editor or IDE, but for the sake of clarity, I will show it here. Tabs are shown as black arrows:
Initial state
Using the Shift key or mouse, I selected a part of JavaScript function
Then I pressed Tab key on my keyboard
Selected lines were indented by inserting tab character on each line.
How it could be done with VBA?
Since I don't post any code (as evidence of my own efforts), I don't expect to get something completely working. But at least, I hope to get an understanding "how" it could be done.
As David suggested, I recorded a macro. Here how it looks:
Sub Indentator()
Selection.TypeText Text:=vbTab
End Sub
The problem is, that I don't understand how to get it work for multiple lines. If I select them, this macro (and it was not surprise for me) just inserts "Tab" instead of selection.
Insert a tab character at the start of each paragraph in the selection:
Sub Indentator()
Dim para As Paragraph
For Each para In Selection.Paragraphs
para.Range.InsertBefore vbTab
Next
End Sub
(This assumes that each of your code "lines" is a new "paragraph" in Word, which it usually would be if you are intending to copy/paste this to/from actual code.)
If the macros are named IncreaseIndent and DecreaseIndent, they can be run using the Increase and Decrease Indent buttons on the Home tab.
Sub IncreaseIndent()
If Selection.Start = Selection.End Then
Selection.InsertBefore vbTab
Selection.Start = Selection.End
Else
Dim p As Paragraph
For Each p In Selection.Paragraphs
p.Range.InsertBefore vbTab
Next
End If
End Sub
Sub DecreaseIndent()
If Selection.Start = Selection.Paragraphs(1).Range.Start Then
Selection.Start = Selection.Start + 1
End If
Dim p As Paragraph, c As Range
For Each p In Selection.Paragraphs
Set c = p.Range.Characters(1)
If c.Text = vbTab Then c.Delete
Next
End Sub
Reference https://wordmvp.com/FAQs/MacrosVBA/InterceptSavePrint.htm

Check Word 2007 bookmark content and do something if it exists

Is it possible to search the content inside a bookmark and if it exists, do something.
For example, if there is a word document with a bookmark named Bookmark1. The enclosing text for Bookmark1 was created by highlighting the the text "Entered Text Goes Here". I want to create a macro that will check to see if the text inside the bookmark was changed, and if NOT, delete the text, the bookmark, the section break before it.
The code below does this except that it deletes the bookmark even if the text is different because it is looking for the name of the bookmark, not its content.
If ActiveDocument.Bookmarks.Exists("Bookmark1") = True Then
ActiveDocument.Bookmarks("Bookmark1").Select
Selection.Delete
With Selection
.EndKey Unit:=wdStory
.TypeBackspace
.Delete
End With
End If
I really want the If statement to say something like:
If the text inside the Bookmark1 = "Entered Text Goes Here" Then do all the stuff below, else quit.
Ideas anyone?
Word 2007.
The below should work if your document is set up how I think it is, otherwise you will need to have a play around with it:
'TestTxt is the default text in the bookmark (assuming that you are not including the paragraph mark in the bookmark)
Dim TestTxt As String: TestTxt = "Enter text here"
'DMRng is the range of the the bookmark you are looking at
Dim BMRng As Range: Set BMRng = ThisDocument.Bookmarks("Bookmark1").Range
If BMRng.Text = TestTxt Then
'Start is the beginning of the bookmark - 1 (as the character before hand should be your section break?!)
BMRng.SetRange Start:=BMRng.Start - 1, End:=BMRng.End
BMRng.Delete
End If

How can I automatically highlight and extract colored text in MS Word?

I have a bunch of documents that need to be edited. The authors use blue text in some parts of the documents to indicate that those words need to be linked.
Thank you cornelius for the highlight text code:
Sub HighlightNotBlack()
Dim char As Range
For Each char In ActiveDocument.Characters
If char.Font.Color <> wdColorAutomatic And char.Font.Color <> wdColorBlack Then
char.HighlightColorIndex = wdYellow
End If
Next
End Sub
I would like to expand on this question. Is it possible to extract out only the highlighted text into a new word file? It should also be smart enough to know that two or more consecutive words in a sentence would all appear on one line in the new document as opposed to each word getting its own line in the new document. I figure it could look for all highlighted selections and only bring those over since the whole block of text would be entirely highlighted
I came with something like this. It highlights all non-black and non-automatic characters in active document.
Sub HighlightNotBlack()
Dim char As Range
For Each char In ActiveDocument.Characters
If char.Font.Color <> wdColorAutomatic And char.Font.Color <> wdColorBlack Then
char.HighlightColorIndex = wdYellow
End If
Next
End Sub