Computer Forum Forum Index
Register  FAQ Profile Log in to check your private messages Log in Register 

Whats the filename? Macro

 
Post new topic   Reply to topic    Computer Forum Forum Index -> microsoft word vba beginners
View previous topic :: View next topic  
Author Message
steve
Guest





PostPosted: Thu Nov 08, 2007 7:05 pm    Post subject: Whats the filename? Macro Reply with quote

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
Back to top
Google
Sponsor





PostPosted: Thu Nov 08, 2007 7:05 pm    Post subject: Advertisement

Back to top
Helmut Weber
Guest





PostPosted: Thu Nov 08, 2007 7:35 pm    Post subject: Re: Whats the filename? Macro Reply with quote

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"
Back to top
steve
Guest





PostPosted: Thu Nov 08, 2007 8:03 pm    Post subject: Re: Whats the filename? Macro Reply with quote

Quote:
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.
Back to top
Helmut Weber
Guest





PostPosted: Thu Nov 08, 2007 8:08 pm    Post subject: Re: Whats the filename? Macro Reply with quote

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"
Back to top
steve
Guest





PostPosted: Thu Nov 08, 2007 8:14 pm    Post subject: Re: Whats the filename? Macro Reply with quote

Wonderfull thats what Im looking for.

Thanks. I will test it out.

Regards
Back to top
steve
Guest





PostPosted: Thu Nov 08, 2007 8:19 pm    Post subject: Re: Whats the filename? Macro Reply with quote

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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Computer Forum Forum Index -> microsoft word vba beginners All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Computer Forum