PDA

View Full Version : [SOLVED:] RUN MACRO BASED ON FILE NAME



Kengar624
09-16-2016, 07:41 AM
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!

gmaxey
09-16-2016, 11:03 AM
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

Kengar624
09-16-2016, 12:15 PM
Thanks gmaxey! Perfect!

Kengar624
09-19-2016, 01:41 PM
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?

gmaxey
09-19-2016, 03:42 PM
That is what the code does now. If it is "Weekly press run" instead of "Weekly Press Run" (as coded) then change the code.

gmayor
09-19-2016, 09:53 PM
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

gmaxey
09-20-2016, 04:08 AM
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.

Kengar624
09-20-2016, 07:30 AM
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)"

Mavila
09-20-2016, 09:07 AM
If oDoc.Name Like "*Weekly Press Run*" then...

gmaxey
09-20-2016, 10:22 AM
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

gmaxey
09-20-2016, 10:27 AM
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"

Kengar624
09-20-2016, 12:29 PM
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.