PC Review


Reply
Thread Tools Rate Thread

Code for Select a file and save it in a different location

 
 
Oggy
Guest
Posts: n/a
 
      22nd Apr 2007


Hi

I am trying to write a code that allows me to select a file and save
it in a different location, and then delete the orginal file. I have
the following code, that does not work, i think i may be over
complicating it. Please Help!




Sub remove FileName()
Dim Filt As String
Dim FilterIndex As Integer
Dim FileName As Variant
Dim Title As String

' Set up list of file filters
Filt = "Text Files (*.txt),*.txt," & _
"Lotus Files (*.prn),*.prn," & _
"Comma Separated Files (*.csv),*.csv," & _
"ASCII Files (*.asc),*.asc," & _
"All Files (*.*),*.*"

' Display *.* by default
FilterIndex = 5

' Set the dialog box caption
Title = "Select a File to move"


' set directory
Chdir H:\OPEN ORDERS

' Get the file name
FileName = Application.GetOpenFilename _
(FileFilter:=Filt, _
FilterIndex:=FilterIndex, _
Title:=Title)

' Exit if dialog box canceled
If FileName = False Then
MsgBox "No file was selected."
Exit Sub
End If
WorkBook.Open filename: .selecteditems(1)

If Val(Application.Version) < 10 Then
MsgBox "This requires Excel 2002 or later.", vbCritical
Exit Sub
End If
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a location for the PO"
.Show
If .SelectedItems.Count = 0 Then
MsgBox "Canceled"
Else
WorkBook.SaveAs .SelectedItems(1)
End If
End With


Chdir H:\OPEN ORDERS

Kill .selecteditems(1)


End Sub

 
Reply With Quote
 
 
 
 
=?Utf-8?B?VmVyZ2VsIEFkcmlhbm8=?=
Guest
Posts: n/a
 
      22nd Apr 2007
Oggy,

try this slightly modified code.


Sub removeFileName()
Dim Filt As String
Dim FilterIndex As Integer
Dim FileName As Variant
Dim Title As String
Dim strSaveAsFile As String

' Set up list of file filters
Filt = "Text Files (*.txt),*.txt," & _
"Lotus Files (*.prn),*.prn," & _
"Comma Separated Files (*.csv),*.csv," & _
"ASCII Files (*.asc),*.asc," & _
"All Files (*.*),*.*"

' Display *.* by default
FilterIndex = 5

' Set the dialog box caption
Title = "Select a File to move"

' set directory
ChDir "D:\TEMP"

' Get the file name
FileName = Application.GetOpenFilename _
(FileFilter:=Filt, _
FilterIndex:=FilterIndex, _
Title:=Title)

' Exit if dialog box canceled
If FileName = False Then
MsgBox "No file was selected."
Exit Sub
End If

If Val(Application.Version) < 10 Then
MsgBox "This requires Excel 2002 or later.", vbCritical
Exit Sub
End If

