+ Reply to Thread
Results 1 to 3 of 3

Automatically copying VBA to mail merged document

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-30-2012
    Location
    Leeds
    MS-Off Ver
    Excel 2013
    Posts
    177

    Automatically copying VBA to mail merged document

    Hello all,
    I have a set of code that colours cells based on their values (MMY/MMI/MMN), to show students whether they have completed a task or not. The code is stored in the mail merge document itself, but when I run the merge and the new 'letters' are created the code has to be manually copied into the VBA window. Is there a way of automatically inserting the VBA into the mail merged letters as some non-techhies will be using this system!

    Many thanks

    Luke

    Sub HighlightTargetsMMN()
    Dim Rng As Range, i As Long, TargetList
    TargetList = Array("MMN") ' put list of terms to find here
    For i = 0 To UBound(TargetList)
      Set Rng = ActiveDocument.Range
      With Rng
        With .Find
          .Text = TargetList(i)
          .Format = True
          .MatchCase = True
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
          .Forward = True
          .Wrap = wdFindStop
          .Execute
        End With
        Do While .Find.Found
          .HighlightColorIndex = wdRed
          With .Font
            .Bold = True
            .ColorIndex = wdRed
            .Name = "TW Cen MT"
            .Size = 14
          End With
          If .Information(wdWithInTable) = True Then
            .Cells(1).Shading.BackgroundPatternColorIndex = wdRed
          End If
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
    Next
    End Sub
    Sub HighlightTargetsMMI()
    Dim Rng As Range, i As Long, TargetList
    TargetList = Array("MMI") ' put list of terms to find here
    For i = 0 To UBound(TargetList)
      Set Rng = ActiveDocument.Range
      With Rng
        With .Find
          .Text = TargetList(i)
          .Format = True
          .MatchCase = True
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
          .Forward = True
          .Wrap = wdFindStop
          .Execute
        End With
        Do While .Find.Found
          .HighlightColorIndex = wdYellow
          With .Font
            .Bold = True
            .ColorIndex = wdYellow
            .Name = "TW Cen MT"
            .Size = 14
          End With
          If .Information(wdWithInTable) = True Then
            .Cells(1).Shading.BackgroundPatternColorIndex = wdYellow
          End If
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
    Next
    End Sub
    Sub HighlightTargetsMMY()
    Dim Rng As Range, i As Long, TargetList
    TargetList = Array("MMY") ' put list of terms to find here
    For i = 0 To UBound(TargetList)
      Set Rng = ActiveDocument.Range
      With Rng
        With .Find
          .Text = TargetList(i)
          .Format = True
          .MatchCase = True
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
          .Forward = True
          .Wrap = wdFindStop
          .Execute
        End With
        Do While .Find.Found
          .HighlightColorIndex = wdBrightGreen
          With .Font
            .Bold = True
            .ColorIndex = wdGreen
            .Name = "TW Cen MT"
            .Size = 14
          End With
          If .Information(wdWithInTable) = True Then
            .Cells(1).Shading.BackgroundPatternColorIndex = wdBrightGreen
          End If
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
    Next
    End Sub
    
    Sub fullmacros()
    HighlightTargetsMMN
    HighlightTargetsMMY
    HighlightTargetsMMI
    End Sub
    Update 22/5 - cross posted at http://www.vbaexpress.com/forum/show...762#post309762
    Last edited by lukestkd; 05-22-2014 at 05:02 AM.

  2. #2
    Forum Contributor
    Join Date
    07-30-2012
    Location
    Leeds
    MS-Off Ver
    Excel 2013
    Posts
    177

    Re: Automatically copying VBA to mail merged document

    Can anyone help with this?

  3. #3
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,842

    Re: Automatically copying VBA to mail merged document

    You might have gotten help rather earlier if you'd posted in the correct forum. Given that it's a Word macro, you should have posted in http://www.excelforum.com/word-programming-vba-macros/. See my reply at vbaexpress.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Missing Text In Mail Merged Field
    By adil.master in forum Word Formatting & General
    Replies: 2
    Last Post: 03-10-2014, 08:34 AM
  2. Pasting to a merged range on a shared document.
    By dkime in forum Excel General
    Replies: 2
    Last Post: 10-28-2008, 11:20 AM
  3. Problem getting cents to round, Merged document
    By LTUser54 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-12-2008, 11:32 AM
  4. Open mail merged doc from VBA not working
    By comptechbranden in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-09-2007, 10:50 PM
  5. [SOLVED] Create merged mail in Excel (*NOT* in Word)?
    By DolfnJudy in forum Excel General
    Replies: 0
    Last Post: 11-08-2005, 09:40 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1