【问题标题】:VBS Save File From LinkVBS 从链接保存文件
【发布时间】:2015-04-17 06:22:21
【问题描述】:

我想知道是否有人可以帮助我。

我想在我尝试组合的脚本中使用this 解决方案,但我有点不确定如何进行需要进行的更改。

您会在解决方案中看到打开的文件类型是 Excel,并且确实是这样保存的。但我要打开和保存的文件是 .docx 和 .dat(由 Dragon 软件使用)文件的混合。

谁能告诉我,有没有一种方法可以让我修改代码,以便它以 Excel 工作簿以外的文件类型打开和保存文件。

这个问题背后的原因是因为我目前正在使用一个脚本,该脚本在给定文件夹的 Excel 电子表格中创建文件列表。对于检索到的每个文件,都有一个超链接,我想为其添加功能,使用户能够复制文件并将其保存到他们选择的位置。

为了帮助解决这个问题,我使用了代码来创建文件列表。

Public Sub ListFilesInFolder(SourceFolder As Scripting.folder, IncludeSubfolders As Boolean)
    Dim LastRow As Long
    Dim fName As String
    On Error Resume Next

    For Each FileItem In SourceFolder.Files
        ' display file properties
        Cells(iRow, 3).Formula = iRow - 12
        Cells(iRow, 4).Formula = FileItem.Name
        Cells(iRow, 5).Formula = FileItem.Path
        Cells(iRow, 6).Select
        Selection.Hyperlinks.Add Anchor:=Selection, Address:= _
        FileItem.Path, TextToDisplay:="Click Here to Open"
        iRow = iRow + 1 ' next row number

        With ActiveSheet
        LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
        LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    End With

For Each Cell In Range("C13:F" & LastRow) ''change range accordingly
    If Cell.Row Mod 2 = 1 Then ''highlights row 2,4,6 etc|= 0 highlights 1,3,5
        Cell.Interior.Color = RGB(232, 232, 232) ''color to preference
    Else
        Cell.Interior.Color = RGB(141, 180, 226) 'color to preference or remove
    End If
Next Cell

    Next FileItem


    If IncludeSubfolders Then
        For Each SubFolder In SourceFolder.SubFolders
            ListFilesInFolder SubFolder, True
        Next SubFolder
    End If
    Set FileItem = Nothing
    Set SourceFolder = Nothing
    Set FSO = Nothing
End Sub

非常感谢和亲切的问候

克里斯

【问题讨论】:

    标签: excel excel-2013 vba


    【解决方案1】:

    Miguel 提供了一个出色的解决方案,在初始测试中该解决方案似乎 100% 有效。但是正如您从帖子末尾的 cmets 中看到的那样,当用户取消操作时出现了一些问题,所以我在 link 发了另一个帖子,问题得到了解决。非常感谢和亲切的问候。克里斯

    【讨论】:

      【解决方案2】:

      下面的代码展示了如何检索文件的扩展名,定义一个带有“允许”扩展名的数组,并将文件的扩展名与数组匹配。

      这是文件操作的大纲,您只需要根据需要对其进行调整

      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
      

      对于文件 .Dat 它有点复杂,特别是如果您需要打开/处理文件中的数据,但this 可能会帮助您。

      编辑:

      2:添加评论

      嗨,IRHM,

      我想你想要这样的东西: 'Worksheet_FollowHyperlink' 是每次单击工作表中的超链接时发生的单击事件,您可以找到更多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
      

      上面的代码在你点击超链接时触发,它会提示一个文件夹选择窗口。

      您只需将代码粘贴到工作表代码中。你应该很高兴。

      【讨论】:

      • 嗨@Miguel_Ryu,非常感谢你,我很抱歉可能没有说明我的情况。我的电子表格的 E 列中有一个文件路径列表,F 列中有一个超链接,每个文件可以是 .docx 或 .dat。单击超链接时我希望这样做是打开一个对话框,允许用户保存文件,所以我希望修改这段代码。 Set wb = Workbooks.Open(hlink.Address) wb.SaveAs saveloc & hlink.Parent & ".xlsx" wb.Close True Set wb = Nothing 非常感谢和亲切的问候
      • 您好 IRHM,您需要打开并保存文件还是只保存文件?
      • 嗨@Miguel_Ryu 谢谢你回来找我。我不需要打开文件,我只需要保存文件。如果它有帮助,我已经在我的原始帖子中发布了代码,我用它来创建带有超链接的列表。非常感谢和亲切的问候。克里斯
      • 嗨 IRHM,你想试试我输入的编辑吗,它不是你想要的,但我认为这就是你需要的。米格尔
      • 嗨@Miguel_Ryu,这太棒了,非常感谢。但是你能告诉我有没有办法可以阻止文件打开。只是我所说的某些文件是 .dat 文件,无法自行打开,例如 Word 文档。非常感谢和亲切的问候。克里斯
      猜你喜欢
      • 2016-12-25
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      相关资源
      最近更新 更多