Reduce Myrange from paragraph into two lines - vba

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.

Related

How to search for entirely bold paragraphs?

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.

VBA code not exiting list (MS Word)

Writing a macro to automatically fix paraphrase spacing issues in MS Word docs generated from software we use.
Goal:
All standard paragraphs have 0pt before and after spacing.
All bullet lists have 3pt before and after spacing.
Progress:
Currently I have a function that sets the entire document to 0pt, then looks through for all lists and changes them to 3pt. (currently also have a highlight on so I can easily see what is being treated as a list).
It works great on some parts, but on other parts (I assume based on how the software we use generates the document), the list doesn't exist and it will continue to format blocks of text and heading to 3pt when it is not wanted (see attached images).
Current code is:
Sub Paragraph()
ActiveDocument.Range.ParagraphFormat.SpaceAfter = 0
ActiveDocument.Range.ParagraphFormat.SpaceBefore = 0
Dim li As Word.list
For Each li In ActiveDocument.lists
li.Range.ParagraphFormat.SpaceBefore = 3
li.Range.ParagraphFormat.SpaceAfter = 3
li.Range.HighlightColorIndex = wdYellow
Next li
End Sub
Working:
Not working:
According to the MSDN:
List Object: Represents a single list format that's been applied to specified paragraphs in a document.
So if you have more than one list with some non-bulleted paragraph(s) in the middle, the Range will start with the first item of the first list and end with the last item of the last list including all non-bulleted paragraph(s) in the middle.
To fix this issue, you need to separate the lists (right-click on the bullet and select Separate List). However, you mentioned that the document was generated by some software, so that is probably not an option. In that case, you will have to iterate though the paragraphs of the Range of each List and check if it has a ListFormat.ListTemplate which indicates that it is a list item, otherwise it is a non-bulleted paragraph:
Sub Paragraph()
ActiveDocument.Range.ParagraphFormat.SpaceAfter = 0
ActiveDocument.Range.ParagraphFormat.SpaceBefore = 0
Dim li As Word.List
Dim p As Paragraph
For Each li In ActiveDocument.Lists
For Each p In li.Range.Paragraphs
If Not p.Range.ListFormat.ListTemplate Is Nothing Then
p.Range.ParagraphFormat.SpaceBefore = 3
p.Range.ParagraphFormat.SpaceAfter = 3
p.Range.HighlightColorIndex = wdYellow
End If
Next p
Next li
End Sub
Even before touching VBA:
Use Styles in the document.
Limit the use of Styles in the document to only those that are in the
template.
Set your spacing in the Styles.
If, at some stage, you change your mind and want to use 6pt spacing, you can adjust the template and re-apply it, rather than finding all the VBA code and re-writing it. Not only that, but by using Styles, you can avoid having VBA code, or having VBA-enabled documents which may interfere with some corporate security settings.
Oh, and set up your corporate structure to limit the use of templates to only the approved ones.

Mac Automator/Word 2016 macro: Trying to make workflow/macro that removes highlights from bullet points in Word 2016 for Mac

