Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-27-2018, 08:31 AM
pentagram_31 pentagram_31 is offline A few macros or functions run in a single macro? Windows 7 32bit A few macros or functions run in a single macro? Office 2013
Novice
A few macros or functions run in a single macro?
 
Join Date: Feb 2015
Posts: 13
pentagram_31 is on a distinguished road
Default A few macros or functions run in a single macro?

Hi everyone, I need to run multiple functions in a single macro. Let me tell you about it with an example:


1- Paul found it much easier to use a pencil
for taking notes as he could .......... mistakes,
which he made plenty of, very easily.
A) type B) erase C) deter
D) furnish E) release

2- You have to watch that market trader very ...........;
otherwise, he will sneakily throw a few rotten
oranges in amongst the good ones.
A) closely B) secretly C) violently
D) daringly E) placidly
3- The-Portuguese water dog is one of several.......... of dog
used by fishermen around the world to retrieve lost nets
and tackle from the water.
A) series B) descriptions C) breeds
D)races E) ranks

I have a lot of test questions/entries like this. I want to make each question (without options) a single paragraph and make them bold. Also there is an empty line between some of the questions, but not all of them, so the macro should take care of it, too. Needless to say, the macro should do its job from the beginning to the end of a document.

Currently I use a macro to make the questions bold, but this macro only make one paragraph/line of "A)", so I need to add a function to make the lines into a single paragraph.



What I'm using right now:

Code:
Sub root()
'
' root Makro
'
'
Selection.Find.ClearFormatting
Selection.GoTo What:=wdGoToHeading, Which:=wdGoToFirst
With Selection.Find
   .Text = "A)"
   .Forward = True
   '.Wrap = wdFindContinue
   .Format = False
   .MatchCase = True
   .MatchWholeWord = True
   .MatchWildcards = False
   .MatchSoundsLike = False
   .MatchAllWordForms = False
End With
Do While Selection.Find.Execute
If Selection.Find.Found = True Then
        Selection.MoveUp Unit:=wdParagraph, Count:=3, Extend:=wdExtend
        Selection.Font.Bold = wdToggle
    Selection.MoveDown Unit:=wdParagraph, Count:=2
End If
Loop
End Sub

Last edited by pentagram_31; 10-27-2018 at 08:35 AM. Reason: I want to clarify sth.
Reply With Quote
  #2  
Old 10-27-2018, 11:33 PM
macropod's Avatar
macropod macropod is offline A few macros or functions run in a single macro? Windows 7 64bit A few macros or functions run in a single macro? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchWildcards = True
    .Text = "[^13]{1,}"
    .Replacement.Text = " "
    .Execute Replace:=wdReplaceAll
    .Text = "[ ]{2,}"
    .Replacement.Text = " "
    .Execute Replace:=wdReplaceAll
    .Text = "(<[0-9]{1,}[!0-9]@)([A-Z]\))"
    .Replacement.Text = "^p\1^p\2"
    .Execute Replace:=wdReplaceAll
    .Text = "(<[0-9]{1,}[!0-9]@) ^13"
    .Replacement.Text = "\1^l"
    .Replacement.Style = "Strong"
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 10-27-2018, 11:37 PM
gmayor's Avatar
gmayor gmayor is offline A few macros or functions run in a single macro? Windows 10 A few macros or functions run in a single macro? Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Based on your example the following should work
Code:
Sub Macro1()
'Graham Mayor - http://www.gmayor.com - Last updated - 28 Oct 2018
Dim orng As Range
    Set orng = ActiveDocument.Range
    orng.Font.Bold = True
    orng = Replace(orng, Chr(11), Chr(13))
    orng = Replace(orng, Chr(13) & Chr(13), Chr(13))
    With orng.Find
        Do While .Execute(FindText:="[0-9]{1,}-", MatchWildcards:=True)
            If orng.Start = orng.Paragraphs(1).Range.Start Then
                orng.MoveEndUntil ")"
                orng.End = orng.End - 2
                orng.Select
                orng = Replace(orng, Chr(13), Chr(32))
                orng = Replace(orng, Chr(32 & Chr(32)), Chr(32))
            End If
            orng.Collapse 0
        Loop
    End With
    Set orng = ActiveDocument.Range
    With orng.Find
        Do While .Execute(FindText:="A)", MatchWildcards:=False)
            If orng.Start = orng.Paragraphs(1).Range.Start Then
                orng.MoveEnd wdParagraph, 2
                orng.End = orng.End - 1
                orng = Replace(orng, Chr(13), Chr(32))
                orng = Replace(orng, Chr(32 & Chr(32)), Chr(32))
                orng.Font.Bold = False
            End If
            orng.Collapse 0
        Loop
    End With
