VBS Save File From Link

前端 未结 2 914
温柔的废话
温柔的废话 2021-01-25 01:56

I wonder whether someone can help me please.

I wanting to use this solution in a script I\'m trying to put together, but I\'m a little unsure about how to make a change

相关标签:
2条回答
  • 2021-01-25 02:32

    The code below shows how to retrieve the extension of a file, define an array with “allowed” extensions, and match the extension of the file to the array.

    This is the outline for file manipulation, you'll just need to tailor it to you needs

    Dim MinExtensionX
    Dim Arr() As Variant
    Dim lngLoc As Variant
    
    
    'Retrieve extension of file
    
      MinExtensionX = Mid(MyFile.Name, InStrRev(MyFile.Name, ".") + 1)
    
      Arr = Array("xls", "xlsx", "docx", "dat") 'define which extensions you want to allow
    
    On Error Resume Next
    
      lngLoc = Application.WorksheetFunction.Match(MinExtensionX, Arr(), 0)
    
    If Not IsEmpty(lngLoc) Then '
    
      'check which kind of extension you are working with and create proper obj manipulation 
      If MinExtensionX = "docx" then
    
         Set wApp = CreateObject("Word.Application")
         wApp.DisplayAlerts = False
         Set wDoc = wApp.Documents.Open (Filename:="C:\Documents\SomeWordTemplate.docx", ReadOnly:=True)
    
         'DO STUFF if it's an authorized file. Then Save file.
    
         With wDoc
    
              .ActiveDocument.SaveAs Filename:="C:\Documents\NewWordDocumentFromTemplate.docx"
    
         End With
    
         wApp.DisplayAlerts = True
    
         End if
    End If
    

    For files .Dat its a bit more complex, specially if you need to open/process data from the file, but this might help you out.

    Edit:

    2: Comments added

    Hi IRHM,

    I think you want something like this: 'Worksheet_FollowHyperlink' is an on click event that occurs every time you click on an Hyperlink within a Worksheet, You can find more here

    Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    
    'disable events so the user doesn't see the codes selection
    Application.EnableEvents = False
    
        Dim FSO
        Dim sFile As String
        Dim sDFolder As String
        Dim thiswb As Workbook ', wb As Workbook
    
        'Define workbooks so we don't lose scope while selecting sFile(thisworkbook = workbook were the code is located).
        Set thiswb = thisworkbook
        'Set wb = ActiveWorkbook ' This line was commented out because we no longer need to cope with 2 excel workbooks open at the same time.
    
        'Target.Range.Value is the selection of the Hyperlink Path. Due to the address of the Hyperlink being "" we just assign the value to a 
        'temporary variable which is not used so the Click on event is still triggers
        temp = Target.Range.Value
        'Activate the wb, and attribute the File.Path located 1 column left of the Hyperlink/ActiveCell
        thiswb.Activate
        sFile = Cells(ActiveCell.Row, ActiveCell.Column - 1).Value
    
        'Declare a variable as a FileDialog Object
        Dim fldr As FileDialog
        'Create a FileDialog object as a File Picker dialog box.
        Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    
        'Allow only single selection on Folders
        fldr.AllowMultiSelect = False
        'Show Folder picker dialog box to user and wait for user action
        fldr.Show
    
        'add the end slash of the path selected in the dialog box for the copy operation
        sDFolder = fldr.SelectedItems(1) & "\"
    
        'FSO System object to copy the file
        Set FSO = CreateObject("Scripting.FileSystemObject")
        ' Copy File from (source = sFile), destination , (Overwrite True = replace file with the same name)
        FSO.CopyFile (sFile), sDFolder, True
    
        ' check if there's multiple excel workbooks open and close workbook that is not needed
        ' section commented out because the Hyperlinks no longer Open the selected file
        ' If Not thiswb.Name = wb.Name Then
        '     wb.Close
        ' End If
    Application.EnableEvents = True
    
    End Sub
    

    The above code Triggers when you click the Hyperlink and it promps a folder selection window.

    You just need to paste the code into the Worksheet code. And you should be good to go.

    0 讨论(0)
  • 2021-01-25 02:35

    Miguel provided a fantastic solution which on initial testing appeared to work 100%. But as you will see from the comments at the end of the post there were some issues when the user cancelled the operation, so I made another post at this link where the problems were ironed out. Many thanks and kind regards. Chris

    0 讨论(0)
提交回复
热议问题