With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a location for the PO"
.Show
If .SelectedItems.Count = 0 Then
MsgBox "Canceled"
Else
'Move the file
strSaveAsFile = StrReverse(FileName)
strSaveAsFile = StrReverse(Mid(strSaveAsFile, 1, InStr(1,
strSaveAsFile, "\")))
Name FileName As .SelectedItems(1) & strSaveAsFile
End If
End With

End Sub


--
Hope that helps.

Vergel Adriano


"Oggy" wrote:

>
>
> Hi
>
> I am trying to write a code that allows me to select a file and save
> it in a different location, and then delete the orginal file. I have
> the following code, that does not work, i think i may be over
> complicating it. Please Help!
>
>
>
>
> Sub remove FileName()
> Dim Filt As String
> Dim FilterIndex As Integer
> Dim FileName As Variant
> Dim Title As String
>
> ' Set up list of file filters
> Filt = "Text Files (*.txt),*.txt," & _
> "Lotus Files (*.prn),*.prn," & _
> "Comma Separated Files (*.csv),*.csv," & _
> "ASCII Files (*.asc),*.asc," & _
> "All Files (*.*),*.*"
>
> ' Display *.* by default
> FilterIndex = 5
>
> ' Set the dialog box caption
> Title = "Select a File to move"
>
>
> ' set directory
> Chdir H:\OPEN ORDERS
>
> ' Get the file name
> FileName = Application.GetOpenFilename _
> (FileFilter:=Filt, _
> FilterIndex:=FilterIndex, _
> Title:=Title)
>
> ' Exit if dialog box canceled
> If FileName = False Then
> MsgBox "No file was selected."
> Exit Sub
> End If
> WorkBook.Open filename: .selecteditems(1)
>
> If Val(Application.Version) < 10 Then
> MsgBox "This requires Excel 2002 or later.", vbCritical
> Exit Sub
> End If
> With Application.FileDialog(msoFileDialogFolderPicker)
> .InitialFileName = Application.DefaultFilePath & "\"
> .Title = "Please select a location for the PO"
> .Show
> If .SelectedItems.Count = 0 Then
> MsgBox "Canceled"
> Else
> WorkBook.SaveAs .SelectedItems(1)
> End If
> End With
>
>
> Chdir H:\OPEN ORDERS
>
> Kill .selecteditems(1)
>
>
> End Sub
>
>

 
Reply With Quote
 
Oggy
Guest
Posts: n/a
 
      22nd Apr 2007
Thank you this worked perfect

Regards

Oggy






On 22 Apr, 13:26, Vergel Adriano
<VergelAdri...@discussions.microsoft.com> wrote:
> Oggy,
>
> try this slightly modified code.
>
> Sub removeFileName()
> Dim Filt As String
> Dim FilterIndex As Integer
> Dim FileName As Variant
> Dim Title As String
> Dim strSaveAsFile As String
>
> ' Set up list of file filters
> Filt = "Text Files (*.txt),*.txt," & _
> "Lotus Files (*.prn),*.prn," & _
> "Comma Separated Files (*.csv),*.csv," & _
> "ASCII Files (*.asc),*.asc," & _
> "All Files (*.*),*.*"
>
> ' Display *.* by default
> FilterIndex = 5
>
> ' Set the dialog box caption
> Title = "Select a File to move"
>
> ' set directory
> ChDir "D:\TEMP"
>
> ' Get the file name
> FileName = Application.GetOpenFilename _
> (FileFilter:=Filt, _
> FilterIndex:=FilterIndex, _
> Title:=Title)
>
> ' Exit if dialog box canceled
> If FileName = False Then
> MsgBox "No file was selected."
> Exit Sub
> End If
>
> If Val(Application.Version) < 10 Then
> MsgBox "This requires Excel 2002 or later.", vbCritical
> Exit Sub
> End If
>
> With Application.FileDialog(msoFileDialogFolderPicker)
> .InitialFileName = Application.DefaultFilePath & "\"
> .Title = "Please select a location for the PO"
> .Show
> If .SelectedItems.Count = 0 Then
> MsgBox "Canceled"
> Else
> 'Move the file
> strSaveAsFile = StrReverse(FileName)
> strSaveAsFile = StrReverse(Mid(strSaveAsFile, 1, InStr(1,
> strSaveAsFile, "\")))
> Name FileName As .SelectedItems(1) & strSaveAsFile
> End If
> End With
>
> End Sub
>
> --
> Hope that helps.
>
> Vergel Adriano
>
>
>
> "Oggy" wrote:
>
> > Hi

>
> > I am trying to write a code that allows me to select a file and save
> > it in a different location, and then delete the orginal file. I have
> > the following code, that does not work, i think i may be over
> > complicating it. Please Help!

>
> > Sub remove FileName()
> > Dim Filt As String
> > Dim FilterIndex As Integer
> > Dim FileName As Variant
> > Dim Title As String

>
> > ' Set up list of file filters
> > Filt = "Text Files (*.txt),*.txt," & _
> > "Lotus Files (*.prn),*.prn," & _
> > "Comma Separated Files (*.csv),*.csv," & _
> > "ASCII Files (*.asc),*.asc," & _
> > "All Files (*.*),*.*"

>
> > ' Display *.* by default
> > FilterIndex = 5

>
> > ' Set the dialog box caption
> > Title = "Select a File to move"

>
> > ' set directory
> > Chdir H:\OPEN ORDERS

>
> > ' Get the file name
> > FileName = Application.GetOpenFilename _
> > (FileFilter:=Filt, _
> > FilterIndex:=FilterIndex, _
> > Title:=Title)

>
> > ' Exit if dialog box canceled
> > If FileName = False Then
> > MsgBox "No file was selected."
> > Exit Sub
> > End If
> > WorkBook.Open filename: .selecteditems(1)

>
> > If Val(Application.Version) < 10 Then
> > MsgBox "This requires Excel 2002 or later.", vbCritical
> > Exit Sub
> > End If
> > With Application.FileDialog(msoFileDialogFolderPicker)
> > .InitialFileName = Application.DefaultFilePath & "\"
> > .Title = "Please select a location for the PO"
> > .Show
> > If .SelectedItems.Count = 0 Then
> > MsgBox "Canceled"
> > Else
> > WorkBook.SaveAs .SelectedItems(1)
> > End If
> > End With

>
> > Chdir H:\OPEN ORDERS

>
> > Kill .selecteditems(1)

>
> > End Sub- Hide quoted text -

>
> - Show quoted text -



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
form & code allowing user to select location of associated file, Thardy Microsoft Access 3 9th Jan 2008 07:43 PM
Select File names and select a location of file =?Utf-8?B?cG9rZGJ6?= Microsoft Excel Programming 1 3rd Aug 2007 10:10 PM
How do I change the default Save Location in this code =?Utf-8?B?RHRvd24gRGF3Zw==?= Microsoft Outlook VBA Programming 2 15th Jul 2006 04:23 AM
Save File to Another Directory, but not change Users File Save location Mike Knight Microsoft Excel Programming 1 28th May 2004 09:06 PM
How to select location to save file after accidentally default to temporary file Wesley Windows XP Internet Explorer 1 20th Dec 2003 11:36 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:23 AM.