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.
Related
I am programming a loop which searches for highlighted text in a document.
If a text is found it should be shown in the Word window similar to the non-vba search function that you have in word.
How can I achieve this?
Here is the code that I use to find the highlighted text, and it works well.
But I don't now how to show the found instances in the word document.
With ActiveDocument.Range
.Find.Highlight = True
Do While .Find.Execute
' Jump to found text and prompt user to take action.
Loop
End With
PS: Once an occurrence is found, I want to prompt the user via to take action on this part of the text. The context is important for the user to decide, therefore he has to see the text. I'm clear on the prompting part, but I cannot figure out how to show the text to the user.
You need to use the .Select method. I'd recommend doing this on a .Duplicate of the current found range. e.g. Inside your do loop use '.Duplicate.Select' (NOT .Find.Duplicate.Select)
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)
After setting Myrange of the first paragraph. I need to evaluate number of lines of current paragraph, if lines are less then 3 then following code will turn font into bold.
Set Myrange= Selection.Range.PARAGRAPHS(1).Range
If Myrange.ComputeStatistics(wdStatisticLines) < 3 Then
Myrange.Font.Bold = True
Else
Set Twolines = myrange.Duplicate
'''Here I want to reduce Myrange to only 2 lines
End If
so my question is how can I change Myrange from paragraph into 2 lines?
I had been doing this by Selection method and don't know how to perform it using Ranges. e.g.
Selection.ExtendMode = True
Selection.EndKey Unit:=wdLine
Selection.MoveDown Unit:=wdLine, Count:=2
The approach you have, using the Selection object, is really the only way.
"Lines" and "pages" in a Word document have no corresponding objects in the Word object model. Unlike Words or Paragraphs, for example, lines and pages are "dynamic": where a line or page break occurs depends totally on how Word's layout engine, in concert with the current print driver, processes the document in a particular session. It's quite possible that a line or page break is different on one computer than on another. And they will certainly change "fluidly" as a document is being edited.
For this reason, it's not really possible to deal with a line or a page as an "object" in the progamming sense of the term. That's why it's only possible to deal with these by using Selection.
I've been teaching myself to use macros and VBA in work. I'm not code-minded so bear with me.
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 any 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).
I did find something that suggested 0 being regular, -1 being entirely bold and 9999999 being partially bold, but I've been unable to get anything like that to work as I couldn't tell what code was relevant to me. I'd really appreciate any help, thanks.
The help that can be offered at this stage of your endeavour doesn't go beyond showing you how to help yourself.
The red line of your programming should be to find bold text in your document and determine if it is a whole paragraph. If it is then delete the blank line following it (and then continue searching for the next such paragraph?), and if it isn't, do nothing (and continue searching for the next bold text?).
At the heart of this plan is VBA's Find method. You can look it up on MSDN and/or you can do the search manually while recording your key strokes with the macro recorder. Either of these ways will be very confusing, perhaps the latter more than the former although it promises to be the shorter way to your target. Actually, much depends upon how you define your target. If you want to learn VBA the questions you meet will be challenges you can master. Others have done it before you.
I prefer the method that you were first considering. Create a Word file, and make sure that some text is bold and some is not. Highlight some plain text, or some bold text, or a range that includes plain and bold, and run the following routine.
Sub WhatTheHeck()
If Selection.Font.Bold = True Then MsgBox "All is Bold"
If Selection.Font.Bold = False Then MsgBox "None is Bold"
If Selection.Font.Bold = wdUndefined Then MsgBox "Some is Bold"
End Sub
When you get the hang of that, learn (or ask again) some of the many ways to select text programmatically (with the .Select method), and you'll have everything you need.
(For example, as somebody else suggested, you will be able to do a .Find with any complex criteria, combined with a .Select, and then call WhatTheHeck()).
NOTE: It is worth mentioning that many true/false properties in VBA can be true, false, or wdUndefined, and possibly other values. In general, wdUndefined means 'a combination of true and false'. I know that the Selection.Font.Hidden property can also be wdUndefined, meaning a combination of both.
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.