首页 > 其他分享 >cad vba 打开excel并弹窗打开指定文件

cad vba 打开excel并弹窗打开指定文件

时间:2024-03-21 21:58:38浏览次数:29  
标签:Function vba End String excel Long Chr Private 打开

 CAD vba 代码实现打开excel,并通过对话框选择xls文件,并打开此文件进行下一步操作。代码如下:


Option Explicit
#If VBA7 Then
Private Declare PtrSafe Function ts_apiGetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (tsFN As tsFileName) As Boolean
Private Declare PtrSafe Function ts_apiGetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (tsFN As tsFileName) As Boolean
Private Declare PtrSafe Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
Private Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" Alias _
"SHGetPathFromIDListA" (ByVal pidl As LongPtr, ByVal pszPath As String) As LongPtr
Private Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" Alias _
"SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As LongPtr
Private Const BIF_RETURNONLYFSDIRS = &H1
Private Type BROWSEINFO
            hOwner As LongPtr
            pidlRoot As LongPtr
            pszDisplayName As String
            lpszTitle As String
            ulFlags As LongPtr
            lpfn As LongPtr
            lParam As LongPtr
            iImage As LongPtr
End Type
Private Type tsFileName
   lStructSize As Long
   hwndOwner As LongPtr
   hInstance As LongPtr
   strFilter As String
   strCustomFilter As String
   nMaxCustFilter As Long
   nFilterIndex As Long
   strFile As String
   nMaxFile As Long
   strFileTitle As String
   nMaxFileTitle As Long
   strInitialDir As String
   strTitle As String
   flags As Long
   nFileOffset As Integer
   nFileExtension As Integer
   strDefExt As String
   lCustData As Long
   lpfnHook As LongPtr
   lpTemplateName As String
End Type
 
' Flag Constants
Private Const tscFNAllowMultiSelect = &H200
Private Const tscFNCreatePrompt = &H2000
Private Const tscFNExplorer = &H80000
Private Const tscFNExtensionDifferent = &H400
Private Const tscFNFileMustExist = &H1000
Private Const tscFNPathMustExist = &H800
Private Const tscFNNoValidate = &H100
Private Const tscFNHelpButton = &H10
Private Const tscFNHideReadOnly = &H4
Private Const tscFNLongNames = &H200000
Private Const tscFNNoLongNames = &H40000
Private Const tscFNNoChangeDir = &H8
Private Const tscFNReadOnly = &H1
Private Const tscFNOverwritePrompt = &H2
Private Const tscFNShareAware = &H4000
Private Const tscFNNoReadOnlyReturn = &H8000
Private Const tscFNNoDereferenceLinks = &H100000
 
Public Function GOFN( _
    Optional ByRef rlngflags As Long = 0&, _
    Optional ByVal strInitialDir As String = "", _
    Optional ByVal strFilter As String = "dwg文件 (*.dwg)" & vbNullChar & "*.dwg" _
    & vbNullChar & "All Files (*.*)" & vbNullChar & "*.*", _
    Optional ByVal lngFilterIndex As Long = 1, _
    Optional ByVal strDefaultExt As String = "", _
    Optional ByVal strFileName As String = "", _
    Optional ByVal strDialogTitle As String = "", _
    Optional ByVal fOpenFile As Boolean = True) As Variant
'On Error GoTo GOFN_Err
    
    Dim tsFN As tsFileName
    Dim strFileTitle As String
    Dim fResult As Boolean
 
    ' Allocate string space for the returned strings.
    strFileName = Left(strFileName & String(256, 0), 256)
    strFileTitle = String(256, 0)
 
    ' Set up the data structure before you call the function
    With tsFN
        .lStructSize = LenB(tsFN)
        '.hwndOwner = Application.hWndAccessApp
        .strFilter = strFilter
        .nFilterIndex = lngFilterIndex
        .strFile = strFileName
        .nMaxFile = Len(strFileName)
        .strFileTitle = strFileTitle
        .nMaxFileTitle = Len(strFileTitle)
        .strTitle = strDialogTitle
        .flags = rlngflags
        .strDefExt = strDefaultExt
        .strInitialDir = strInitialDir
        .hInstance = 0
        .strCustomFilter = String(255, 0)
        .nMaxCustFilter = 255
        .lpfnHook = 0
    End With
   
    ' Call the function in the windows API
   
        fResult = ts_apiGetOpenFileName(tsFN)
    If fResult Then
        rlngflags = tsFN.flags
        GOFN = tsTrimNull(tsFN.strFile)
    Else
        GOFN = Null
        MsgBox "您未选择"
        End
    End If
 
