PDA

View Full Version : Solved: AutoOpen macro that peforms task based upon title or path of Word document



hkeiner
10-06-2010, 09:41 AM
Hi,

I created an AutoOpen macro that successfully peforms a "task" when a Word 2007 document is opened. However, I would like to have this task performed only when the opened Word document meets certain conditions (rather than for every Word document opened).

Examples of the conditions I would test would be the following:

1) The first 10 characters of the text in the title bar of the Word document.

2) The first 10 characters of the file patch where the Word document is located.

I understand how to construct an "if" condition to control a task but I do not know how to to get the text from the title bar or the file path to write the code.

Thanks in advance for any advice on this

fumei
10-06-2010, 09:56 AM
"However, I would like to have this task performed only when the opened Word document meets certain conditions (rather than for every Word document opened). "

Read your own words.

"have this task performed only when the opened Word document meets certain conditions "


In other words, it is ALREADY opened. So what does this mean?

It means that yes, you can sort of do this, but the document MUST be opened first. You can not test document name (the text in the titlebar), or its Path, until it is opned already.
Sub autoopen()
If ActiveDocument.Name = "Emphasis.doc" Then
MsgBox "No thanks."
ActiveDocument.Close
Else
MsgBox "Hello " & ActiveDocument.Name
End If
End Sub
So, you click on Emphasis.doc and Word opens it, and THEN the logic is tested, so it closes it.

NOTE: this also means you can never open it!

So, can you test what WILL BE opened? Nope.

hkeiner
10-06-2010, 10:59 AM
Thanks for responding. Perhaps by initial post was a bit unclear.

Said another way, the "task" in the AutoOpen macro should only be performed if the name of the opened Word document starts with a certain text string (Such as 'ABCD'). For example, the task is performed if the the document name is ABCDEFGH and it is not performed if the document name is anything else (such as ZYXWVUT). The opened Word document remains open either way for the user. The "task" is simple, it merely changes some document attributes. I just want it to happen on only certain Word documents that can be distinguished by the document name and/or it's file path.

fumei
10-06-2010, 11:09 AM
Then use the code I posted.
Sub autoopen()
' test against the first four characters
If Left(ActiveDocument.Name, 4) = "ABCD.doc" Then
' whatever it is you want to do to the doc
End If
End Sub
If the first four characters of the document name are "ABCD" then do...something. All other documents are ignored.

OR....

Sub autoopen()
' test against the PATH
If ActiveDocument.Path = "c:\whatever\blah" Then
' whatever it is you want to do to the doc(s)
End If
End Sub
All document in C:\whatever\blah will have actions performed on them. All others are ignored.

hkeiner
10-06-2010, 01:43 PM
Thanks. It works fine for me now. The "ActiveDocument.Path" and "ActiveDocument.Name" are the terms I didn't know about.

fumei
10-06-2010, 02:05 PM
Ooops...

If Left(ActiveDocument.Name, 4) = "ABCD.doc" Then

should be (as it is for only 4 characters)

If Left(ActiveDocument.Name, 4) = "ABCD" Then

Regarding .Path, remember that the returned string does NOT include the last folder separation slash. If you are just testing folder value then this should not be an issue, but if you are doing anything else, it may be.

So, again, what is returned by .Path is:

C:\Blah\Whatever

NOT

C:\Blah\Whatever\

hkeiner
10-06-2010, 02:31 PM
Thanks for the added comments. I understand that the NUMBER argument for the LEFT function has to be consistent with the length of the text the function's result is compared to.

In my situation, I need to only look at the first few characters of either the file path or file name to determine if the task should run or not. My AutoOpen macro is working great. Thanks for your help. As you may be able to tell, I am a newbie to VBA.