Macro not working? (New to VBA)

P

paoloricardo

I have the following macro, which is a bog standard starter example:

Sub SelectAndCentrePara3()

Dim rngParagraph As Range
Set rngParagraph = ActiveDocument.Paragraphs(3).Range
With rngParagraph
.ParagraphFormat.Alignment = wdAlignParagraphCenter
With .Font
.Bold = True
.Name = "Arial"
.Size = 15
End With
End With
End Sub

I have macros enabled in my Word document but when I run the macro all
that happens is that an extra line is inserted between the first and
second paragraphs of the document (which has 5 paragraphs).

I'd appreciate any indication of what might be wrong (I'm using Word
2007)

Thanks
 
J

Jay Freedman

I have the following macro, which is a bog standard starter example:

Sub SelectAndCentrePara3()

Dim rngParagraph As Range
Set rngParagraph = ActiveDocument.Paragraphs(3).Range
With rngParagraph
.ParagraphFormat.Alignment = wdAlignParagraphCenter
With .Font
.Bold = True
.Name = "Arial"
.Size = 15
End With
End With
End Sub

I have macros enabled in my Word document but when I run the macro all
that happens is that an extra line is inserted between the first and
second paragraphs of the document (which has 5 paragraphs).

I'd appreciate any indication of what might be wrong (I'm using Word
2007)

Thanks

You're right, this is a bog standard macro, and here it does exactly what you
think it should. This makes me wonder whether you're really running this macro,
or if you're mistakenly running something else.

Try sticking a MsgBox line in this macro, just before or after the Dim
statement, to pop up a message when the macro runs. Does it appear? If not,
exactly how are you running the macro?
 
P

paoloricardo

You're right, this is a bog standard macro, and here it does exactly what you
think it should. This makes me wonder whether you're really running this macro,
or if you're mistakenly running something else.

Try sticking a MsgBox line in this macro, just before or after the Dim
statement, to pop up a message when the macro runs. Does it appear? If not,
exactly how are you running the macro?

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.

Jay: I was running the macro via the Macros -> Run option, selecting
the named macro from the listbox.

Anyway, I added a MsgBox and it pops up when and where expected in the
correct macro.

Paul
 
J

Jay Freedman

Jay: I was running the macro via the Macros -> Run option, selecting
the named macro from the listbox.

Anyway, I added a MsgBox and it pops up when and where expected in the
correct macro.

Paul

OK, next step: Open the macro in the VBA editor; arrange the windows so you can
see both the document and the code. Put the cursor anywhere in the code and
press F8 repeatedly. Each time you press F8, VBA will execute one line of the
macro. You _should_ see the changes taking place in the document one at a time.
What happens?
 
P

paoloricardo

OK, next step: Open the macro in the VBA editor; arrange the windows so you can
see both the document and the code. Put the cursor anywhere in the code and
press F8 repeatedly. Each time you press F8, VBA will execute one line of the
macro. You _should_ see the changes taking place in the document one at a time.
What happens?

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.

Jay: nothing happens in the document until I step over the penultimate
End With, at which point an additional line is inserted after
paragraph 1

Paul
 
S

Shauna Kelly

Jay: nothing happens in the document until I step over the penultimate
End With, at which point an additional line is inserted after
paragraph 1

Paul

Hi Paul

Based on your diagnosis so far, I have two suggestions:

1. Immediately after the line that says
With rngParagraph
add a new line:
.Select

When the macro finishes running, what is .Select-ed? Is it what you
think the third paragraph is? Is it possible that whatever is now
selected has been centred and made bold?

2. Click in your document, and then click the ¶ button on the toolbar.

You should now see one end-of-paragraph marker (¶) at the end of each
paragraph.

I suspect that what you think are three paragraphs are not.

For more information about that, see
What do all those funny marks, like the dots between the words in my
document, and the square bullets in the left margin, mean?
http://www.word.mvps.org/FAQs/Formatting/NonPrintChars.htm

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
P

paoloricardo

Hi Paul

Based on your diagnosis so far, I have two suggestions:

1. Immediately after the line that says
With rngParagraph
add a new line:
.Select

When the macro finishes running, what is .Select-ed? Is it what you
think the third paragraph is? Is it possible that whatever is now
selected has been centred and made bold?

2. Click in your document, and then click the ¶ button on the toolbar.

You should now see one end-of-paragraph marker (¶) at the end of each
paragraph.

I suspect that what you think are three paragraphs are not.

For more information about that, see
What do all those funny marks, like the dots between the words in my
document, and the square bullets in the left margin, mean?http://www.word.mvps.org/FAQs/Formatting/NonPrintChars.htm

Hope this helps.

Shauna Kelly. Microsoft MVP.http://www.shaunakelly.com/word

Shauna: I think that your second comment about the end of paragraph
markers is correct. I had a single line between each of my paragraphs,
each of which had an end of paragraph marker, so the blank lines were
being regarded as paragraphs. I have eliminated those now and the
macro works as intended. Thanks to you and Jay for your assistance.

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top