I need to loop through all the headings in the document and need to add new heading between Heading-2 and Heading-3. All existing headings are Heading 1 and I know the text.
I need to delete Heading-5.
The original file has 30000 more paragraphs; using old method takes too much time
for example here are the headings
1. Heading-1
2. Heading-2
3. Heading-3
4. Heading-4
5. Heading-5
6. Heading-6
For P = 1 To ActiveDocument.Paragraphs.Count - 1
ptext=ActiveDocument.Paragraphs(p).text
If Left(ActiveDocument.Paragraphs(P).Style, 9) = "Heading 1" Then
If InStr(1, ptext, "Heading-2") > 0 Then
ActiveDocument.Paragraphs.Add _
Range:=ActiveDocument.Paragraphs(P).Range
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="Heading New"
End If
End If
Next
In that case, record a macro in Word for the following steps:
In the Find dialog enter the text for "Heading-3" then click "find". The selection should jump to Heading-3
This will give you the basic syntax for going to Heading 3. You can edit it to remove parameters you don't want/need (you only need the text search, really).
Following the Find you insert code that will create a new paragraph with the text you want. There are a number of ways this can be done, my preference:
Dim rng as Word.Range
Set rng = Selection.Range
rng.InsertBefore vbCr 'paragraph mark
rng.Collapse wdCollapseStart
rng.Text = "NEW STUFF"
Related
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 (:
I'm building a Word document in VBA. I add a table row by row; once it's complete, I want to insert a blank line/paragraph and then start a new table. But when I add the paragraph after the table, the insertion point appears before the paragraph marker, so the next table is added there, and becomes part of the first table.
Set HeaderTableId = WordDoc.Tables.Add(Range:=wrdSel.Range, numcolumns:=3, numrows:=1, AutoFitBehavior:=wdWord9TableBehavior)
Set RowId = HeaderTableId.Rows(1)
RowId.Cells(1) = LeftHeader
RowId.Cells(2).Range.Font.Bold = True
RowId.Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
RowId.Cells(2) = CentreHeader
RowId.Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
RowId.Cells(3) = RightHeader
' (this table only has one row)
With HeaderTableId.Range
.Collapse (WdCollapseDirection.wdCollapseEnd)
.Move Unit:=wdCharacter, Count:=3
.Select
.InsertParagraph
End With
The final .InsertParagraph correctly inserts a blank paragraph after the table, but the insertion point is then before the paragraph marker.
I've also tried inserting a page break, but it has the same problem. I can't work out how to move the insertion point to the end.
I had to "flesh out" your code in order to test - I've pasted the entire test code below.
The key to inserting a second table following the first, separated by a paragraph mark to ensure the two tables are not merged:
It's necessary to collapse the table Range twice: once before and once after inserting the new paragraph.
The code in the question uses .Move, which is unclear as to how the Range is changed. If I were to use a "move" I'd go with .MoveStart which will keep a collapsed range collapsed, but for this problem I prefer Collapse. (There's also MoveEnd, which will extend a collapsed Range to include content.)
What's also different in my version:
it uses a "working Range" that's independent of any table range - this is personal preference
it uses InsertAfter vbCr for inserting the new paragraph - again, personal preference: I always know that what's inserted is part of the Range object. Sometimes, with Insert methods the new content may not be part of the Range, but I know it is with InsertAfter and InsertBefore
The code:
Sub InsertSuccessiveTables()
Dim HeaderTableId As word.Table, nextTable As word.Table
Dim RowId As word.Row
Dim workRange As word.Range
Dim WordDoc As word.Document
Set WordDoc = ActiveDocument
Set workRange = Selection.Range
Set HeaderTableId = WordDoc.Tables.Add(Range:=workRange, numcolumns:=3, numrows:=1, AutoFitBehavior:=wdWord9TableBehavior)
Set RowId = HeaderTableId.Rows(1)
RowId.Cells(1).Range.text = "Left"
RowId.Cells(2).Range.Font.Bold = True
RowId.Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
RowId.Cells(2).Range.text = "Center"
RowId.Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
RowId.Cells(3).Range.text = "Right"
' (this table only has one row)
Set workRange = HeaderTableId.Range
With workRange
.Collapse WdCollapseDirection.wdCollapseEnd
.InsertAfter vbCr 'ANSI 13
.Collapse WdCollapseDirection.wdCollapseEnd
End With
Set nextTable = workRange.Tables.Add(workRange, 1, 4, AutoFitBehavior:=wdWord9TableBehavior)
End Sub
I have the following piece of code (not mine) which adds a tab to every footnote in MS Word (2013).
The code works fine, but it would work even better if it didn't add a tab each time but it would simply replace first character (whatever it would be - tab, space) with a tab.
That way if the macro is run twice I didn't have two tabs, etc.
Sub TabFootnotes()
For s = 1 To ActiveDocument.Footnotes.Count
ActiveDocument.Footnotes(s).Range.Select
With Selection
.Collapse Direction:=wdCollapseStart
.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
.TypeText Text:=vbTab
End With
Next
End Sub
However you do this, you will have to come up with a way to determine if the first character is replaceable or not. This is because when you change that space to something else, this something becomes a part of the footnote's Range. So when the macro is run another time, you'll need to know if you must keep or replace the first character.
Dim f As Footnote
For Each f In ActiveDocument.Footnotes
With f.Range.Characters(1)
If .Text = vbTab Or .Text = " " Then 'Use any other sensible detection logic here
.Text = vbTab
Else
.InsertBefore vbTab
End If
End With
Next
I have VBA for Word that adds a button to the context menu of the right click which launches my application (which works).
I need the word clicked on to pass it as argument. I saw that I couldn't use Selection because right click doesn't select the word, it gives me the letter after the cursor.
With what I've read, I could possibly look at the position of the cursor, then look at both sides to where the word begins and finishes.
This seems to work
Selection.Words(1).Text
Edit
A little more robust to account for ends of sentences.
Sub FindWord()
Dim rWord As Range
If Selection.Words(1).Text = vbCr Then 'end of sentence
'get last word of sentence
Set rWord = Selection.Words(1).Previous(wdWord)
Else
'get selected word
Set rWord = Selection.Words(1)
End If
'There has to be a better way than this
If rWord.Text = "." Or rWord.Text = "?" Then
Set rWord = rWord.Previous(wdWord)
End If
Debug.Print rWord.Text
End Sub
Here is the most simple way to check for the word under the cursor.
Sub Sample()
Dim pos As Long
'~~> if the cursor is at the end of the word
Selection.MoveEnd Unit:=wdCharacter, Count:=1
Do While Len(Trim(Selection.Text)) = 0
'~~> Move one character behind so that the cursor is
'~~> at the begining or in the middle
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
Loop
'~~> Expand to get the word
Selection.Expand Unit:=wdWord
'~~> Display the word
Debug.Print Selection.Text
End Sub
I have a document with various headings (so not necessarily Heading 1 or Heading 2 - but all types of headings).
What I'm trying to do is write a macro that will, for example, delete 2 spaces at the end of each heading.
For example, we have
This is a heading
At the end of heading, I will do:
Selection.Delete Unit:=wdCharacters, Count:=2
I need this to be applied at the end of each heading.
Does anyone know how to do this?
Goto first heading:
Selection.GoTo What:=wdGoToHeading,Which:=wdGoToFirst
Goto next heading
Selection.GoTo What:=wdGoToHeading,Which:=wdGoToNext
Check where you are to see if you have reached the last heading:
x=Selection.Start
Selection.GoTo What:=wdGoToHeading,Which:=wdGoToNext
if x = Selection.Start then '... last heading reachd
Goto end of current heading (for example, to delete chars:
Selection.EndKey Unit:=wdLine