热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

VB如何跳出选择文件对话框?

PrivateDeclareFunctionFindWindowLibuser32AliasFindWindowA(_ByVallpClassNameA
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
        ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( _
        ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
        
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long

Private Const GWL_STYLE As Long = (-16)
Private Const WS_CAPTION As Long = &HC00000

Private Sub Form_Load()
       Dim hwnd&
    '  Dim xlApp As New excel.application'如果在工程中引用了EXCEL下句声明不要
       Dim xlApp As Object
        If IsFileOpen(App.Path & "\" & Dir("*.dll")) Then _
            MsgBox App.EXEName & " 文件已经打开!", vbInformation, "系统提醒:": GoTo ooo
        
        DoEvents
        
       Set xlApp = CreateObject("Excel.Application") '如果在工程中引用了EXCEL这句可不要
        
        xlApp.Workbooks.Open (App.Path & "\" & Dir("*.dll"))
        
        hwnd = FindWindow(vbNullString, xlApp.Caption)
        SetWindowLong hwnd, GWL_STYLE, IStyle
        DrawMenuBar hwnd
      
        xlApp.Visible = True
      ' xlApp.WindowState = xlMaximized '用createobject函数的话这两句要出错
      ' xlApp.ActiveWindow.WindowState = xlMaximized
         
        Set xlApp = Nothing
ooo:
        Unload Me
       End
End Sub

Function IsFileOpen(filename As String)
       Dim filenum As Integer, errnum As Integer
       On Error Resume Next
       filenum = FreeFile()
        
       Open filename For Input Lock Read As #filenum
       Close filenum
       
       errnum = Err
       On Error GoTo 0
       
       Select Case errnum
           Case 0
               IsFileOpen = False
           Case 70
               IsFileOpen = True
           Case Else
               Error errnum
       End Select
End Function


现在的源码是打开文件所在路径里面的文件,但是无法选择某个文件,
我的要求是打开对话框后选择文件,点击确定就可打开.
还有一个问题是,现在的源码打开excel文件后,蓝色标题被隐藏,应该删除那句代码?
×注(源码中的DLL文件是excel文件伪装的)

9 个解决方案

#1


 添加通用对话框吧

#2


部件里选择“Microsoft Common dialog control 6.0”

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Form_Load()

        On Error GoTo ms:
        With Me.CommonDialog1
            .Filter = "所有文件|*.*"
            .ShowOpen
        End With
        
        Dim strFileName  As String
        strFileName = Me.CommonDialog1.filename
        If Len(strFileName) > 0 And Dir(strFileName) <> "" Then
            ShellExecute Me.hWnd, "Open", strFileName, vbNullString, vbNullString, 1
        End If
          
ms:
      
End Sub

#3


学习下

#4


可以试试excel VBA里的application.openfilename

dim fn as string
fn=xlApp.openfilename

#5


如果要求大部分系统都能有,就用api吧
很多系统没comdlg32.ocx,但comdlg32.dll肯定有的,我昨天正好研究过,搜了些代码


'========================打开/保存对话框 API 函数及结构===================
Private Type tagOPENFILENAME
   lStructSize As Long        '结构大小
   hwndOwner As Long          '
   hInstance As Long          '
   strFilter As String        '过滤器字符串
   strCustomFilter As String '选中的过滤器(过滤器索引所指的过滤器)字符串
   nMaxCustFilter As Long     '过滤器最大长度
   nFilterIndex As Long       '选中的过滤器索引,意义与 CommonDialog 控件相同
   strFile As String          '选中的全路径文件名
   nMaxFile As Long           '装载全路径文件名的字符串长度
   strFileTitle As String     '去掉了路径的文件名
   nMaxFileTitle As Long      '装载去掉了路径的文件名字符串长度
   strInitialDir As String    '去掉了文件名的路径(没有最后的反斜杠)
   strTitle As String         '对话框标题,意义与 CommonDialog 控件相同
   flags As Long              '标志,意义与 CommonDialog 控件相同
   nFileOffset As Integer     '路径长度(包括最后的反斜杠)
   nFileExtension As Integer '全路径文件名长度(不计算前面 3 个表示盘符的字符,如 D:\)
   strDefExt As String        '默认提取
   lCustData As Long          '
   lpfnHook As Long           '勾子函数地址
   lpTemplateName As String   '
End Type

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (ofn As tagOPENFILENAME) As Boolean
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (ofn As tagOPENFILENAME) As Boolean


'返回选择的文件名。输入参数:
'1.对话框类型(0=保存,1=打开);2.对话框标题;3.过滤器字符串
'4.过滤器索引;5.标志;6.路径;7.文件名
Public Function CmdDlg(Optional ByVal DlgType As Boolean = True, _
   Optional ByVal DialogTitle As String, Optional ByVal Filter As String, _
   Optional FilterIndex As Long = 1, Optional flags As Long, _
   Optional ByVal InitialDir As String, Optional ByVal FileName As String, Optional ByRef hWnd As Long = 0) As String
  
On Error GoTo CmdDlg_Error
Dim ofn As tagOPENFILENAME
Dim fResult As Boolean
If InitialDir = "" Then InitialDir = CurDir
If Len(Filter) > 0 Then Filter = Replace(Filter, "|", vbNullChar) 'Filter以Chr(0)为分隔符

With ofn
   .lStructSize = Len(ofn)
   .hwndOwner = hWnd                                   '0为屏幕句柄
   .strFilter = Filter
   .nFilterIndex = FilterIndex
   .strFile = Left(FileName & String$(255, 0), 255)   '用空字符补足全路径文件名255字节
   .nMaxFile = 255                                    '全路径文件名长度
   .strFileTitle = String$(255, 0)                    '用空字符填充(去掉路径的)文件名
   .nMaxFileTitle = 255                               '(去掉路径的)文件名长度
   .strTitle = DialogTitle                            '对话框标题
   .flags = flags
   .strDefExt = ""
   .strInitialDir = InitialDir
   .hInstance = 0
   .strCustomFilter = String(255, 0)                  '用空字符填充选中的过滤器
   .nMaxCustFilter = 255                              '选中的过滤器长度
   .lpfnHook = 0
End With
If DlgType Then fResult = GetOpenFileName(ofn) Else fResult = GetSaveFileName(ofn)
If fResult Then
   CmdDlg = Left(ofn.strFile, InStr(ofn.strFile, vbNullChar) - 1)
   'FilterIndex = ofn.nFilterIndex                    '返回选中的过滤器索引
Else
   CmdDlg = vbNullChar
End If
CmdDlg_Error:
End Function


#6


本帖最后由 bcrun 于 2011-03-31 19:46:36 编辑
With CreateObject("MSComDlg.CommonDialog")
 .showopen
 If .FileName <> "" Then
  '在此加上选中文件的处理操作
 End If
End With

#7


本帖最后由 bcrun 于 2011-03-31 19:47:00 编辑
vba code:
Application.GetOpenFilename
麻烦楼主结帖!!!

#8


通用对话框

#9


牛人,就如此简单!
引用 7 楼 fzx4936 的回复:
vba code:
Application.GetOpenFilename
麻烦楼主结帖!!!

推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文探讨了Hive中内部表和外部表的区别及其在HDFS上的路径映射,详细解释了两者的创建、加载及删除操作,并提供了查看表详细信息的方法。通过对比这两种表类型,帮助读者理解如何更好地管理和保护数据。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • 2023年京东Android面试真题解析与经验分享
    本文由一位拥有6年Android开发经验的工程师撰写,详细解析了京东面试中常见的技术问题。涵盖引用传递、Handler机制、ListView优化、多线程控制及ANR处理等核心知识点。 ... [详细]
  • IneedtofocusTextCellsonebyoneviaabuttonclick.ItriedlistView.ScrollTo.我需要通过点击按钮逐个关注Tex ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
author-avatar
秋夜里的寂寞
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有