THE PROBLEM
I am working in Microsoft Word 2016 for Mac on a very large bullet-point outline in which I frequently need to highlight text. If I highlight all the text on a bullet point, word automatically highlights the bullet point as well. This is because Word by default makes its bullet points take on the styles of their associated text, including highlights. This default behavior has always somewhat irked me because I find the resulting highlighting of bullet points distracting and sloppy.
PREVIOUS MANUAL FIXES
Previously, I have manually fixed the issue. For example, I sometimes would highlight only part of the associated text, then highlighting the rest of text (partially fixing the issue ex ante--but not allowing me to highlight all the text I wanted to at once). Other times, I would fix the issue ex post: I would highlight all the associated text, resulting in the bullet point becoming highlighted, but would then delete the bullet point, bring my text into line with the previous unhighlighted bullet point, and then click "enter" to create a new unhighlighted bullet point (this worked because the text associated with the previous bullet point was not completely highlighted).
THE NEED FOR AUTOMATION
Now, however, I am dealing with a very long document and repetitively manually fixing the issue would be far too inefficient. Thus, I am now looking for a solution that fixes/prevents the bullet point highlighting without me having to make arduous individual changes each time I highlight all the text associated with a bullet point.
My hope is that I could create either an Automator workflow or Microsoft Word 2016 (for Mac) macro that, upon activation, would either: (i) highlight only the associated text of a bullet point--leaving the bullet point unhighlighted--or (ii) automatically remove the highlighting of a highlighted bullet point while leaving the associated text highlighted. With regard to option (ii), I would be happy with a workflow/macro that either (a) removed highlighting from an individual bullet point or (b) removed highlighting from all bullet points in a given document--though the latter is probably preferable. Does anyone have any ideas as to whether any of these options are feasible, and, if so, which would be easiest to implement?
I figured out how to do it with a Word macro (VBA). The below will highlight only the text (and not the bullet point) of the paragraph/associated text where the cursor is. You can keybind the macro to make this pretty fast. However, this is not the fastest fix because it doesn't allow you to highlight en masse, but it certainly is way faster than doing it manually.
Hope this can help someone who runs into the same problem that I had!
Sub Highlighter_Macro()
'
' Highlighter_Macro Macro
'
'
Selection.EndKey Unit:=wdLine
Selection.MoveUp Unit:=wdParagraph, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=3, Extend:=wdExtend
Options.DefaultHighlightColorIndex = wdTurquoise
Selection.Range.HighlightColorIndex = wdTurquoise
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdCharacter, Count:=3, Extend:=wdExtend
Options.DefaultHighlightColorIndex = wdTurquoise
Selection.Range.HighlightColorIndex = wdTurquoise
Selection.EndKey Unit:=wdLine
End Sub

VBA Word Selection.TypeText changing font?

I have a font which has lots of special unicode characters in, I would like to type them into a word document using a macro.
A very simplified version looks like this:
Sub Macro2()
Selection.TypeText (ChrW(58081))
End Sub
I can see using BabelMap (a font viewer), that the character at decimal 58081 is the one I want to enter.
The Issue
When I run this macro, the font changes from the font with the special characters to "Calibri" which is the default. This causes the character to show up as a square box similar to 
If I then manually highlight the square box and change it to my desired font, it corrects itself and shows up.
What I have tried
I have tried setting the font again before typing the text, it doesn't make a difference:
With Selection
.font.Name = "MyFont"
End With
Selection.TypeText (ChrW(58081))
I have also tried adding to the macro to go back and change the font after inserting it. This doesn't make a difference either, the code I use is this:
With Selection
.font.Name = "MyFont"
End With
Selection.TypeText (ChrW(58081))
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.font.Name = "MyFont"
Selection.MoveRight Unit:=wdCharacter, Count:=1
A useful observation
When I enter a character within a normal set, e.g:
Selection.TypeText (ChrW(97))
This works without changing the font as I expect - why does the font change when I enter 58081?
This is only an idea: there is a difference between a paragraph having a font and a [sequence of] letter[s] having a font. If the paragraph has your myFont font, then the inserted text should be your font; otherwise it may revert to the paragraph's font (default for all text of the paragraph unless set to a different font).
If the selection is empty, i.e. you are refering to the insertion point, and you set the font, then the font for the next character is set. However, the TypeText method inserts it before the insertion point, so your set font has no effect.
As for your last attempt, to select the inserted character and set its font, I have no idea why that shouldn't work. Try following setting the font to print the size and font name of the selection (the selection should now be 1 characters in size).
You might also try using the InsertAfter or InsertBefore methods of the Selection object.
If you want to use Selection, this here worked just fine for me:
With Selection
.Font.Name = "Arial Unicode MS"
.Text = "Umlaut test ä ö ü ß"
End With
Many thanks for these ideas, they gave me another thing to look at. In the end, I found even if I copy/pasted a character from another location into the document that it was changing to font to Calibri. When creating a brand new document with nothing else into it and running the macro in my question, I had the same issue. However, I have an old document that was created around 2003 and I found when running even on the same version of office (2013 I am on), the macro works. I think something is going on when creating word documents, perhaps some default setting or perhaps some internal format differences, as my above macros work fine on the old document but not on any new documents I create.
My final solution is to use the old document, copy all my macros into it, and it works fine, even saved as a dotm file and when saved using my 2013 version of office. I am still totally at a loss as to why this is. I can still create new documents on which my macro does not work. Both files are .dotm, the only difference is the machine they were originally created on the version of office.
I hope this helps anyone else with the same problem, it's cost me about a day of pulling my hair out.

Word Macro to convert Bullets into simple Text

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.