lbl_Exit:
    Set orng = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #4  
Old 10-28-2018, 08:15 AM
pentagram_31 pentagram_31 is offline A few macros or functions run in a single macro? Windows 10 A few macros or functions run in a single macro? Office 2016
Novice
A few macros or functions run in a single macro?
 
Join Date: Feb 2015
Posts: 13
pentagram_31 is on a distinguished road
Default

Thanks for both of the answers! I'll try them when I'm on the computer and let you know.
Reply With Quote
  #5  
Old 10-28-2018, 08:39 AM
pentagram_31 pentagram_31 is offline A few macros or functions run in a single macro? Windows 10 A few macros or functions run in a single macro? Office 2016
Novice
A few macros or functions run in a single macro?
 
Join Date: Feb 2015
Posts: 13
pentagram_31 is on a distinguished road
Default

I tried both of your suggestions, but I got runtime error 5560 in both. In macropod's suggestion, the macro doesn't make any changes.

In gmayor's suggestion, in spite of the error, the macro makes ALL the characters bold.

Quote:
(Do While .Execute(FindText:="[0-9]{1,}-", MatchWildcards:=True)
Any further suggestion is appreciated, thanks in advance.
Reply With Quote
  #6  
Old 10-28-2018, 12:53 PM
pentagram_31 pentagram_31 is offline A few macros or functions run in a single macro? Windows 10 A few macros or functions run in a single macro? Office 2016
Novice
A few macros or functions run in a single macro?
 
Join Date: Feb 2015
Posts: 13
pentagram_31 is on a distinguished road
Default

I guess I made a mistake while explaining my problem in the first post:
[QUOTE]Currently I use a macro to make the questions bold, but this macro only make one paragraph/line of "A)", so I need to add a function to make the lines into a single paragraph.[QUOTE]

It should be : Currently I use a macro to make the questions bold, but this macro only make ONE paragraph/line UPSIDE of "A)", so I need to add a function to make the lines into a single paragraph.

Maybe you have already understood what it meant by looking at the macro codes, but since your suggestions didn't work for me, I double-checked my first post.
Reply With Quote
  #7  
Old 10-28-2018, 01:06 PM
pentagram_31 pentagram_31 is offline A few macros or functions run in a single macro? Windows 10 A few macros or functions run in a single macro? Office 2016
Novice
A few macros or functions run in a single macro?
 
Join Date: Feb 2015
Posts: 13
pentagram_31 is on a distinguished road
Default

@macropod, I got the same runtime error 5560 in this line
Quote:
.Execute Replace:=wdReplaceAll
(the first one)
Reply With Quote
  #8  
Old 10-28-2018, 01:54 PM
macropod's Avatar
macropod macropod is offline A few macros or functions run in a single macro? Windows 7 64bit A few macros or functions run in a single macro? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The code I posted works just fine with the sample data you posted. For example, it turns:
Quote:
1- Paul found it much easier to use a pencil
for taking notes as he could .......... mistakes,
which he made plenty of, very easily.
A) type B) erase C) deter
D) furnish E) release
into:
Quote:
1- Paul found it much easier to use a pencil for taking notes as he could .......... mistakes, which he made plenty of, very easily.
A) type B) erase C) deter D) furnish E) release
The most likely cause of the error is that you're using a system with non-English regional settings (which you didn't tell us about), in which case you need to replace all four instances of:
,}
in the code with:
;}
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 10-28-2018, 01:58 PM
pentagram_31 pentagram_31 is offline A few macros or functions run in a single macro? Windows 10 A few macros or functions run in a single macro? Office 2016
Novice
A few macros or functions run in a single macro?
 
