Lấy tên đường dẫn file Excel từ AutoCad

Liên hệ QC

NguyenNgocSon

KEEP WALKING
Tham gia
4/4/08
Bài viết
280
Được thích
832
Nghề nghiệp
Ths. Cầu hầm
Có cách nào để từ môi trường VBA Cad mình lấy được tên đường dẫn của một file Excel (hay folder được chọn ) được chọn không ạ ?
Em cám ơn !
 
Bạn chú ý Tenfile là đường dẫn nhé

Mã:
Public ExcelApp As Excel.Application
Public Const Tenfile = "D:\PTH\Khoahoc\Nam2008\Excel_AutoCad.xls"

Sub KhoidongExcel()
  On Error Resume Next  'Bo qua loi khi Excel chua mo
    'Khi Excel dang mo
    Set ExcelApp = GetObject(, "Excel.Application")
      'Khi Excel chua mo thi mo
      If Err Then
        Err.Clear
        Set ExcelApp = CreateObject("Excel.Application")
      End If
    ExcelApp.Visible = True
    ExcelApp.Workbooks.Open (Tenfile) 'Mở file dữ liệu Tenfile
    AppActivate AutoCAD.Caption
End Sub
 
Bạn chú ý Tenfile là đường dẫn nhé

Mã:
Public ExcelApp As Excel.Application
Public Const Tenfile = "D:\PTH\Khoahoc\Nam2008\Excel_AutoCad.xls"

Sub KhoidongExcel()
  On Error Resume Next  'Bo qua loi khi Excel chua mo
    'Khi Excel dang mo
    Set ExcelApp = GetObject(, "Excel.Application")
      'Khi Excel chua mo thi mo
      If Err Then
        Err.Clear
        Set ExcelApp = CreateObject("Excel.Application")
      End If
    ExcelApp.Visible = True
    ExcelApp.Workbooks.Open (Tenfile) 'Mở file dữ liệu Tenfile
    AppActivate AutoCAD.Caption
End Sub
Dạ. Em cám ơn bác.nhưng ý em là lấy tên đường dẫn động cơ ạ ?
Có cách nào không ?
 
Dạ. Em cám ơn bác.nhưng ý em là lấy tên đường dẫn động cơ ạ ?
Có cách nào không ?

Đường dẫn động nghĩa là sao đây? File Excel đó nằm ở đâu thì đường dẫn của nó vẫn luôn thế thôi, "động" là "động" thế nào chứ?
 
Đường dẫn động nghĩa là sao đây? File Excel đó nằm ở đâu thì đường dẫn của nó vẫn luôn thế thôi, "động" là "động" thế nào chứ?
Dạ. Ý em là như thế này:
+ Em có file Cad gốc
+ Khi mở file Cad lên khi mình click chọn file excel nào thì nó tự lấy đường dẫn ạ ?
Ví dụ: khi file ở trong usb thì nó là:G:\Flile.xls; khi mình copy vào ổ D là: D:\Flile.xls
Như vậy nếu khai báo const thì e không ổn ạ?
Em cám ơn thầy !
 
Vấn đề nằm ở chổ này ---> Ý tôi muốn biết cái việc chọn ấy được thực hiện ra sao?
cụ thể là như thế này:
PHP:
Public ExcelApp As Excel.Application Public Const Tenfile = "D:\PTH\Khoahoc\Nam2008\Excel_AutoCad.xls"  Sub KhoidongExcel()   On Error Resume Next  'Bo qua loi khi Excel chua mo     'Khi Excel dang mo     Set ExcelApp = GetObject(, "Excel.Application")       'Khi Excel chua mo thi mo       If Err Then         Err.Clear         Set ExcelApp = CreateObject("Excel.Application")       End If     ExcelApp.Visible = True     ExcelApp.Workbooks.Open (Tenfile) 'Mở file dữ liệu Tenfile  
   AppActivate AutoCAD.Caption End Sub
