Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-31-2014, 10:18 AM
Hoxton118 Hoxton118 is offline Insert input box into macro to allow user to choose multiple text entries Windows 7 32bit Insert input box into macro to allow user to choose multiple text entries Office 2010 32bit
Novice
Insert input box into macro to allow user to choose multiple text entries
 
Join Date: Mar 2014
Posts: 21
Hoxton118 is on a distinguished road
Default Insert input box into macro to allow user to choose multiple text entries

Please could you explain how I could include code which would allow TargetList below to be populated from an input box rather than manually editing the macro. A very simple box along the lines of "Please enter text:".



It's an array so it would be good to give the user the chance to enter more than one item: so that entering "Cats, Dogs" would return Cats or Dogs.

Many thanks

Code:
'Sub CopyParas
Dim range As range
Dim i As Long
Dim TargetList
TargetList = Array("Cats", "Dogs") ' put list of terms to find here, in quotation marks, separated by commas
For i = 0 To UBound(TargetList)
Selection.Find.ClearFormatting
With Selection.Find
.Text = TargetList(i)
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
Selection.StartOf Unit:=wdParagraph
Selection.MoveEnd Unit:=wdParagraph
sBigString = sBigString + Selection.Text
Selection.MoveStart Unit:=wdParagraph
Loop
Next
Documents.Add DocumentType:=wdNewBlankDocument
Selection.InsertAfter (sBigString)
End Sub
Reply With Quote
  #2  
Old 03-31-2014, 03:18 PM
macropod's Avatar
macropod macropod is offline Insert input box into macro to allow user to choose multiple text entries Windows 7 32bit Insert input box into macro to allow user to choose multiple text entries Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Basically:
Code:
Dim TargetList, i As Long
TargetList = InputBox("Put list of terms to find here, separated by commas")
For i = 0 To UBound(Split(TargetList, ","))
  MsgBox Split(TargetList, ",")(i)
Next
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-01-2014, 01:42 AM
Hoxton118 Hoxton118 is offline Insert input box into macro to allow user to choose multiple text entries Windows 7 32bit Insert input box into macro to allow user to choose multiple text entries Office 2010 32bit
Novice
Insert input box into macro to allow user to choose multiple text entries
 
Join Date: Mar 2014
Posts: 21
Hoxton118 is on a distinguished road
Default

Many thanks. I've tried to insert the code as follows (and lots of other ways too) but cannot make it work. Please advise where I've made an error. Thanks.

Code:
Sub CopyParas()
'
' CopyParas Macro
'
Dim range As range
Dim TargetList, i As Long
TargetList = InputBox("Put list of terms to find here, separated by commas")
For i = 0 To UBound(Split(TargetList, ","))
  MsgBox Split(TargetList, ",")(i)
Next
For i = 0 To UBound(TargetList)
Selection.Find.ClearFormatting
With Selection.Find
.Text = TargetList(i)
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
Selection.StartOf Unit:=wdParagraph
Selection.MoveEnd Unit:=wdParagraph
sBigString = sBigString + Selection.Text
Selection.MoveStart Unit:=wdParagraph
Loop
Next
Documents.Add DocumentType:=wdNewBlankDocument
Selection.InsertAfter (sBigString)
End Sub
Reply With Quote
  #4  
Old 04-01-2014, 05:40 AM
macropod's Avatar
macropod macropod is offline Insert input box into macro to allow user to choose multiple text entries Windows 7 32bit Insert input box into macro to allow user to choose multiple text entries Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

You haven't integrated the code I gave you with the rest of your code - all you've done is add it to the module.

Try:
Code:
Sub CopyParas()
Dim TargetList, i As Long, sBigString As String, Doc As Document
TargetList = InputBox("Put list of terms to find here, separated by commas")
For i = 0 To UBound(Split(TargetList, ","))
  With ActiveDocument.Range
    With .Find
      .ClearFormatting
      .Text = "[!^13]@" & Split(TargetList, ",")(i) & "*^13"
      .Forward = True
      .Wrap = wdFindStop
      .Format = False
      .MatchWildcards = True
      .Execute
    End With
    Do While .Find.Found
      sBigString = sBigString & .Text
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
Next
Set Doc = Documents.Add(DocumentType:=wdNewBlankDocument)
Doc.Range.Text = sBigString
Set Doc = Nothing
End Sub
Although my implementation differs, the principle is the same.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 04-01-2014, 10:42 AM
Hoxton118 Hoxton118 is offline Insert input box into macro to allow user to choose multiple text entries Windows 7 32bit Insert input box into macro to allow user to choose multiple text entries Office 2010 32bit
Novice
Insert input box into macro to allow user to choose multiple text entries
 
Join Date: Mar 2014
Posts: 21
Hoxton118 is on a distinguished road
Default

Many thanks for your reply, and sorry I did not know how to include this. Your code works but unfortunately the PC hangs on some searches and fails to return results on others (just shows a blank page). Perhaps this is because of lack of capacity in my PC, but I just wondered if the code could be amended in some way to speed it up? Thanks.
Reply With Quote
  #6  
Old 04-01-2014, 03:11 PM
macropod's Avatar
macropod macropod is offline Insert input box into macro to allow user to choose multiple text entries Windows 7 32bit Insert input box into macro to allow user to choose multiple text entries Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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 already runs about as fast as is possible - and much faster than your original code. If you have a large document and many terms to find, it's quite reasonable that it may take some time to finish processing.

As for skipping some results, that's probably because you've inserted spaces between the terms; only the commas should be inserted (e.g. dog,cat,mouse not dog, cat, mouse), otherwise the spaces are treated as part of the Find criteria.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 04-03-2014, 12:12 AM
Hoxton118 Hoxton118 is offline Insert input box into macro to allow user to choose multiple text entries Windows 7 32bit Insert input box into macro to allow user to choose multiple text entries Office 2010 32bit
Novice
Insert input box into macro to allow user to choose multiple text entries
 
Join Date: Mar 2014
Posts: 21
Hoxton118 is on a distinguished road
Default

Many thanks.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert input box into macro to allow user to choose multiple text entries User input to a variable on the document dsm1995gst Word VBA 1 09-03-2013 03:43 PM
Insert input box into macro to allow user to choose multiple text entries Replacing text with user input.?.?.? brad1977 Word 3 11-20-2012 10:20 AM
Insert input box into macro to allow user to choose multiple text entries Restricting User Input on a TextBox (and setting focus) joatmon Excel Programming 1 06-05-2012 03:01 PM
Macro to Insert Text Into Cells Having Multiple Lines revans611 Excel Programming 4 10-24-2011 10:15 AM
Look up an array based on user input johnsmb Excel 2 01-07-2011 01:12 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:52 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