Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-28-2006, 12:17 PM
Ziggy1 Ziggy1 is offline
Novice
get file name without Path
 
Join Date: Sep 2006
Location: Ont, Canada
Posts: 8
Ziggy1
Default get file name without Path

Hello

I am using this code to select a file an run a macro, but the code returns the full path. How can I get just the filename? I need to use the same file name that was opened but the path will be different? do I need to parse or is there a method to return just the file name

Dim FileToOpen As String
Dim FileOpenName As String


Dim SaveName As String

' Set the default directory.
' Note: Substitute any existing directory.
Options.DefaultFilePath(Path:=wdDocumentsPath) = "\\10.3.16.243\9105_boh"




With Dialogs(wdDialogFileOpen)
On Error Resume Next
If .Display <> 0 Then
FileToOpen = WordBasic.FileNameInfo(.Name, 1)
Else
MsgBox "No file selected"
End If
End With



Documents.Open FileName:=FileToOpen, ConfirmConversions:=False, ReadOnly:= _
False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
"", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto
Reply With Quote
  #2  
Old 09-29-2006, 07:55 PM
Ziggy1 Ziggy1 is offline
Novice
get file name without Path
 
Join Date: Sep 2006
Location: Ont, Canada
Posts: 8
Ziggy1
Default

Well I guess there is not a lot of activity here, I found a solution here....

http://archive.baarns.com/word/faq/wd_mgf.asp#2

but I tweaked it to suit my needs....


The code will alter your Default the File Open Location to a specific location (coded), but it resets back at the end of the code. It allows me to grab files quickly that are saved in a difficult location (Read Only) by our main system, format the Page and then Save to the users folder where they can work with it

It separates the filename from the Path so I can use it in the Save path


Code:
'Macro recorded/Edited 9/26/06 by ziggy

Sub FileOpenTest()

Dim FileToOpen
Dim FileToSave


Dim szFileName As String
Dim wrdDoc As Document
'''This is just demonstrate the below library function.
szFileName = FileOpenCustom1("", "*.Doc;*.txt")


'''In reality it would be more productive to pass a document object back.
If szFileName <> "" Then
Set wrdDoc = Word.Documents(szFileName)

' disabled
' MsgBox wrdDoc.Path + Chr$(13) + wrdDoc.Name


FileToOpen = wrdDoc.Name


'Modified Ziggy sept 29 2006
'Parse out .txt and replace with .doc
FileToSave = Left(wrdDoc.Name, Len(wrdDoc.Name) - 4) & ".doc"


Dim varDocOne As Variant


Documents.Open FileName:=FileToOpen, ConfirmConversions:=False, ReadOnly:= _
False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
"", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto
Selection.WholeStory
Selection.Font.Size = 8
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientLandscape
.TopMargin = InchesToPoints(0.92)
.BottomMargin = InchesToPoints(0.92)
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(11)
.PageHeight = InchesToPoints(8.5)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.GutterPos = wdGutterPosLeft
End With
ChangeFileOpenDirectory _
"C:\Reports" ' Make Sure directory exists
ActiveDocument.SaveAs FileName:=FileToSave, FileFormat:=wdFormatDocument, _
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False
MsgBox "File " & FileToSave & " Saved to:" & vbNewLine & vbNewLine & " C:\Reports"

ActiveDocument.Close



End If
End Sub
'''
''' Function: FileOpenCustom
'''
''' Comments: Returns the document name if successful. Path of
''' document should be obtained from the document via
''' FileNameFromWindow$() minus the return value.
'''
''' Arguments: pszInitDir$ initial directory.
''' pszFilter$ initial filters to use in dialog.
'''
''' Returns: Filename for success, empty string for failure.
'''
''' Date Developer Action
''' ------------------------------------------------------------------
''' 6/07/96 Steven (Smitch) Mitchell Created
''' Copyright Baarns Consulting Group, Inc. 1996
'''
Function FileOpenCustom1(pszInitDir As String, pszFilter As String) As String
Dim dlgFileOpen As Word.Dialog
Dim szCurDir As String
Dim szdlgFullName As String
Dim wrdDoc As Word.Document

'''Store the current document path
szCurDir = Application.Options.DefaultFilePath(wdDocumentsPath)
'''If pszInitDir is empty use the default path.

' Set the default directory. 'Modified Ziggy sept 29 2006
' Note: Substitute any existing directory.
Options.DefaultFilePath(Path:=wdDocumentsPath) = "\\ServerTest"


If pszInitDir <> "" Then
ChDir pszInitDir
Else
pszInitDir = szCurDir
End If
'''Initailize and reference the dialog object.
Set dlgFileOpen = Word.Dialogs(wdDialogFileOpen)
With dlgFileOpen
'''Update the dialog object.
.Update

'''Set the filter
.Name = pszFilter
'''Display and execute the dialog.
If .Show() <> False Then
'''If the user didn't cancel...
'''The active document is the one just opened.
Set wrdDoc = ActiveDocument
'''Cheat and use the document object to do the parsing.
'''Return the name of the document.
FileOpenCustom1 = wrdDoc.Name

End If
End With

'''Restore the default path setting.
With Application.Options
If .DefaultFilePath(wdDocumentsPath) <> szCurDir Then
.DefaultFilePath(wdDocumentsPath) = szCurDir
End If
End With
End Function





Ziggy
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
truncating path or renaming audio file to play in powerpoint rbookend PowerPoint 0 05-02-2006 03:39 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:52 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft