PDA

View Full Version : identify the current file and path



jiggler
11-06-2008, 02:00 AM
Hello

I'm new to VBA and have been attempting to write a code in Word.
The code is intended to save the active file as its name plus 1. So i therefore have numbered incremental file names.

Then this saved file should be saved as the same file name but instead of the number in the file name it should say "Control". Thus replacing any previously saved files with this name. It should save one as a word doc and another as other as html

The problem i am having is that though i can get this code to work, i can only get it to work if i specify individual file names and paths. But i need the code to work on any file that i have open- what do i need to put in place of the file names and path?


Sub incrementedsavename2()
Dim PathAndFileName As String, n As Long
PathAndFileName = "h:\IAS STUFF\Change the file type\animals\elephant v"
PathAndFileName2 = "h:\IAS STUFF\Change the file type\animals\elephant CONTROL"
If Dir(PathAndFileName & ".doc") = "" Then
ActiveDocument.SaveAs (PathAndFileName & ".doc")
Else
n = 1
Do While Dir(PathAndFileName & n & ".doc") <> ""
n = n + 1
Loop
ActiveDocument.SaveAs PathAndFileName & n & ".doc"
End If
ActiveDocument.SaveAs PathAndFileName2 & ".htm"
ActiveDocument.SaveAs PathAndFileName2 & ".doc"
End Sub


I have searched the threads as well as other sites but cant find specifically what i'm looking for, can someone please help??

Thanks
Jiggler

Nelviticus
11-06-2008, 07:42 AM
A document object (including the ActiveDocument object) has a Path property and a Name property. For a document that has been saved, the Name property is the file name.

fumei
11-06-2008, 11:46 AM
I just want to add that:

ActiveDocument.Path

does NOT, repeat NOT, include the folder separation symbol - "\". In other words, if "c:\zzz\yadda.doc" is the active document:
ActiveDocument.Path

returns, "c:\zzz" - NOT "c:\zzz\".

Therefore to get the fully qualified path/filename, you must add the "\". You can do this with either an explicit string ("\"), OR Application.PathSeparator.

ActiveDocument.Path & "\" & _
ActiveDocument.Name


OR

ActiveDocument.Path & _
Application.PathSeparator & ActiveDocument.Name


Both will return: "c:\zzz\yadda.doc"