VBA - set default linebreak between two content controls - vba

I am trying to get lines between two content controls in VBA for Word.
I want to set default line break between two content controls.
If it is more than two line breaks between I want to delete the others.
When doing my code in a loop, merging of strings into the content control makes multiple line breaks after merging into the content control.
How can I set default two line breaks between two content control?
Content control A
- line break
- line break
Content control B

I have find a solution for this.
Sub RemoveLineBreak()
Dim rStart As Range
Dim rEnd As Range
ActiveDocument.SelectContentControlsByTag("ContentC1").Item(1).Range.Select
Selection.MoveDown Unit:=wdLine, Count:=1
Set rStart = Selection.Range
ActiveDocument.SelectContentControlsByTag("ContentC2").Item(1).Range.Select
Selection.MoveUp Unit:=wdLine, Count:=1
Set rEnd = Selection.Range
ActiveDocument.Range(rStart.Start, rEnd.End).Select
Selection.Delete
Selection.InsertBreak Type:=wdLineBreak
End Sub

Related

Why does adding a Table of Contents in Word with VBA not create a TOC object like it does when adding one manually?

Office 365; Word v. 2205
Why does adding a TOC with VBA (ActiveDocument.TablesOfContents.Add) not produce the same robust TOC object that adding one manually does?
You will see in the first screenshot, a TOC that I added with VBA. The second screenshot is a TOC that I added manually. The manual TOC almost acts like a field object. It even comes with a title.
Is there a way to replicate the manual TOC but with VBA?
Here is my code:
Sub TOC()
With Selection
.HomeKey Unit:=wdStory
.MoveStartUntil (Chr(13))
.MoveStart Count:=1
.TypeParagraph
.MoveEnd Count:=-1
.ClearFormatting
End With
Set oTOC = Selection.Range
With ActiveDocument
.TablesOfContents.Add Range:=oTOC
End With
End Sub
Screenshot 1:
Screenshot 2:

How to search from a certain point in a document down and end the search?

I have the following code that searches for a certain point in a document and creates a search range until the end of the document. Then within that range it removes the paragraph following entirely bold paragraphs (subheadings), ignoring any styles that aren't Normal and aren't in a table. However, it seems to search the entire document (i.e. the beginning as well). How can I make it only search the range (i.e. from where I've positioned the cursor down to the end of the document)?
Dim aPara As Paragraph
Dim oSearchRange As Range
With Selection.Find
.Text = "Dear "
End With
Selection.MoveDown Unit:=wdParagraph, Count:=4
Set oSearchRange = Selection.Range
oSearchRange.End = ActiveDocument.Content.End
oSearchRange.MoveEnd wdParagraph, -1
For Each aPara In oSearchRange.Paragraphs
If aPara.Range.Font.Bold = True And aPara.Range.Next.Style = ActiveDocument.Styles("Normal") And Not aPara.Range.Next.Information(wdWithInTable) Then aPara.Range.Next.Delete
Next aPara
Thanks
I needed to add .Execute after the "Dear " search, thanks to Teamothy (:

Word 2013 Move the insertion point to the end of a word

It there a simple direct way to move the insertion point to the end of a word in Word 2013? By end of the word, I mean the last character of the word is to the insertion point’s left, and the trailing space or punctuation is to the right, and nothing is selected. I’m convinced Word 2002 was able to do this without a macro. I’ve created the following macro to do this, but I’m convinced there has to be a built in way to do it, or at least the macro can be made simpler.
Sub MoveCursorEndWord()
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
If Selection.Text <> " " Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
End If
End Sub
Actually, the procedure I came up with isn't so much different from yours at all.
Sub EndOfWord()
Dim Rng As Range
With Selection
.Words(1).Select
.Collapse wdCollapseEnd
Do While .Start
Set Rng = .Range
Rng.MoveStart wdCharacter, -1
If Asc(Rng.Text) = 32 Then
.Move wdCharacter, -1
Else
Exit Do
End If
Loop
End With
End Sub
The problem is that Word insists on including trailing spaces into its concept of a "word". Since you seem to follow a different definition there is a natural conflict.

VBA Word expanding each find occurance by two lines

Trying to join text of video caption into one paragraph, I want to delete all blank lines with 2 additional lines. For example:
1
00:00:04,350 --> 00:00:07,609
This tutorial will show how to seamlessly transfer
2
00:00:07,609 --> 00:00:11,059
a model from Revit structure to ETABS
etc, I would the result to be: This tutorial will show how to
seamlessly transfer a model from Revit structure to ETABS,...
My best try is and is giving incomplete results :(
Sub DelExpandEmpty()
Dim oPara As Word.Paragraph
Dim var
For Each oPara In ActiveDocument.Paragraphs
If Len(oPara.Range) = 1 Then
oPara.Range.Select
Selection.MoveDown unit:=wdLine, Count:=3
Selection.Expand wdLine
Selection.Delete
End If
Next
End Sub
I think it should be:
Selection.MoveDown Unit:=wdLine, Count:=2, Extend:=wdExtend

How to commence and stop highlighting text using VBA's HighlightColorIndex

I am pulling text from another application and creating a MS-Word document on the fly.
Occasionally there may be some highlighting of words needed which I perform as I find these. What I cannot understand is how to cease displaying the HighlightColorIndex.
I've tried Selection.Collapse, Selection.Range.Collapse and Selection.Range.HighlightColorIndex = wdNoHighlight all to limited success. Can you assist please?
Dim lngRangeStart As Long
Dim lngRangeEnd As Long
Selection.TypeText Text:="Test of colour" ' No highlighting at present
Selection.TypeParagraph '
Selection.TypeText Text:="Starting colour after colon: " ' No highlighting at present
lngRangeStart = Selection.Start ' set to the start of the Range
Selection.Range.StartOf
Selection.TypeText Text:="This text is highlighted"
lngRangeEnd = Selection.Start ' set to the end of the Range and sel.start appears correct
Selection.SetRange Start:=lngRangeStart, End:=lngRangeEnd ' sets range correctly
Selection.Range.HighlightColorIndex = wdYellow
' >>> This is where I need to cease highlighting but what to do?
{funky code to stop highlighting here}
Selection.TypeText Text:="Now back to clear text"
You need to select text as you did before and reset its highlight to none wdNoHighlight
Use below code
' >>> This is where I need to cease highlighting but what to do?
'{funky code to stop highlighting here}
Selection.Move WdUnits.wdCharacter, 1
''Clear for text
lngRangeStart = Selection.Start
Selection.TypeText text:="Now back to clear text"
lngRangeEnd = Selection.Start
Selection.SetRange Start:=lngRangeStart, End:=lngRangeEnd ' sets range correctly
Selection.Range.HighlightColorIndex = wdNoHighlight
Selection.Move WdUnits.wdCharacter, 1
Selection.TypeText text:="Now back to the future text"
If I understand your question correctly, then you just set the highlight color to wdColorAutomatic, which is a constant specifying the automatic (default) color.
So putting it all together, to highlight text, you'd set its background to wdColorYellow. To remove the highlighting, you'd set its background to wdColorAutomatic.