Join Date: Feb 2015
Posts: 13
pentagram_31 is on a distinguished road
Default

Yes, your example is just what I need.
I've made the change in the code, but now I get the runtime error 5834 in this line:
[QUOTE.Replacement.Style = "Strong"][/QUOTE]

By the way, I'm using Turkish in my system.
Reply With Quote
  #10  
Old 10-28-2018, 02:01 PM
macropod's Avatar
macropod macropod is offline A few macros or functions run in a single macro? Windows 7 64bit A few macros or functions run in a single macro? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Change:
"Strong"
to:
wdStyleStrong
(without the quote characters)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 10-28-2018, 02:07 PM
pentagram_31 pentagram_31 is offline A few macros or functions run in a single macro? Windows 10 A few macros or functions run in a single macro? Office 2016
Novice
A few macros or functions run in a single macro?
 
Join Date: Feb 2015
Posts: 13
pentagram_31 is on a distinguished road
Default

Thank you for the quick reply, it means a lot to me!
I've made the change, now it works. The only thing is that it clears the styling of the bold part (it becomes times new roman, 12), but that's not a big deal, I can choose all and change the font manually as I like
Thank you again for your help, master!
Reply With Quote
  #12  
Old 10-28-2018, 02:35 PM
pentagram_31 pentagram_31 is offline A few macros or functions run in a single macro? Windows 10 A few macros or functions run in a single macro? Office 2016
Novice
A few macros or functions run in a single macro?
 
Join Date: Feb 2015
Posts: 13
pentagram_31 is on a distinguished road
Default

By the way, there should have been an empty line between each question, can we do that in the code? This is also my bad in the first post, sorry.
Reply With Quote
  #13  
Old 10-28-2018, 03:17 PM
pentagram_31 pentagram_31 is offline A few macros or functions run in a single macro? Windows 10 A few macros or functions run in a single macro? Office 2016
Novice
A few macros or functions run in a single macro?
 
Join Date: Feb 2015
Posts: 13
pentagram_31 is on a distinguished road
Default

gmayor's macro also worked after I changed ,} to ;}, but it doesn't delete extra empty spaces between words.
I'm going for macropod's macro because it runs faster and deletes the spaces between words, but the only problem is that it also deletes the empty lines between the questions.
Reply With Quote
  #14  
Old 10-29-2018, 03:55 AM
pentagram_31 pentagram_31 is offline A few macros or functions run in a single macro? Windows 10 A few macros or functions run in a single macro? Office 2016
Novice
A few macros or functions run in a single macro?
 
Join Date: Feb 2015
Posts: 13
pentagram_31 is on a distinguished road
Default

one last touch maybe?
Reply With Quote
  #15  
Old 10-29-2018, 12:49 PM
macropod's Avatar
macropod macropod is offline A few macros or functions run in a single macro? Windows 7 64bit A few macros or functions run in a single macro? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Spaces between questions, like fonts, should be managed by the application of the appropriate Style definitions. Since we don't know exactly what you want or what Styles you're using, we're really not in a position to help.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
macro multiple functions

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
A few macros or functions run in a single macro? Infinite loops occurring in find and replace functions in word macro Thefirstfish` Word VBA 5 04-06-2017 07:18 PM
A few macros or functions run in a single macro? Why are my Macros combined into a single module? Cyberseeker Excel Programming 1 04-02-2017 02:43 AM
A few macros or functions run in a single macro? Creating a "super macro" with all kind of functions Xanthopteryx PowerPoint 1 06-16-2016 01:38 AM
A few macros or functions run in a single macro? what functions would this task use, format, macro ? etc fremoy Word 2 02-16-2015 10:48 AM
A few macros or functions run in a single macro? Macro to save to a single PDF LukeExcelKid Excel Programming 4 11-17-2013 01:39 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:26 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft