怎樣使用CommonDialog控件及控件的介紹;

解决方案 »

  1.   

    http://xzd.2000y.net/mb/1/ReadNews.asp?NewsID=581866
      

  2.   

    CommonDialog 控件
                CommonDialog 控件提供一组标准的操作对话框,进行诸如打开和保存文件,设置打印选项,以及选择颜色和字体等操作。通过运行 Windows 帮助引擎控件还能显示帮助。语法CommonDialog说明CommonDialog 控件在 Visual Basic 和 Microsoft Windows 动态链接库 ommdlg.dll. 的例程之间提供了一个接口。为了用这个控件创建一个对话框,ommdlg.dll. 必须在 Microsoft Windows 的 SYSTEM 目录下。在应用程序中要使用 CommonDialog 控件,可将其添加到窗体中并设置其属性。控件所显示的对话框由控件的方法确定。在运行时,当相应的方法被调用时,将显示一个对话框或是执行帮助引擎;在设计时,CommonDialog 控件是以图标的形式显示在窗体中。该图标的大小不能改变。使用指定的方法,CommonDialog 控件能够显示下列对话。方法 所显示的对话框 
    ShowOpen 显示“打开”对话框 
    ShowSave 显示“另存为”对话框 
    ShowColor 显示“颜色”对话框 
    ShowFont 显示“字体”对话框 
    ShowPrinter 显示“打印”或“打印选项”对话框 
    ShowHelp 调用 Windows 帮助引擎 
    在对话框接口上单击,CommonDialog 控件将自动提供与上下文有关的帮助: 单击标题栏中的“这是什么?”帮助按钮,然后单击想详细信息的项目。
    将鼠标放在想进一步详细信息的项目上,单击右键,然后在所显示的上下文菜单中选择这是什么命令。 
    操作系统提供在 Windows 95 帮助弹出中显示的文本。也可以通过设置 Flags 属性,在带有 CommonDialog 控件的对话框中显示一个帮助按钮,但是,必须在这个位置提供帮助主题。注意 无法指定对话框显示在什么地方。详细信息 要查看各对话的帮助主题,单击“请参阅”。
      

  3.   

    使用CommonDialog 控件保存文件
    如果保存文件的文件名與保存位置文件名相同
    將提示三種選項
    “是”,“否”,“取消”
    如果選擇“否” and “取消”
    將報錯,
    Code as follow:
    oBook.SaveAs CommonDialog1.FileName
      

  4.   

    在事件最上面加上
    On Error Goto ErrH
    最下面ErrH:
       If Err>0 Then Exit Sub
    End Sub
      

  5.   

    如果我要選擇"否" and “取消”
    選項呢?
      

  6.   

    否 与 取消 不会同时存在, 只有保存与取消才会同时存在, 取消 On error 会替你解决是否复盖? 是 与 否 此时才会同时存在, 但轮不到 取消, 别自己乱发明.
    Private Sub Command1_Click()
       On Error GoTo errhandler
       With CommonDialog1
          .Filter = "Bmp|*.bmp"
          .Flags = cdlOFNOverwritePrompt  '若文件已存在,提示确认覆盖
          .ShowSave
       End With
       If CommonDialog1.FileName <> "" Then SavePicture Picture2.Image, CommonDialog1.FileName
    errhandler:
       If Err > 0 Then Exit Sub
    End Sub
      

  7.   

    你那是 “CommonDialog控件” 吗?贴张图片看看!
      当 CommonDialog控件 的 .Flags 属性设置 &H2 标志位时,在“保存”对话框中指定了已经存在的文件,会产生警告消息询问是否覆盖,这是的提示消息框中有“是”和“否”两个按钮;但无论如何,‘保存对话框’中只有“保存”和“取消”两个按钮。
      

  8.   

    [size=10px]又出現另外的錯誤,
    “類不支持自動化或不支持期望的接口”;
    錯誤行代碼如下[color=#0000FF]:
    [/[/color]size]
    oSheet.Range("A1").CopyFromRecordset DataGrid1.DataSource
      

  9.   

    Code as follow:Private Sub OutputToExcel_Click()On Error GoTo ErrH'2009.10.20 修改
    'Dim sNWind As StringDim conn As New ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim xlsheet As Excel.Worksheet
    Dim Irowcount As Integer            '數據的行數
    Dim Icolcount As Integer            '數據的列數
    '修改:把"絕對路徑"改成"相對路徑"
    'sNWind = "C:\Documents and Settings\goldenzhong\桌面\分析維修管理系統\information.mdb"
    'conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sNWind & ";"conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\information.mdb;"
    conn.CursorLocation = adUseClient
    Set rs = conn.Execute("Info", , adCmdTable)'在Excel中创建新的workbookDim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Excel.Worksheet
    Set oExcel = CreateObject("Excel.Application")
    Set oBook = oExcel.Workbooks.Add
    Set oSheet = oBook.Worksheets("Sheet1")'向 Excel中传输数据
    'oSheet.Range("A1").CopyFromRecordset rs
    '2009.10.25 修改 修改原因:把DataGrid1數據復制到Execl表
    '2009.12.6 修改  修改原因:如果DatagGrid數據為空,將報錯
    If DataGrid1.Columns.Count >= 3 Then
        oSheet.Range("A1").CopyFromRecordset DataGrid1.DataSource
    Else
        MsgBox "數據為空,不能導出到Execl表!", 0, "提示"
        Exit Sub
    End If
    '保存并退出Excel
    '修改原因:讓用戶選擇"保存路徑"及“文件名”?
    '2009.10.23 修改 打開變成另存為
    'CommonDialog1.ShowOpenCommonDialog1.ShowSave'2009.10.22 修改  修改目的:導出execl表有標題
    If rs.RecordCount > 0 Then
        '2009.10.24 修改 原因:rs.Fields.Count <>DataGrid1.Columns.Count
        'For i = 1 To rs.Fields.Count
        Icolcount = DataGrid1.Columns.Count
        'Irowcount = rs.RecordCount
        '計算DataGrid1行數 (“DataGrid”只是當前畫面的數據行,而非所有“DataGrid”數據行)
        'Irowcount = DataGrid1.Row + 1
        'Irowcount = DataGrid1.VisibleRows
        Irowcount = Adodc2.Recordset.RecordCount  'Adodc2.Recordset.RecordCount<>rs.RecordCount    
        For i = 1 To DataGrid1.Columns.Count
            'oSheet.Cells(1, i) = rs.Fields(i - 1).Name
            oSheet.Cells(1, i) = DataGrid1.Columns(i - 1).Caption               '調用Datgrid1.Columns(i-1)字段名
        Next i
        
        With oSheet
            .Range(.Columns("A"), .Columns("M")).AutoFit
            '設表自動調整列寬
            .Range(.Cells(1, 1), .Cells(Irowcount, Icolcount)).Borders.LineStyle = xlContinuous
            '设表格边框样式
        End With    oSheet.Columns("A:AC").HorizontalAlignment = xlCenter                   '所有行居中
        'CommonDialog1.Filter = "execl(*.xls)*.xls"
        'CommonDialog1.Filter = "*.xls"               ' 異常,直接CommonDialog1屬性Filter設置
        '如果保存文件名與保存文件夾中文件的文件名相同,將報錯(選擇"取消","否")    If Len(CommonDialog1.FileName) > 3 Then
            If Dir(CommonDialog1.FileName) <> vbNullString Then
                If MsgBox("原文件存在,是否覆盖?", vbYesNo, "提示框") = vbYes Then
                    Kill CommonDialog1.FileName
                Else
                    Exit Sub
                End If
            End If
            
            oBook.SaveAs CommonDialog1.FileName
            MsgBox "導出Execl成功!", 0, "提示"
            oExcel.Quit
        Else
            MsgBox "没有数据导出", , "提示"
        End If
    End If'oBook.SaveAs "C:\Documents and Settings\goldenzhong\桌面\Book1.xls"
    '关闭连接rs.Close
    conn.CloseErrH:
      If Err > 0 Then
        Exit Sub
     End IfEnd Sub