Discussion:
VBA 出現 沒有設定物件變數或 with 區塊變數
(时间太久无法回复)
Mercury
2006-05-29 14:37:02 UTC
Permalink
我有一個在 EXCEL 下的自定表單
表單內有一個文字框和一個OK的按鈕
按下OK按鈕後應該會判斷文字框內所輸入的檔案是否存在
幾個星期前用它都很很正常
這幾天用它時都會出現
沒有設定物件變數或 with 區塊變數
的錯誤訊息
不知道是哪裏有問題呢?
麻煩幫我看一下

程式碼如下:
' OK Button
Private Sub OB_Click()

Dim fsOBJ As Object

Set fsOBJ = CreateObject("Scripting.FileSystemObject")

nowpath = ThisWorkbook.Path

If Right(nowpath, 1) <> "\" Then
nowpath = nowpath & "\"
End If

Select_File_Name = nowpath & Trim(TextBox.Text)

' 判斷欲匯入資料檔案存在與否
If fsOBJ.FileExists(Select_File_Name) = True Then

' 不存在
MsgBox ("找不到指定的檔案!!! '" & Select_File_Name & "' 請再確認 檔名及副檔名 是否正確!!!")
Select_File_Name = ""

End If

Set fsOBJ = Nothing
Unload Me

End Sub

謝謝
--
-----=== Mercury ===-----
chijanzen
2006-05-29 15:00:03 UTC
Permalink
你好:

使用 Windows Script 物件,Excel 版本要在 2000 以上,不知道你的Excel 版本

請參考以下連結的第二種方法

http://www.vba.com.tw/plog/post/1/28
--
天行健,君子以自強不息
地勢坤,君子以厚德載物

http://www.vba.com.tw/plog/


"Mercury" 來函:
Post by Mercury
我有一個在 EXCEL 下的自定表單
表單內有一個文字框和一個OK的按鈕
按下OK按鈕後應該會判斷文字框內所輸入的檔案是否存在
幾個星期前用它都很很正常
這幾天用它時都會出現
沒有設定物件變數或 with 區塊變數
的錯誤訊息
不知道是哪裏有問題呢?
麻煩幫我看一下
' OK Button
Private Sub OB_Click()
Dim fsOBJ As Object
Set fsOBJ = CreateObject("Scripting.FileSystemObject")
nowpath = ThisWorkbook.Path
If Right(nowpath, 1) <> "\" Then
nowpath = nowpath & "\"
End If
Select_File_Name = nowpath & Trim(TextBox.Text)
' 判斷欲匯入資料檔案存在與否
If fsOBJ.FileExists(Select_File_Name) = True Then
' 不存在
MsgBox ("找不到指定的檔案!!! '" & Select_File_Name & "' 請再確認 檔名及副檔名 是否正確!!!")
Select_File_Name = ""
End If
Set fsOBJ = Nothing
Unload Me
End Sub
謝謝
--
-----=== Mercury ===-----
Mercury
2006-05-30 01:30:02 UTC
Permalink
你好,
  我是使用 Excel 2003 版.
  使用你提供的方法後, 原本產生問題處的問題已解決.
  但相同的錯誤訊息卻出現在其他地方, 如下:

  Dim oXL As Excel.Application
  Dim oWB As Excel.Workbook
  Dim oSheet As Excel.Worksheet

  ' 匯入 Excel 檔, 共檢查 C~I 欄位不可有 ","
Set oXL = CreateObject("Excel.Application")

'oXL.Visible = False
Set oWB = oXL.Workbooks.Open(Select_File_Name, True) <--- 問題出在這一行

  錯誤訊息相同, 不懂的是, 從一開始寫好後正常, 到現在出現錯誤, 所使用的都是相同的環境, 怎麼現在會有問題勒??

  謝謝!
--
-----=== Mercury ===-----


