PDA

View Full Version : [SOLVED:] How to ignore second empty paragraph?



dagerr
01-27-2018, 10:13 AM
Hi,
I want bold a second paragraph and this is a code:


ActiveDocument.Paragraphs(2).Range.Bold = True

but how to bold second paragraph if second is empty (no text) and has only enter sign so third paragraph should be bold:


ActiveDocument.Paragraphs(3).Range.Bold = True

macropod
01-27-2018, 12:42 PM
Your description suggests you're using a poorly-formatted document. There should be no empty paragraphs - the space between paragraphs should be controlled via the 'space before' and/or 'space after' settings, preferably as part of the relevant paragraph Styles. That said, you could use:

With ActiveDocument
If Len(.Paragraphs(2).Range.Text) > 1 Then
.Paragraphs(2).Range.Bold = True
Else
.Paragraphs(3).Range.Bold = True
End If
End With

dagerr
01-28-2018, 12:20 PM
Thank You macropod,
It is working but I can't bold anythig more in document using ctrl+B shortcut. Even when I open next one. Macro is working permanently.

macropod
01-28-2018, 03:35 PM
Well, you shouldn't have assigned your macro to the Ctrl-B shortcut! Restore the original setting, then assign you macro to an an used shortcut.

dagerr
01-28-2018, 11:53 PM
There is no shortcut assigned. I can't bold anything more in a document after macro was turning on. Problem is still working in next documents. I can't bold anything even when I use simple mouse pointer and click on bold B button on a screen. Also if there is only one paragraph it is taking me to visual basic and shows runtime error 5941. It is looks like macro never gonna stop.

macropod
01-29-2018, 12:15 AM
Perhaps you should post your entire code, along with a description of what you're trying to do with it. So far, what you've supplied give no context. Furhermore, unless you've assigned the macro to the Ctrl-B shortcut, it would have no effect on that shortcut's operation.

dagerr
01-29-2018, 12:35 AM
Ok, this is a code:




Sub bold_paragraph()




With ActiveDocument
If Len(.Paragraphs(2).Range.Text) > 1 Then
.Paragraphs(2).Range.bold = True
Else
.Paragraphs(3).Range.bold = True
End If
End With
End Sub









It seems that macro can't stopped.

macropod
01-29-2018, 12:42 AM
I also asked for a description of what you're trying to do with the macro. So far, all you have done is post the code snippet I supplied into a macro without any accompanying logic or error-checking.

dagerr
01-29-2018, 01:00 AM
I don't know how to describe it so please look on movie:

https://youtu.be/au63onRd3ro

macropod
01-29-2018, 01:15 AM
Well, of course the macro won't work with that document - because it only has one paragraph! What would be the point of running a macro to bold the 'second' paragraph when the document has only one paragraph??? You really do need to explain what you are trying to achieve.

dagerr
01-29-2018, 01:18 AM
Yes, so please explain why macro is still working while I'm clicking B-bold buttton on a screen. It should not.

gmayor
01-29-2018, 01:22 AM
According to your video you are trying to use a macro process the second paragraph in a document that has only one paragraph. That will produce an error message unless you trap the error e.g. as follows.
You have also named your macro 'Bold' which is a built-in command name and so your macro will intercept your comands to embolden the text and run the macro. You need to name it something else.

Sub Para2_Bold()
If Documents.Count > 0 Then
With ActiveDocument
If .Paragraphs.Count > 2 Then
If Len(.Paragraphs(2).Range.Text) > 1 Then
.Paragraphs(2).Range.Bold = True
Else
.Paragraphs(3).Range.Bold = True
End If
Else
Beep
MsgBox "Paragraphs 2 and 3 don't exist!"
End If
End With
Else
Beep
MsgBox "There is no document open!"
End If
End Sub

macropod
01-29-2018, 01:29 AM
Yes, so please explain why macro is still working while I'm clicking B-bold buttton on a screen.
Because you named it 'Bold', not 'bold_paragraph' as you said in post #7. The name of the built-in macro attached to the Bold key is, naturally enough, 'Bold'...
Try:

Sub Bold_Para()
With ActiveDocument
If .Paragraphs.Count < 2 Then Exit Sub
If Len(.Paragraphs(2).Range.Text) > 1 Then
.Paragraphs(2).Range.Bold = True
ElseIf .Paragraphs.Count > 2 Then
.Paragraphs(3).Range.Bold = True
End If
End With
End Sub

dagerr
01-29-2018, 01:33 AM
Ok, thanks for answers the problem was with name of macro: "Sub bold()", I've changed it and now it is correct.
Once again thank You for Your patience.