End Function
Public Function GSFN( _
    Optional ByRef rlngflags As Long = 0&, _
    Optional ByVal strInitialDir As String = "", _
    Optional ByVal strFilter As String = "dwg文件 (*.dwg)" & vbNullChar & "*.dwg" _
    & vbNullChar & "All Files (*.*)" & vbNullChar & "*.*", _
    Optional ByVal lngFilterIndex As Long = 1, _
    Optional ByVal strDefaultExt As String = "", _
    Optional ByVal strFileName As String = "", _
    Optional ByVal strDialogTitle As String = "", _
    Optional ByVal fOpenFile As Boolean = False) As Variant
'On Error GoTo tsGetFileFromUser_Err
    
    Dim tsFN As tsFileName
    Dim strFileTitle As String
    Dim fResult As Boolean
 
    ' Allocate string space for the returned strings.
    strFileName = Left(strFileName & String(256, 0), 256)
    strFileTitle = String(256, 0)
 
    ' Set up the data structure before you call the function
    With tsFN
        .lStructSize = LenB(tsFN)
        '.hwndOwner = Application.hWndAccessApp
        .strFilter = strFilter
        .nFilterIndex = lngFilterIndex
        .strFile = strFileName
        .nMaxFile = Len(strFileName)
        .strFileTitle = strFileTitle
        .nMaxFileTitle = Len(strFileTitle)
        .strTitle = strDialogTitle
        .flags = rlngflags
        .strDefExt = strDefaultExt
        .strInitialDir = strInitialDir
        .hInstance = 0
        .strCustomFilter = String(255, 0)
        .nMaxCustFilter = 255
        .lpfnHook = 0
    End With
   
        fResult = ts_apiGetSaveFileName(tsFN)
    If fResult Then
        rlngflags = tsFN.flags
        GSFN = tsTrimNull(tsFN.strFile)
    Else
        GSFN = Null
        MsgBox "您未保存"
        End
    End If
 
End Function
 
' Trim Nulls from a string returned by an API call.
Private Function tsTrimNull(ByVal strItem As String) As String
On Error GoTo tsTrimNull_Err
    Dim I As Integer
   
    I = InStr(strItem, vbNullChar)
    If I > 0 Then
        tsTrimNull = Left(strItem, I - 1)
    Else
        tsTrimNull = strItem
    End If
    
tsTrimNull_End:
    On Error GoTo 0
    Exit Function
 
 
tsTrimNull_Err:
    Beep
    MsgBox Err.Description, , "Error: " & Err.Number _
    & " in function basBrowseFiles.tsTrimNull"
    Resume tsTrimNull_End
 
End Function

Public Function GOFOLDER() As String
On Error GoTo Err_GOFOLDER
    Dim x As LongPtr, bi As BROWSEINFO, dwIList As LongPtr
    Dim szPath As String, wPos As Integer
   
    With bi
        '.hOwner = hWndAccessApp
        .lpszTitle = "请选择文件夹"
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With
   
    dwIList = SHBrowseForFolder(bi)
    szPath = Space$(512)
    x = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)
   
    If x Then
        wPos = InStr(szPath, Chr(0))
        GOFOLDER = Left$(szPath, wPos - 1)
    Else
        GOFOLDER = ""
        MsgBox "您未选择"
        End
    End If
Exit_GOFOLDER:
    Exit Function
Err_GOFOLDER:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_GOFOLDER
End Function
#Else
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (lpofn As OPENFILENAME) As Long
Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (lpofn As OPENFILENAME) As Long
Declare Function SHBrowseForFolder Lib "SHELL32.DLL" (lpBrowseInfo As BROWSEINFO) As Long
Declare Function SHGetPathFromIDList Lib "SHELL32.DLL" (ByVal pidl As Long, ByVal pszPath As String) As Long
Public choice As String
Type OPENFILENAME
     lStructSize As Long
     hwndOwner As Long
     hInstance As Long
     lpstrFilter As String
     lpstrCustomFilter As String
     nMaxCustFilter As Long
     nFilterIndex As Long
     lpstrFile As String
     nMaxFile As Long
     lpstrFileTitle As String
     nMaxFileTitle As Long
     lpstrInitialDir As String
     lpstrTitle As String
     flags As Long
     nFileOffset As Integer
     nFileExtension As Integer
     lpstrDefExt As String
     lCustData As Long
     lpfnHook As Long
     lpTemplateName As String
