Code mở File bị báo lỗi khi trong code đặt đuôi .xls? (1 người xem)

Người dùng đang xem chủ đề này

hoanglocphat

Thành viên thường trực
Tham gia
27/1/13
Bài viết
260
Được thích
30
Chào các bạn!
Tôi có 1 đoan code để mở 1 File là
Mã:
FileName = "D:\DuAnMoi\BaoThue_DA.xls"    
    With Workbooks.Open(FileName)
Nếu File BaoThue_DA có đuôi là .xls nhưng nếu là đuôi .xlsm thì code kg mở File BaoThue_DA lên được
tôi có sửa
Mã:
FileName = "D:\DuAnMoi\BaoThue_DA.xls[COLOR=#ff0000][B]?[/B][/COLOR]"    
    With Workbooks.Open(FileName)

Nghĩa là thêm ? vào, nhưng code bị báo lỗi là tìm kg thấy File
Trong trường hợp trên, ta fải sửa code như thế nào để vẫn mở được file BaoThue_DA.xls hoặc file BaoThue_DA.xlsm
Xin cảm ơn các bạn!
 
Chào các bạn!
Tôi có 1 đoan code để mở 1 File là
Mã:
FileName = "D:\DuAnMoi\BaoThue_DA.xls"    
    With Workbooks.Open(FileName)
Nếu File BaoThue_DA có đuôi là .xls nhưng nếu là đuôi .xlsm thì code kg mở File BaoThue_DA lên được
tôi có sửa
Mã:
FileName = "D:\DuAnMoi\BaoThue_DA.xls[COLOR=#ff0000][B]?[/B][/COLOR]"    
    With Workbooks.Open(FileName)

Nghĩa là thêm ? vào, nhưng code bị báo lỗi là tìm kg thấy File
Trong trường hợp trên, ta fải sửa code như thế nào để vẫn mở được file BaoThue_DA.xls hoặc file BaoThue_DA.xlsm
Xin cảm ơn các bạn!

Thử code này
PHP:
Sub Open_File()
Dim ObjFile As Object, path As String
path = "D:\DuAnMoi"
   With CreateObject("Scripting.FileSystemObject")
      With .GetFolder(path)
         For Each ObjFile In .Files
            If .GetBaseName(ObjFile) = "file BaoThue_DA" Then
               With Workbooks.Open(ObjFile, 0)
                  'Place your code here
                  .Close True
               End With
            End If
         Next
      End With
   End With
End Sub
 
Upvote 0
Thử code này
PHP:
Sub Open_File()
Dim ObjFile As Object, path As String
path = "D:\DuAnMoi"
   With CreateObject("Scripting.FileSystemObject")
      With .GetFolder(path)
         For Each ObjFile In .Files
            If .GetBaseName(ObjFile) = "file BaoThue_DA" Then
               With Workbooks.Open(ObjFile, 0)
                  'Place your code here
                  .Close True
               End With
            End If
         Next
      End With
   End With
End Sub
Nó báo lỗi màu vàng "If .GetBaseName(ObjFile) = "file BaoThue_DA" Then"
và báo như sau:
"Run-time error '438
Object doesn't Support this property or method"
 
Upvote 0
Nó báo lỗi màu vàng "If .GetBaseName(ObjFile) = "file BaoThue_DA" Then"
và báo như sau:
"Run-time error '438
Object doesn't Support this property or method"
Viết chay nên dễ có lỗi

PHP:
Sub Open_File()
Dim ObjFile As Object, path As String, fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
path = "D:\DuAnMoi"
With fso.GetFolder(path)
   For Each ObjFile In .Files
      If fso.GetBaseName(ObjFile) = "file BaoThue_DA" Then
         With Workbooks.Open(ObjFile, 0)
            'Place your code here
            .Close True
         End With
      End If
   Next
End With
End Sub
 
Upvote 0
Viết chay nên dễ có lỗi

PHP:
Sub Open_File()
Dim ObjFile As Object, path As String, fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
path = "D:\DuAnMoi"
With fso.GetFolder(path)
   For Each ObjFile In .Files
      If fso.GetBaseName(ObjFile) = "file BaoThue_DA" Then
         With Workbooks.Open(ObjFile, 0)
            'Place your code here
            .Close True
         End With
      End If
   Next
End With
End Sub
Kg còn báo lỗi nữa nhưng
Tôi kg thấy file BaoThue_DA mở lên bạn à!
 
Upvote 0
Chào các bạn!
Tôi có 1 đoan code để mở 1 File là
Mã:
FileName = "D:\DuAnMoi\BaoThue_DA.xls"    
    With Workbooks.Open(FileName)
Nếu File BaoThue_DA có đuôi là .xls nhưng nếu là đuôi .xlsm thì code kg mở File BaoThue_DA lên được
tôi có sửa
Mã:
FileName = "D:\DuAnMoi\BaoThue_DA.xls[COLOR=#ff0000][B]?[/B][/COLOR]"    
    With Workbooks.Open(FileName)

Nghĩa là thêm ? vào, nhưng code bị báo lỗi là tìm kg thấy File
Trong trường hợp trên, ta fải sửa code như thế nào để vẫn mở được file BaoThue_DA.xls hoặc file BaoThue_DA.xlsm
Xin cảm ơn các bạn!
Thì cũng đâu có phức tạp gì. IF một phát: Nếu mở cái này không được thì mở cái kia
Mã:
Sub Test()
  Dim wkb As Workbook, fileName As String
  fileName = "D:\DuAnMoi\BaoThue_DA.xls"
  On Error Resume Next
  Set wkb = Workbooks.Open(fileName)  ''<---mở file xls
  If Err.Number Then Set wkb = Workbooks.Open(fileName & "m")   ''<--- nếu có lỗi thì mở file xlsm
  On Error GoTo 0
  With wkb
    ''code của bạn
  End With
End Sub
 
Lần chỉnh sửa cuối:
Upvote 0
Kg còn báo lỗi nữa nhưng
Tôi kg thấy file BaoThue_DA mở lên bạn à!

Bạn có sửa cho cái tên file đúng chưa?
If fso.GetBaseName(ObjFile) = "file BaoThue_DA" Then
Chắc là để y nguyên code rồi chứ gì
If fso.GetBaseName(ObjFile) = "BaoThue_DA" Then
 
Upvote 0

Bài viết mới nhất

Back
Top Bottom