Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Macro help

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I need help creating a macro in word which will check for a string of text, if found then it will print in a certain way, if not found it will continue with the macro. I hope someone can help me.
 
What do you mean by 'print in a certain way'

'check for a string of text'; do you mean find a certain string in an MS Word document or check if a string variable contains a particular string

for the latter, use the InStr() function

for the former, use the .Find object of the Selection or Range objects.

This should get you started
Code:
Option Explicit

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 04/15/02 by Justin G. Ezequiel
'
  Selection.Find.ClearFormatting
  With Selection.Find
    .Text = "justin"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  Selection.Find.Execute
End Sub

Yell if you need more help
 
Let me explain. I'm really really new to this. We have several documents at work which we print in different ways. For eg: One type we print 1 copy of Sections 1 & 6, 2 copies of Sections 2 & 3, 3 copies of section 4 & 5. I have recorded different printing macros for myself for each type of document. I would like to start the macro off by searching through the document for particular wording which are specific to certain documents, if present it would proceed to print in the way that document requires, if not present it would then search for other wording that are in another document. etc. I was going to start off by recording the macro to find the wording, but did not know how to put in the if statements.
Thanks again
 
Code:
Option Explicit

Sub HandleDifferently()
  Dim oDoc As Word.Document
  
  Set oDoc = ActiveDocument
  
  With oDoc.Range.Find
    If .Execute(FindText:="particular wording specific to document1") Then
      MsgBox "handle as document1"
    ElseIf .Execute(FindText:="particular wording specific to document2") Then
      MsgBox "handle as document2"
    ElseIf .Execute(FindText:="particular wording specific to document3") Then
      MsgBox "handle as document3"
    ElseIf .Execute(FindText:="particular wording specific to document4") Then
      MsgBox "handle as document4"
    Else
      MsgBox "handle how?"
    End If
  End With
End Sub
 
Thank you for the reply Justin. Call me stupid, but can I just cut and paste that shown into a macro (other than replacing what I am searching for etc.)? What does Object Explicit mean? What about the brackets after "Handle Differently"?
 
if you want to paste the code i posted into your macro then just copy the code between the
[tt]Sub HandleDifferently()[/tt] and [tt]End Sub[/tt] lines

[tt]Option Explicit[/tt] should be placed at the start of your code window

or you could just ignore it

what it does if present is require all variables used to be dimensioned
ex. [tt]Dim oDoc As Word.Document[/tt]

I do not know how to begin explaining the brackets after [tt]Sub HandleDifferently[/tt]
tell you what, go to the end of your code window and on a new line type &quot;Sub Sample&quot; without the quotes, hit the <Enter> key and see what happens

if you could post your code, I can show you where to put the code I suggested, or something similar.
 
Justin, you are amazing. Thank you so much. This is going to save loads of time printing my docs.

One more thing. How do I remove a macro from my toolbar (I had originally put it there by dragging it when I created it) AND.... How do I place my new macro on my toolbar once I create it.

I cannot find the macro on my toolbar in my list of macros, nor can I find how to place an existing macro onto the toolbar.
 
>>>remove a macro from my toolbar
click on Tools-->Customize
then Right-click on the button you want to remove
then the click on 'Delete'

>>>place my new macro on my toolbar
same way you added the macro you want to delete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top