"chijanzen" 來函:
Post by chijanzen
使用 Windows Script 物件,Excel 版本要在 2000 以上,不知道你的Excel 版本
請參考以下連結的第二種方法
http://www.vba.com.tw/plog/post/1/28
--
天行健,君子以自強不息
地勢坤,君子以厚德載物
http://www.vba.com.tw/plog/
"Mercury" 來函:
Post by Mercury
我有一個在 EXCEL 下的自定表單
表單內有一個文字框和一個OK的按鈕
按下OK按鈕後應該會判斷文字框內所輸入的檔案是否存在
幾個星期前用它都很很正常
這幾天用它時都會出現
沒有設定物件變數或 with 區塊變數
的錯誤訊息
不知道是哪裏有問題呢?
麻煩幫我看一下
' OK Button
Private Sub OB_Click()
Dim fsOBJ As Object
Set fsOBJ = CreateObject("Scripting.FileSystemObject")
nowpath = ThisWorkbook.Path
If Right(nowpath, 1) <> "\" Then
nowpath = nowpath & "\"
End If
Select_File_Name = nowpath & Trim(TextBox.Text)
' 判斷欲匯入資料檔案存在與否
If fsOBJ.FileExists(Select_File_Name) = True Then
' 不存在
MsgBox ("找不到指定的檔案!!! '" & Select_File_Name & "' 請再確認 檔名及副檔名 是否正確!!!")
Select_File_Name = ""
End If
Set fsOBJ = Nothing
Unload Me
End Sub
謝謝
--
-----=== Mercury ===-----
Mercury
2006-06-08 02:37:01 UTC
Permalink
我終於找到問題了
原來是防毒軟體將巨集擋住了
將防毒軟體關閉後, 原本有問題的地方全都正常了
真是....被搞死了!
--
-----=== Mercury ===-----


"Mercury" 來函:
Post by Mercury
你好,
  我是使用 Excel 2003 版.
  使用你提供的方法後, 原本產生問題處的問題已解決.
  Dim oXL As Excel.Application
  Dim oWB As Excel.Workbook
  Dim oSheet As Excel.Worksheet
  ' 匯入 Excel 檔, 共檢查 C~I 欄位不可有 ","
Set oXL = CreateObject("Excel.Application")
'oXL.Visible = False
Set oWB = oXL.Workbooks.Open(Select_File_Name, True) <--- 問題出在這一行
  錯誤訊息相同, 不懂的是, 從一開始寫好後正常, 到現在出現錯誤, 所使用的都是相同的環境, 怎麼現在會有問題勒??
  謝謝!
--
-----=== Mercury ===-----
"chijanzen" 來函:
Post by chijanzen
使用 Windows Script 物件,Excel 版本要在 2000 以上,不知道你的Excel 版本
請參考以下連結的第二種方法
http://www.vba.com.tw/plog/post/1/28
--
天行健,君子以自強不息
地勢坤,君子以厚德載物
http://www.vba.com.tw/plog/
"Mercury" 來函:
Post by Mercury
我有一個在 EXCEL 下的自定表單
表單內有一個文字框和一個OK的按鈕
按下OK按鈕後應該會判斷文字框內所輸入的檔案是否存在
幾個星期前用它都很很正常
這幾天用它時都會出現
沒有設定物件變數或 with 區塊變數
的錯誤訊息
不知道是哪裏有問題呢?
麻煩幫我看一下
' OK Button
Private Sub OB_Click()
Dim fsOBJ As Object
Set fsOBJ = CreateObject("Scripting.FileSystemObject")
nowpath = ThisWorkbook.Path
If Right(nowpath, 1) <> "\" Then
nowpath = nowpath & "\"
End If
Select_File_Name = nowpath & Trim(TextBox.Text)
' 判斷欲匯入資料檔案存在與否
If fsOBJ.FileExists(Select_File_Name) = True Then
' 不存在
MsgBox ("找不到指定的檔案!!! '" & Select_File_Name & "' 請再確認 檔名及副檔名 是否正確!!!")
Select_File_Name = ""
End If
Set fsOBJ = Nothing
Unload Me
End Sub
謝謝
--
-----=== Mercury ===-----
Loading...