End Type
Public Type BROWSEINFO
    hOwner As Long
    pidlRoot As Long
    pszDisplayName As String
    lpszTitle As String
    ulFlags As Long
    lpfn As Long
    lParam As Long
    iImage As Long
End Type

Function GOFOLDER(Optional message) As String
Dim bInfo As BROWSEINFO
Dim path As String
Dim r As Long, x As Long, pos As Integer
bInfo.pidlRoot = 0
bInfo.lpszTitle = ""
bInfo.ulFlags = &H1
x = SHBrowseForFolder(bInfo)
path = Space$(256)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
    pos = InStr(path, Chr(0))
    GOFOLDER = Left(path, pos - 1)
Else
    GOFOLDER = ""
    MsgBox "您未选择"
    End
End If
End Function
Function GOFN() As String
    Dim sOFN As OPENFILENAME
    With sOFN
        .lStructSize = Len(sOFN)
        
       .lpstrFilter = "dwg文件(*.dwg)" & Chr(0) & "*.dwg" & Chr(0) & "dxf文件(*.dxf)" & Chr(0) & "*.dxf" & Chr(0) & "Excel文件(*.xl*)" & Chr(0) & "*.xl*" & Chr(0) & "Word文件(*.do*)" _
        & Chr(0) & "*.do*" & Chr(0) & "PPT文件(*.pp*)" & Chr(0) & "*.pp*" & Chr(0) & "所有文件(*.*)" & Chr(0) & "*.*" _
        & Chr(0) & Chr(0)
        .lpstrFile = Space(1024)
        .nMaxFile = 1025
    End With
    Dim sFileName As String

    If GetOpenFileName(sOFN) <> 0 Then
        With sOFN
            sFileName = Trim(.lpstrFile)
            GOFN = Left(sFileName, Len(sFileName) - 1)
        End With
    Else
        GOFN = ""
          MsgBox "您已取消,请重新选择"
        End
    End If
End Function
Function GSFN() As String
    Dim sSFN As OPENFILENAME
    With sSFN
        .lStructSize = Len(sSFN)
        '设置保存文件对话框中的文件筛选字符串对
       .lpstrFilter = "dwg文件(*.dwg)" & Chr(0) & "*.dwg" & Chr(0) & "dxf文件(*.dxf)" & Chr(0) & "*.dxf" & Chr(0) & "Excel文件(*.xl*)" & Chr(0) & "*.xl*" & Chr(0) & "Word文件(*.do*)" _
        & Chr(0) & "*.do*" & Chr(0) & "PPT文件(*.pp*)" & Chr(0) & "*.pp*" & Chr(0) & "所有文件(*.*)" & Chr(0) & "*.*" _
        & Chr(0) & Chr(0)
        '设置文件完整路径和文件名的缓冲区
        .lpstrFile = Space(1024)
        '设置文件完整路径和文件名的最大字符数,一定要比lpstrFile参数指定的字符数多1,用于存储结尾Null字符
        .nMaxFile = 1025
    End With
     
    Dim sFileName As String
    If GetSaveFileName(sSFN) <> 0 Then
        With sSFN
            sFileName = Trim(.lpstrFile)
            GSFN = Left(sFileName, Len(sFileName) - 1)
        End With
    Else
        GSFN = ""
        MsgBox "您已取消,请重新选择"
        End
       
    End If
'    Debug.Print GSFN, Len(GSFN)

End Function
#End If



Sub CAD打开excel_cadvba实现()
Dim excel As Object
Dim excelSheet As Object
    ' Start Excel
    On Error Resume Next
    
    Set excel = GetObject(, "Excel.Application")
    
    If Err <> 0 Then
        Err.Clear
        Set excel = CreateObject("Excel.Application")
            
        If Err <> 0 Then
            MsgBox "Could not load Excel.", vbExclamation
            End
        End If
    End If
    excel.Visible = True
'    MsgBox GOFN
    excel.Workbooks.Open FileName:=GOFN
