Consulting

Results 1 to 12 of 12

Thread: RUN MACRO BASED ON FILE NAME

  1. #1

    RUN MACRO BASED ON FILE NAME

    I've only used VBA in Word on a very limited basis...

    I need to write a macro that will run a certain code if a particular document is open (specific filename contains certain words). IE:

    If a document is open whose filename contains "Weekly Press Run" then do something. Otherwise, do something else.

    Any help is appreciated!

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oDoc As Document
      For Each oDoc In Documents
        If InStr(oDoc.Name, "Weekly Press Run") > 0 Then
          'Do this
        Else
          'Do that
        End If
      Next
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Thanks gmaxey! Perfect!

  4. #4
    Hey Greg! Actually, that doesn't work. I need it to only run if the file name CONTAINS "Weekly press run", not if that is the exact name of the document. Any ideas?

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    That is what the code does now. If it is "Weekly press run" instead of "Weekly Press Run" (as coded) then change the code.
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    Better still convert the filenames to lower case before making the test e.g. as follows, then it won't matter what the case of the name is.

    Sub ScratchMacro()
    'Graham Mayor - http://www.gmayor.com
    Dim oDoc As Document
        For Each oDoc In Documents
            If InStr(LCase(oDoc.Name), "weekly press run") > 0 Then
                'Do this
            Else
                'Do that
            End If
        Next
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    Graham,

    Yes, considered that or rather UCASE and WEEKLY PRESS RUN but wasn't sure is the user really wanted that and didn't want to risk muddying the waters ;-). Either way the issue should be resolved.
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    Well, the problem is that the file name doesn't just contain ("Weekly press run") but also a date that changes from week to week...it may also change depending in how many times I open the file from my email (IE, "Weekly press run 9-20-16 (2)" or "Weekly press run 9-20-16 (3)"

  9. #9
    VBAX Contributor
    Joined
    Aug 2012
    Posts
    120
    Location
    If oDoc.Name Like "*Weekly Press Run*" then...

  10. #10
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    Kengar624,

    The solution to the problem is the code that Graham and I have provided/discussed. Have you tried it.
    I have on all three of your example file names. It works.

    Sub ScratchMacro()
    Dim oDoc As Document
      For Each oDoc In Documents
        'If the string "WEEKLY PRESS RUN" is in the string "WhAtEVER ThE fiLE nAMe is Weekly PrEss run" then 
        If InStr(UCase(oDoc.Name), "WEEKLY PRESS RUN") > 0 Then
          Beep  'Do this
        Else
         'Do that
        End If
      Next
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  11. #11
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    Mavila,

    That works for the original question (like mine did) but it isn't case sensitive so won't work with the follow on requirement "Weekly press run"
    Greg

    Visit my website: http://gregmaxey.com

  12. #12
    Quote Originally Posted by gmaxey View Post
    Kengar624,

    The solution to the problem is the code that Graham and I have provided/discussed. Have you tried it.
    I have on all three of your example file names. It works.

    Sub ScratchMacro()
    Dim oDoc As Document
      For Each oDoc In Documents
        'If the string "WEEKLY PRESS RUN" is in the string "WhAtEVER ThE fiLE nAMe is Weekly PrEss run" then 
        If InStr(UCase(oDoc.Name), "WEEKLY PRESS RUN") > 0 Then
          Beep  'Do this
        Else
         'Do that
        End If
      Next
    lbl_Exit:
      Exit Sub
    End Sub

    Thanks guys and sorry! I did try it but I think I had some issues with Excel that were resolved when I closed it completely and restarted.

Posting Permissions

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