gpt4 book ai didi

使用VBS浏览本地文件的3种方式,获取完整路径

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章使用VBS浏览本地文件的3种方式,获取完整路径由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

因为系统组件差异,所以代码也有不同文章最后我给出一个通用的代码 。

XP下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Function BrowseForFile( )
' Select File dialog based on a script by Mayayana
' Known issues:
' * Tree view always opens Desktop folder
' * In Win7/IE8 only the file NAME is returned correctly, the path returned will always be C:\fakepath\
' * If a shortcut to a file is selected, the name of that FILE will be returned, not the shortcut's
'On Error Resume Next 
'===========1, file browserer in XP ==================
Set objDialog = CreateObject( "UserAccounts.CommonDialog" )
objDialog.Filter = "txt|*.txt"
objDialog.InitialDir = "C:\"
intResult = objDialog.ShowOpen
If intResult <> 0 Then
     BrowseForFile = objDialog.FileName
     exit function
else
     msgbox "Error."
End If
end function
BrowseForFile

2003下'for windows 2003 。

?
1
2
3
4
5
Function SelectAFile
Set objDialog = CreateObject( "SAFRCFileDlg.FileOpen" )
intResult = objDialog.OpenFileOpenDlg
SelectAFile = objDialog.FileName
End Function

浏览器方式:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Function BrowseForFile( )
  '===========2, fileselect in IE ======================
  'Another way to get file path
   Dim objIE, strSelected
   BrowseForFile = ""
   Set objIE = CreateObject( "InternetExplorer.Application" )
objIE.ToolBar = False
objIE.Resizable = False
objIE.StatusBar = False
objIE.Width = 300
objIE.Height = 100
   objIE.visible = True
   objIE.Navigate( "about:blank" )
   Do Until objIE.ReadyState = 4
   Loop
  ' Center the dialog window on the screen
  With objIE.Document.ParentWindow.Screen
  objIE.Left = (.AvailWidth - objIE.Width ) \ 4
  objIE.Top = (.Availheight - objIE.Height) \ 4
  End With
   objIE.Document.Write "<HTML><BODY><INPUT ID=" "FileSelect" " NAME=" "FileSelect" " TYPE=" "file" "><BODY></HTML>"
   With objIE.Document.all.FileSelect
     .focus
     .click
     strSelected = .value
   End With
   objIE.Quit
   Set objIE = Nothing
  If Trim(strSelected) = "" Then
  Msgbox "You selected no file."
  Wscript.Quit
  End If
  BrowseForFile = strSelected
End Function
 
BrowseForFile

有没有发现上面的代码运行不易啊,这里我小编为大家分享一个可用的,虽然上面的的代码不能用但学习参考一下还是不错的,现在因为平台愿意导致,原来这些代码都是可以使用的.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Function ChooseFile()
    Dim Result
    Result = ""
    Dim IE : Set IE = CreateObject( "InternetExplorer.Application" )
    With IE
      .Visible = False
      .Navigate( "about:blank" )
      Do Until .ReadyState = 4 : Loop
      With .Document
        .Write "<html><body><input id='f' type='file'></body></html>"
        With .All.f
          .Focus
          .Click
          Result = .Value
        End With
      End With
      .Quit
    End With
    Set IE = Nothing
   ChooseFile = Result
End Function
ChooseFile

好了这篇 关于vbs选择本地文件功能的代码就介绍到这了,需要的朋友可以参考一下.

最后此篇关于使用VBS浏览本地文件的3种方式,获取完整路径的文章就讲到这里了,如果你想了解更多关于使用VBS浏览本地文件的3种方式,获取完整路径的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com