'    On Error GoTo errorcontrol
'errorcontrol: MsgBox Err.Number & " - " & Err.Description
'End

End Sub

标签:Function,vba,End,String,excel,Long,Chr,Private,打开
From: https://blog.csdn.net/yongshiqq/article/details/136921927

相关文章

  • 从数据库查询数据并导出到excel
    importpymysqlimportdatetimeimportxlwtimportosimportpandasaspddefexport_excel(output_dir):current_datetime=datetime.datetime.now()#将日期时间格式化为字符串,例如:2023-10-23_14-30-15formatted_datetime=current_datetime.strftime('%......
  • WPS WORD EXCEL 不合并显示
    WPSWORDEXCEL不合并显示 版本:WPS12,下载时间约是2023年。 1.在开始菜单里找到WPSOFFICE-配置工具2.点击“高级(A)”。3.在“其他选项”选项卡中,点击“切换到旧版的多组件模式”。4.选择“多组件模式”,然后确定。11......
  • ACCESS 关于使用VBA选择路径时提示"方法'FileDislog作用于对象'_Application’时失败"
    以下是源码:PrivateSubCommand0_Click()'打开文件选择对话框WithApplication.FileDialog(msoFileDialogFilePicker).AllowMultiSelect=False.Filters.Clear.Filters.Add"Excel文件","*.xls;*.xlsx",1I......
  • python之自定义表头、列表内容导出excel文件例子
    函数三个参数outputfile:导出excel文件的位置,没有的话在该位置建该文件title:表头args:列的内容,每列是一个列表importxlsxwriterdefwriteExcel(outputfile,title,*args):wb=xlsxwriter.Workbook(outputfile)#创建sheetsheet=wb.add_worksheet("Sh......
  • excel 对列的统一操作(1)
    实现对某列统一添加相同内容比如要对某列文件前加上左括号,后加上右括号方法1:使用设置单元格格式的自定义功能1)选中要操作的单元格右击,选择设置单元格格式2)数字tab页,选择自定义,类型找到@,该@代表了该单元格的内容3)前后加(),即在@前后加(),效果如示例所展示的。4)点击确认,效果如......
  • Jmeter无法录制火狐浏览器打开的本机地址——http://Localhost:8080/*****
    问题描述:用管理员账号打开jmeter,新建录制模板,配置好网址和代理地址,证书也导入到火狐浏览器,浏览器也配好了代理地址。发现Jmeter可以录制别的地址,无法录制本机地址。原因分析:我们打开火狐浏览器的代理设置,可以发现它默认本机地址通过代理连接。接着我们要更改配置。在网址中......
  • 【Java】Excel 读取图片
     一、需求背景:做一个大屏管理系统,基础信息包括管理的应用名称,大屏的截图,通过一个excel批量导入excel的单元格里要插入图片,对应一个大屏应用的信息导入需要读取到大屏截图,至于存哪还没说....二、技术实现Hutool这块没有做图片读取的封装,看了网上的方式都是通过poi原生的api......
  • 基于Html对父页面打开子页面Dialog()的使用
    1、背景:​ 作者在写项目的时候,遇到了一个很坑的问题,项目前端基于QUI,但是大部分是js+css实现。​ 有一个功能:列表页面使用Dialog()组件打开编辑、新增窗体,编辑、新增窗体点击提交关闭窗体,能够刷新列表页面,无论怎么百度就是找不到可以实现的方法,最终功夫不负有心人,终于找到了一......
  • Android 获取 打开 WPS内的文档
    1.需求场景在项目开发中碰到这种情况,我们需要利用WPS的文档管理能力,比如需要调用WPS的文件选择器,来选择文档,同时需要得到WPS选择的文档结果返回给我们的应用。之前在网上找到了很久都没有找到WPS移动端有相关的API接口文档和解决方案,最近在逛WPS社区论坛的时看到了有一个WP......
  • Java根据模板生成excel文件【EasyExcel】【xls、xlsx】
    本文章参考:作者:WaiSaa  原文链接:https://blog.csdn.net/qq_42761569/article/details/1190251711、简介如下图所示,template目录下是准备好的模板,export目录下是生成数据文件。我们这里以第一个模板《theUser蒸汽历史数据.xls》为例进行测试,theUser为占位符,生成的文件中会被替换......