Moving a file in VBA (1 Viewer)

Lochwood

Registered User.
Local time
Today, 04:49
Joined
Jun 7, 2017
Messages
115
I have a button a form which is a file picker.
1. click the button it opens an explorer window.
2. choose the file, which then records the file chosen in an unbound field in the form called Text3403
3. looks at text3403 and moves the file to another location renaming it to datetimesecond.
4. populates the hyperlink Doc with same code as move file.

My issue is sometimes the hyperlink can be a second out.. how can i overcome this?

Heres my code..

On Error GoTo Err_Command3402_Click
'Declare a variable as a FileDialog object.

Dim fd As FileDialog

'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)

'Declare a variable to contain the path
'of each selected item. Even though the path is aString,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.

Dim vrtSelectedItem As Variant

'Use a With...End With block to reference the FileDialog object.

With fd

'Set the initial path

.InitialFileName = C:\Files\"

'Reference the FileDialog object.
'Allow the user to select only one file.
.AllowMultiSelect = False
' Show method to display the File Picker dialog box and return the user's action.
'The user pressed the action button.
If .Show = -1 Then
'Step through each string in the FileDialogSelectedItems collection.
For Each vrtSelectedItem In .SelectedItems

[Text3403] = vrtSelectedItem

Next vrtSelectedItem

'The user pressed Cancel.
Else
Set fd = Nothing
End If

End With

'Set the object variable to Nothing.
Set fd = Nothing
If Not IsNull(Me.Text3403) Then
MoveFile Me.Text3403, "C:\NewLocation\ " & Format(Now(), "yyyyMMddHHmmss") & ".pdf"
Me.Doc.Value = "C:\NewLocation\ " & Format(Now(), "yyyyMMddHHmmss") & ".pdf" (sometimes this is a second out and hyperlink doesnt work)
End If
Exit_Command3402_Click:
Exit Sub
Err_Command3402_Click:
MsgBox Err.Description
Resume Exit_Command3402_Click
End Sub
 

moke123

AWF VIP
Local time
Today, 07:49
Joined
Jan 11, 2013
Messages
2,237
set a variable to Format(Now(), "yyyyMMddHHmmss") and use the variable as the name.
Code:
dim dteName as string

If Not IsNull(Me.Text3403) Then
dteName = Format(Now(), "yyyyMMddHHmmss")
MoveFile Me.Text3403, "C:\NewLocation\ " & dteName & ".pdf"
Me.Doc.Value = "C:\NewLocation\ " & dteName & ".pdf"
End If
 
Last edited:

Users who are viewing this thread

Top Bottom