none
Execute macro's depending on a certain value in word RRS feed

  • Question

  • I have 2 macro's and one of the two has to be used depending on a value in my word-document.

    How do I have to program this?

    Wednesday, August 13, 2014 12:40 PM

Answers

  • I'm sorry to labour the point, but 'at the top of the document' is still a little vague. Macros require precise directions. However, let's assume that the code is in the first paragraph of the document body (and not in the page header) then the following macros which should be in the document template will work.

    The AutoOpen macro will run when you open a document based on that template and should be in the document template. It will not run when you open the template. If the document is based on the normal template then the macro will run on ALL documents.

    The autopen macro runs SelectMacro, which looks in the first paragraph for the code sequence, and establishes what the first character is. If the first character is 0 it runs the macro named here as MacroA. If 1 it runs the macro here named as MacroB. I have included a pair of simple macros to test it. Change the names of the macros (in bold) to suit your macro names.


    Sub AutoOpen()
    If Not ActiveDocument = ThisDocument Then Call SelectMacro
    End Sub

    Sub SelectMacro()
    Dim oRng As Range
        Set oRng = ActiveDocument.Sections(1).Range.Paragraphs(1).Range
        With oRng.Find
            Do While .Execute(FindText:="[01][A-Z][0-9]{2}-[0-9]{2}", MatchWildcards:=True)
                Select Case oRng.Characters(1)
                    Case 0: Call MacroA
                    Case 1: Call MacroB
                    Case Else
                End Select
                Exit Do
            Loop
        End With
    End Sub

    Sub MacroA()
        MsgBox "This is MacroA"
    End Sub
    Sub MacroB()
        MsgBox "This is MacroB"
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Piri10 Tuesday, August 26, 2014 6:11 AM
    Sunday, August 17, 2014 6:26 AM

All replies

  • What sort of value? Where is that value stored. And when do you want it to run?

    You could use a third macro to evaluate the 'value in the document' and run the appropriate macro. If you tell us a bit more about what you are trying to do it should be simple enough to do it.


    Graham Mayor - Word MVP
    www.gmayor.com

    Wednesday, August 13, 2014 2:32 PM
  • The value is 0 or 1 at a certain place in the document, it's just text.The value shows the type of document and depending on that, text should be replaced or added. It should run automatically when you open the document. I already have 2 macro's to replace / add the text but I can't get the third one (the one to determine which macro should run) right.

    Wednesday, August 13, 2014 2:44 PM
  • Where EXACTLY in the document is the 0 or 1?

    Macros don't do 'a certain place'. You have to be specific.


    Graham Mayor - Word MVP
    www.gmayor.com

    Thursday, August 14, 2014 8:06 AM
  • At the top of the page there is a code, for example 0B15-15 or 1A18-87

    The 0 and the 1 at the beginning of the code show what kind of document it is, for example a internal or external document.

    At certain places in the rest of the document, text has te be added or deleted depending on what kind of document it is. For example if it is external, an address-line etc. should be added.

    So I already have 2 macro's that add/delete the text needed for the 2 types of documents.

    But I need something that reads the first digit of the code so that the correct macro will be executed when you open the file.

    Saturday, August 16, 2014 1:47 PM
  • I'm sorry to labour the point, but 'at the top of the document' is still a little vague. Macros require precise directions. However, let's assume that the code is in the first paragraph of the document body (and not in the page header) then the following macros which should be in the document template will work.

    The AutoOpen macro will run when you open a document based on that template and should be in the document template. It will not run when you open the template. If the document is based on the normal template then the macro will run on ALL documents.

    The autopen macro runs SelectMacro, which looks in the first paragraph for the code sequence, and establishes what the first character is. If the first character is 0 it runs the macro named here as MacroA. If 1 it runs the macro here named as MacroB. I have included a pair of simple macros to test it. Change the names of the macros (in bold) to suit your macro names.


    Sub AutoOpen()
    If Not ActiveDocument = ThisDocument Then Call SelectMacro
    End Sub

    Sub SelectMacro()
    Dim oRng As Range
        Set oRng = ActiveDocument.Sections(1).Range.Paragraphs(1).Range
        With oRng.Find
            Do While .Execute(FindText:="[01][A-Z][0-9]{2}-[0-9]{2}", MatchWildcards:=True)
                Select Case oRng.Characters(1)
                    Case 0: Call MacroA
                    Case 1: Call MacroB
                    Case Else
                End Select
                Exit Do
            Loop
        End With
    End Sub

    Sub MacroA()
        MsgBox "This is MacroA"
    End Sub
    Sub MacroB()
        MsgBox "This is MacroB"
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Piri10 Tuesday, August 26, 2014 6:11 AM
    Sunday, August 17, 2014 6:26 AM
  • You're the best.

    Right now I have something like this:

         0B15-15

            Name:

            Address:

         1.1 Deel I: OA

              TEXT PART 1

         1.2 DEEL II: VB

              TEXT PART 2

    A whole paragraph of tekst should be placed below 1.1 DEEL I: OA and 1.2 DEEL II: VB

    depending on the kind of document (0 or 1 at the beginning of the code).

    But apparently, the tekst is to long, I've put the code I'm using below.

    In the template I put "TEXT PART 1" and the macro replaces it by the tekst I need.

     

    Sub MacroB_metborg()

        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = _
                "TEXT PART 1"
            .Replacement.Text = "here I add the text that apparently is to long "
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll

    End Sub

    And if I split the tekst, the macro won't work.

     

    Sub MacroB_metborg()
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = _
                "TEXT PART 1"
            .Replacement.Text = "..."
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = _
                "TEXT PART 2"
            .Replacement.Text = ..."
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll

    End Sub



    • Edited by Piri10 Monday, August 18, 2014 2:48 PM
    Monday, August 18, 2014 2:47 PM