2016-08-25 4 views

答えて

0

プロジェクトにFileSystemObjectオブジェクト

を活用するために、「Microsoftスクリプトランタイム」ライブラリへの参照を追加するには、このコードを使用します

Option Explicit 

Sub main() 
    Dim cell As Range 
    Dim currenthWbPath As String, oldName As String 
    Dim fso As New FileSystemObject 
    Dim file As file 
    Dim hyp As Hyperlink 

    With ActiveWorkbook '<--| refer to currently active workbook 
     currenthWbPath = .Path '<--| store its full path 
     With .Worksheets("pics") '<--| refer to its "pics" worksheet (change it as per your needs) 
      For Each cell In .Range("B1", .Cells(.Rows.Count, 2).End(xlUp)).SpecialCells(XlCellType.xlCellTypeConstants) '<--| loop through referenced sheet column "B" cells with values in it 
       If cell.Hyperlinks.Count > 0 Then '< if current cell contains a hyperlink 
        Set hyp = cell.Hyperlinks.Item(1) '<-- stote the first hyperlink object associated to the cell 
        If fso.FileExists(currenthWbPath & "\" & hyp.Address) Then '<--| if the hyperlink leads to an existent file 
         Set file = fso.GetFile(currenthWbPath & "\" & hyp.Address) '<--| get the file corresponding to hyperlink 
         oldName = file.name '<-- store old name 
         file.name = cell.Offset(, -1) & "." & fso.GetExtensionName(file.Path) '<--| rename the file 
         hyp.Address = Replace(hyp.Address, oldName, file.name) '<--| refresh hyperlink address 
         hyp.TextToDisplay = cell.Hyperlinks.Item(1).Address '<--| refresh hyperlink text to display 
        Else 
         ' code to deal with invalid hyperlinks 
        End If 
       End If 
      Next cell 
     End With 
    End With 
End Sub 
+0

@yuyuを:あなたはそれを介して取得しましたか? – user3598756

+0

ありがとうございました! –

+0

エラーが発生しました!Array Index Of Bounds!あなたはリンクをたどることができます:http://pan.baidu.com/s/1i5qa2Mhキー:1xra –

関連する問題