Cái đường dẫn "D:\PTH\Khoahoc\Nam2008\Excel_AutoCad.xls"
không phải cố định.Mà khi thực hiện thủ tục thì yêu cầu chọn file Excel, và lấy tên của file vừa mở cập nhật làm đường dẫn luôn
 
cụ thể là như thế này:
PHP:
Public ExcelApp As Excel.Application Public Const Tenfile = "D:\PTH\Khoahoc\Nam2008\Excel_AutoCad.xls"  Sub KhoidongExcel()   On Error Resume Next  'Bo qua loi khi Excel chua mo     'Khi Excel dang mo     Set ExcelApp = GetObject(, "Excel.Application")       'Khi Excel chua mo thi mo       If Err Then         Err.Clear         Set ExcelApp = CreateObject("Excel.Application")       End If     ExcelApp.Visible = True     ExcelApp.Workbooks.Open (Tenfile) 'Mở file dữ liệu Tenfile  
   AppActivate AutoCAD.Caption End Sub
Cái đường dẫn "D:\PTH\Khoahoc\Nam2008\Excel_AutoCad.xls"
không phải cố định.Mà khi thực hiện thủ tục thì yêu cầu chọn file Excel, và lấy tên của file vừa mở cập nhật làm đường dẫn luôn

Chạy thử code xem đúng ý không

PHP:
Sub test()
  With Application.FileDialog(msoFileDialogOpen)
    .Show
    MsgBox .SelectedItems(1)
  End With
End Sub

À, mà đây là viết trong VBA Excel, bạn làm trong Cad thì cũng vậy. Dùng code của anh Hướng nhưng thay Application. bằng ExcelApp. là được.
 
Lần chỉnh sửa cuối:
Không hiểu sao thực hiện thủ tục xong
Cad treo luôn :-=
 
cụ thể là như thế này:
PHP:
Public ExcelApp As Excel.Application Public Const Tenfile = "D:\PTH\Khoahoc\Nam2008\Excel_AutoCad.xls"  Sub KhoidongExcel()   On Error Resume Next  'Bo qua loi khi Excel chua mo     'Khi Excel dang mo     Set ExcelApp = GetObject(, "Excel.Application")       'Khi Excel chua mo thi mo       If Err Then         Err.Clear         Set ExcelApp = CreateObject("Excel.Application")       End If     ExcelApp.Visible = True     ExcelApp.Workbooks.Open (Tenfile) 'Mở file dữ liệu Tenfile  
   AppActivate AutoCAD.Caption End Sub
Cái đường dẫn "D:\PTH\Khoahoc\Nam2008\Excel_AutoCad.xls"
không phải cố định.Mà khi thực hiện thủ tục thì yêu cầu chọn file Excel, và lấy tên của file vừa mở cập nhật làm đường dẫn luôn
Bạn hãy tra google từ khóa FileDialog in ACAD VBA ---> Có cả đống
 
Em cám ơn bác rất nhiều
Đây là Code:
PHP:
Public Tenfile As String
Public Sub OpenFile()
    'sample to show how to use FileDialogs
    Dim objFile As FileDialogs
    Dim strFilter As String
    Dim strFileName As String
    Set objFile = New FileDialogs
    'desc,filter combinations must all be separated with pipe char "|"
    strFilter = "All Files (*.*)|*.*|Drawings (*.dwg)|*.dwg"
    objFile.OwnerHwnd = ThisDrawing.HWnd    'bind the dialog to the window
    objFile.Title = "Open a drawing"
    'default dir is CurDir
    objFile.StartInDir = "c:\"
    objFile.Filter = strFilter
    'return a valid filename
    strFileName = objFile.ShowOpen
    If Not strFileName = vbNullString Then
        'use this space to perform operation
        MsgBox strFileName
    End If
    Tenfile = strFileName
    Set objFile = Nothing
End Sub
 
Web KT
Back
Top Bottom