我正在嘗試訪問A7只有一張 excel 表的 excel 檔案的單元格。但是我Public member 'Range' on type '__ComObject' not found.在宣告中收到以下錯誤Dim tempValue As String = owb.Worksheets(1).Range("A7") 'Error。為什么會出現這個錯誤以及如何糾正它?順便說一句,我也嘗試在上述陳述句中使用Valueand Value2afterRange但即使這樣也不起作用 - 令人驚訝的是value andvalue2并沒有出現在代碼Range中的錯誤陳述句中的智能感知中。
Imports System
Imports System.IO
Imports Microsoft.Office.Interop
Module Program
Dim oxl As Excel.Application
Dim owb As Excel.Workbook
Sub Main()
oxl = CreateObject("Excel.Application")
oxl.Visible = True
Dim path As String = "G:\Amit Kapoor\Matthews Asia\Matthews Raw Data"
Dim names As String() = Directory.GetFiles(path, "*.xlsx")
Dim pathofFirstfile As String = names(0)
Console.WriteLine(pathofFirstfile) 'Works fine
owb = oxl.Workbooks.Open(pathofFirstfile) 'Works fine
Dim tempValue As String = owb.Worksheets(1).Range("A7") 'Error
Console.WriteLine("Scheme Name: {0}", tempValue)
Console.ReadLine()
End Sub
End Module
另一個相關問題如下: 在上面的代碼中,我oxl通過使用oxl打開excel檔案指向了帶有變數的excel作業簿。但是,如果我打開它,我也必須關閉它——這會增加我的代碼長度。那么有沒有辦法將變數oxl與 excel 作業簿相關聯,以便在不打開 excel 作業簿的情況下執行 excel 任務?也就是說,是否可以oxl在不使用變數打開作業簿的情況下指出所需的 Excel 作業簿oxl?
uj5u.com熱心網友回復:
我沒有安裝 VB,所以無法測驗、查看示例等,看起來您需要明確地轉換一些東西才能使其正常作業?
Imports System
Imports System.IO
Imports Microsoft.Office.Interop
Module Program
Dim oxl As Excel.Application
Dim owb As Excel.Workbook
Dim ows As Excel.Worksheet
Dim owr As Excel.Range
Sub Main()
oxl = CreateObject("Excel.Application")
oxl.Visible = True
Dim path As String = "G:\Amit Kapoor\Matthews Asia\Matthews Raw Data"
Dim names As String() = Directory.GetFiles(path, "*.xlsx")
Dim pathofFirstfile As String = names(0)
Console.WriteLine(pathofFirstfile)
owb = oxl.Workbooks.Open(pathofFirstfile)
ows = CType(owb.Sheets(1), Excel.Worksheet)
owr = ows.Range("A7")
Dim tempValue As String = CStr(owr.Value)
Console.WriteLine("Scheme Name: {0}", tempValue)
Console.ReadLine()
End Sub
End Module
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/525268.html
