I have code that searches for ^p and replaces with ^p^p to insert a blank line between every paragraph.
I'd like this to not apply to paragraphs that are entirely bold (i.e. subheadings).
So "search for ^p and replace with ^p^p except for cases where the entire paragraph is bold".
I have a sort-of fix where I search for entirely bold paragraphs and delete the paragraph immediately following (which should just be a blank line after the search ^p and replace ^p^p.
The code below deletes the character immediately following an entirely bold paragraph.
Dim para As Paragraph
Dim searchRange As Range
Set searchRange = Selection.Range
searchRange.End = ActiveDocument.Content.End
For Each para In searchRange.Paragraphs
If para.Range.Font.Bold = True And Not para.Range.Information(wdWithInTable) Then para.Range.Next.Delete
Next para
The problem is it does it in tables. So if I have a table header that is bold, it deletes the paragraph following, which will be the text in the next row of the table.
How can I get this bit of code to effectively check if a paragraph is entirely bold, and then delete the paragraph following it, UNLESS it is in a table?
Also if I could do the same but exempting if the following paragraph is a certain style, e.g. List Bullet.
The answer is in the code you are already using. To work with the following paragraph use para.Next, as you are already doing for the deletion.
Simply change:
And Not para.Range.Information(wdWithInTable)
to:
And Not para.Next.Range.Information(wdWithInTable)
Related
In MS Word (2010 .docx) file I need to modify text of title with autonumber, but keep the autonumber:
Original Word Example:
1.4.12 [ORIGINAL_TEXT]
Intended tile after modification:
1.4.12 [MODIFIED_TEXT]
I can get the [ORIGINAL_TEXT] by
ActiveDocument.Paragraphs(i).Range.Text
This returns [ORIGINAL_TEXT], but not autonumber.
But by setting
ActiveDocument.Paragraphs(i).Range.Text = "[MODIFIED_TEXT]"
the autonumber disappear and the title text is only
[MODIFIED_TEXT]
(autonumber missing)
From there I understand, that assigning anything to ActiveDocument.Paragraphs(i).Range.Text is not a way, because I want to keep autonumber.
I was able to modify the title by
ActiveDocument.Paragraphs(i).Range.InsertBefore ("[MODIFIED_TEXT]")
This results in title in form:
1.4.12 [MODIFIED_TEXT][ORIGINAL_TEXT]
(This looks like pointless, but actually my [ORIGINAL_TEXT] is constant text in whole document - it is placeholder, which tells mactro where to insert autogenerated text "[MODIFIED_TEXT]". Therefore after inserting autogenerated text by InsertBefore I can later simply do find and replace "[ORIGINAL_text]" to "", but I want to avoid this second step)
How it is possible to replace [ORIGINAL_TEXT] by [MODIFIED_TEXT] without loosing the autonumber?
ActiveDocument.Paragraphs(i).Range.Text includes the paragraph mark (carriage return).
To replace the text you can either include a carriage return in your replacement text
ActiveDocument.Paragraphs(i).Range.Text = "[MODIFIED_TEXT]" & vbCr
or modify the range you are replacing
Dim textRange As Range
Set textRange = ActiveDocument.Paragraphs(i).Range
textRange.MoveEnd wdCharacter, -1
textRange.text = "Modified text"
The autonumber is likely in the paragraph formatting so you would want to avoid replacing the paragraph mark unless you can add that formatting back.
I need to be able to find paragraphs that are wholly bold (such as a subheading), but I've been struggling to figure it out or find much help online. I've seen similar questions but the responses are too complicated for me to really know what I'm replicating, despite trying to use parts that seem relevant.
My end goal is to, after finding a wholly bold paragraph, delete the character immediately following it (usually an empty line).
So far, this is my code, using MsgBox as my quick easy test to see if the search works correctly:
Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs
If Selection.Font.Bond = True Then MsgBox "All Bold"
Else: Next para
What this macro does is bring up a MsgBox saying "All Bold" for each paragraph in the document (as in, if there are 50 paragraphs it brings the MsgBox up 50 times), rather than check each paragraph then bring up a MsgBox IF it's entirely bold. Ideally, in my actual document of about 50 paragraphs, only the 3 headings would prompt the MsgBox to appear.
As your code doesn't select anything Selection.Font.Bold does not relate to anything in the loop. By simply changing Selection to para.Range you will find only those paragraphs you are looking for.
Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs
If para.Range.Font.Bold = True Then para.Next.Range.Delete
Next para
The paragraph following can be accessed simply by using the Next property.
If the headings have been formatted using a style you could find them simply by looking for all instances of that style.
I have the following code:
For Each DocPara In ActiveDocument.Paragraphs
If (DocPara.style = "Title 1") Then
...
Else
(if DocPara is LIST then)
...
(else if DocPara is TABLE then)
...
End If
Next DocPara
Is there any way to know if the current paragraph is an IMAGE.
The current paragraph cannot "be" an image because any image is always a character in a paragraph OR anchored to a paragraph. It would be necessary to count the number of images in / attached to the paragraph's range.
So a paragraph cannot be just an image, it will always contain at least one string character (ANSI 13, the paragraph mark) and may contain an unlimited number besides an image.
Word supports two kinds of images: InlineShapes and Shapes. The first are handled the same as characters; the latter have text-wrap formatting.
An image formatted with text-wrap may appear to be "in" a paragraph, but is not, and may not even be anchored to the paragraph where it appears. So when the type of image in question is a Shape it's not really possible to determine whether there's an image "in" a paragraph by querying the Paragraph object.
Here's the code to determine whether an InlineShape is in a paragraph and whether any Shapes are anchored to a paragraph. Based on the way your question is phrased I'm hoping your images are InlineShapes...
Dim rngPara as Word.Range
Set rngPara = DocPara.Range
If rngPara.InlineShapes.Count > 0 Then 'the paragraph contains an image
If rngPara.ShapeRange.Count > 0 Then 'an image is anchored to the paragraph
I am having an issue when inserting a file into a word document(main document) using the InsertFile method using a Word VBA Macro I created, where it is inserting an extra space to the left of the paragraph I am trying to insert into the document. The document I amd trying to insert into is set up with multiple bookmarks that either deletes or retains paragraphs and is supposed to retain the spacing between the paragraphs. For whatever reason When inserting a file it adds not only that extra space but also adds extra lines after the paragraph too. Just so you know I have space symbols enabled which word represents with dots.
Below is the structure of the main document that I am inserting all of the text into (I scratched out text from a paragraph that is always in the main document, anonymized the text):
The reason why the middle paragraph bookmark isn't set to the same line as the previous paragraph is because when I try to insert a line break before inserting the file it doesn't add the line breaks and just prints the text in the previous paragraph when i really want them seperated. Wierd thing is the line breaks work for the other paragraphs that are not inserting text from a file...
The file I am attempting to insert is just a word document which consists of one paragraph of text where the space before the first word of the paragraph is not present but when inserted that space is added. Here is an image of the issue with the main document after all of the insertions(anonymized the text with explanations of the issue):
My code first sets the bookmark to a range variable, then sets the text of the bookmark to blank(essentially deleting the bookmark), then uses that range variable to insert either a file, hardcoded text or a database field. The code works fine for the text or database field but adds that extra space when inserting text from a file. Here is the code I am using to insert the file into my main document:
BookmarkRange.InsertFile (FiletoInsert)
My question is there a way to avoid this extra space? Or if not do I just need to figure out the range for that extra space and then delete it out after the insertion? Let me know if you need more information to answer this question.
I can't duplicate the space you're seeing when you insert a file, unless that file has a space as its first character.
When you insert a file, the last character inserted will be a paragraph mark, which explains why the text following what you insert "moves down". If you're getting more than one new paragraph then the file you're inserting contains multiple "empty" paragraphs at the end.
There are two possibilities for bringing in the file and suppressing that paragraph mark:
Bookmark the text in the file that you want to bring in. InsertFile has a parameter Range that lets you specify a bookmark name in the file and inserts only the bookmark's content:
rngBookmark.InsertFile "C:\Test\Test_Centered.docx", "test3"
Remove the paragraph after inserting the file. Range.InsertFile leaves the range at its starting point. So a second range is needed to mark the point that follows the inserted content.
Sub InsertFileNoLeadingSpace()
Dim rngBookmark As Word.Range, rngAfterBookmark As Word.Range
Set rngBookmark = ActiveDocument.Bookmarks("test1").Range
Set rngAfterBookmark = rngBookmark.Duplicate
rngAfterBookmark.Collapse wdCollapseEnd
'Move one character further so that the Range is beyond the inserted content
rngAfterBookmark.MoveStart wdCharacter, 1
rngBookmark.InsertFile "C:\Test\Test_Centered.docx"
'Move it back by two in order to pick up the inserted paragraph mark
rngAfterBookmark.MoveEnd wdCharacter, -2
'If the position after the inserted content is required after deleting the paragraph mark
'this next line is necessary because deleting the paragraph mark
'sets rngAfterBookmark to another location - it doesn't remain where you'd think
Set rngBookmark = rngAfterBookmark.Duplicate
rngAfterBookmark.Delete
rngBookmark.Select
End Sub
I am looking a way to convert the Bullets in Word document to simple text. E.g.
I have these kind of Bullets:
a)-> Apple
b)-> Orange
c)-> Mangoes
I want them to be like this:
a)Apple
b)Oranges
c)Mangoes
I am using this code but it removes the Bullets entirely:
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs()
Set r = oPara.Range
If r.ListFormat.RemoveNumbers = wdListBullet Then
r.ListFormat.ApplyListTemplate _
ListTemplate:=ListGalleries(wdNumberGallery) _
.ListTemplates(1)
End If
Set r = Nothing
Next
Is ActiveDocument.ConvertNumbersToText what you're after?
It can also be run on a specific list if you're not doing this globally.
ETA: It seems like ConvertNumbersToText takes a NumberType argument (this isn't documented by the 2010 spec that F1 brings up, but it is valid). Perhaps the default doesn't apply to all the bullets in your document. A combination of the three possibilities might work.
ActiveDocument.ConvertNumbersToText(wdNumberParagraph) 'Preset numbers you can add to paragraphs by selecting a template in the Bullets and Numbering dialog box.
ActiveDocument.ConvertNumbersToText(wdNumberListNum) 'Default value for LISTNUM fields.
ActiveDocument.ConvertNumbersToText(wdNumberAllNumbers) 'Default value for all other cases.
I tend to use the first one, but your case might be different.