我编写了一个使用Beforedoubleclick()范围的宏:双击第5列的单元格,然后该宏进入特定的文件夹中,以查找在单元格内编写有代码的pdf文件。
此宏无法扫描子文件夹,但我的pdf文件可能位于子文件夹中。
我在网上搜索过,看来我必须使用循环或类似的内容。我不知道如何编写这段代码。
我的宏:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
Dim testo As String
Dim nomefile As String
Dim path As String
On Error Resume Next
If Target.Column = 5 Then
path = "C:\Users\Alex\"
testo = path & Cells(Target.Row, 5)
nomefile = Dir(Left(testo, Len(testo)) & "*.pdf")
If nomefile = "" Then
MsgBox "File non trovato", vbCritical, "ATTENZIONE"
Exit Sub
End If
Do
Shell "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe " & path & nomefile, vbMaximizedFocus
nomefile = Dir
Loop While nomefile <> ""
End If
End Sub
答案 0 :(得分:0)
尝试这样的事情:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
Dim testo As String
Dim FSO As Object
Dim Folder As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder("C:\Users\Alex\")
...
...
testo = path & Cells(Target.Row, 5)
nomefile = FindFile(testo, Folder, FSO)
End Sub
Function FindFile(FileName As String, Folder As Object, ByRef FSO As Object) As String
DoEvents
If FSO.FileExists(Folder & "\" & FileName & ".pdf") Then
FindFile = Folder & "\" & FileName & ".pdf"
Exit Function
End If
Dim SubFolder As Object
For Each SubFolder In Folder.SubFolders
FindFile = FindFile(FileName, SubFolder, FSO)
Next
End Function
FindFile函数以文件夹开头。我要的文件在那里,很好-退回它。如果不是,请使用每个子文件夹并调用FindFile并尝试。 DoEvents
在那里,因为否则很难中断。
答案 1 :(得分:0)
感谢您的建议,山姆;我尝试根据您的情况调整您的代码,并写了这样的代码:
.kuguar-sport-color {
background-color: rgba(227, 30, 36, 1);
}
.nav-center {
display: flex;
justify-content: center;
}
.nav-margin {
margin-left: 25%;
margin-right: 25%;
}
.nav-btn-clear {
border-width: 0;
border-style: none;
border-color: transparent;
border-image: none;
background-color: transparent;
outline: none !important;
box-shadow: none !important;
}
.nav-btn:hover {
background-color: rgba(227, 30, 36, 0.5); /*.kuguar-sport-color with opacity*/
}
.nav-padding {
padding-left: 25%;
padding-right: 25%;
}
.nav-center-nested {
position: absolute !important;
top: 100%;
left: 50%;
transform: translate(-50%, 0);
z-index: 998;
width: 100%;
}
结束功能
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="categories" class="col kuguar-sport-color no-padding no-margin">
<nav id="main-nav">
<div class="row nav-margin p-3 text-center"><div name="nav-btn" class="nav-btn col" data-v="138"><button class="nav-btn-clear text-light"> Aktualności</button></div><div name="nav-btn" class="nav-btn col" data-v="139"><button class="nav-btn-clear text-light"> O nas</button></div><div name="nav-btn" class="nav-btn col" data-v="140"><button class="nav-btn-clear text-light"> Rowery</button></div><div class="position-relative w-100"><div class="nav-center-nested row no-margin kuguar-sport-color text-center"><div style="display: none;" class="col" data-p="140" data-v="368"><div class="d-inline"><a class="text-light" href="#"> Górskie</a></div><div class="d-inline"><a class="text-light" href="#"> testgorskie</a></div><div class="d-inline"><a class="text-light" href="#"> testgorskie2</a></div><div class="d-inline"><a class="text-light" href="#"> testgorskie3</a></div><div class="d-inline"><a class="text-light" href="#"> testgorskie1-1</a></div><div class="d-inline"><a class="text-light" href="#"> testgorskie1-2</a></div></div><div class="w-100"></div><div style="display: none;" class="col" data-p="140" data-v="369"><div class="d-inline"><a class="text-light" href="#"> Miejskie</a></div></div><div class="w-100"></div><div style="display: none;" class="col" data-p="140" data-v="370"><div class="d-inline"><a class="text-light" href="#"> Dziecięce</a></div></div><div class="w-100"></div></div></div></div>
</nav>
</div>
结束子
问题是我要查找的文件(名为“ Test.pdf”)不直接位于“ Alex”文件夹中,而是位于子文件夹(... \ Alex \ TestFolder \“)中。 宏总是返回MsgBox“找不到文件!”。 我的代码有什么问题?
谢谢, 亚历克斯