Whats the filename? Macro

S

steve

I have found the below macro. It works fine.

However how can I make it so that it fill find Document1, Document2
etc to 9. In other words something like a global expression eg
Document?, where ? stands for a number between 1 and 9.

Sub WhatsMyFilename1()
Dim pString As String
pString = "Document1"
If ActiveDocument.FullName = pString Then
MsgBox "Whatever message you want"

End If
End Sub
 
H

Helmut Weber

Hi Steve,

working with .fullname doesn't make much sense
before the document has been saved at least once.

If it was saved, then fullname contains the path
plus the extension ".doc", usually.

This might give you an idea.

Sub WhatsMyFilename1a()
Dim pString As String
pString = Left(ActiveDocument.Name, 9)
If pString Like "Document" & "[123456789]" Then
MsgBox ActiveDocument.Name
End If
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
S

steve

working with .fullname doesn't make much sense
before the document has been saved at least once.

Thank you your right. All Im really checking for is if the file is new
or an existing file.That is, I want to see if it is a new file that
has not been saved and has been given the default name by word out of
the shute.

I want to run a macro on new files. You say well why dont you use the
AutoNew(), because sadly it does not run on the first document when
word is first opened. MS says. .."This macro only runs when word is
already open when you click to open a New document." So then you would
say why dont you use Autoexec, because sadly (form me anyway) Autoexec
runs everytime word opens, So if word is closed and you double click
on a .doc and word opens the autoexec is going to run. But I dont want
it to run on existing files only new files So the only way I can think
of to decrease the chances of the macro running on existing files is
to have it run both in Autoexec and in AutoNew, and in the autoexec
macro have it first check to see if the file is called Document[1-9]
If it is then run. If it is not then dont run. Of course if I have a
document that is acutally called Document2 for exeample then Im
screwed. (Not a serius thing really)

Im sure there are better solutions out there, and Im happy to take
advice but this is the only albeit perhaps Pathetic way of doing it.
Any advice ?? Is there a way to check to see if the file is new or
existing .

Regards.
 
H

Helmut Weber

Hi Steve,

if activedocument.path is empty then
the file was never saved,
means, it is a new file.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
S

steve

Just to be thorough for others who may ask the same thing This seems
to work great thanks again.


Sub HaveDocBeenSaved()
HasItBeenSaved = ActiveDocument.Path
If HasItBeenSaved = Null Then
MsgBox "Not Saved"
Else
MsgBox "Has been Saved"
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top