我有一個用vb.net編寫的看起來像這樣的Datepicker。GetActiveObject從Excel的工作簿
我不得不這樣做這種方式,因爲我們有一些安全設置的。因此,Excel中的Build in Datepicker有時會生成一個錯誤。
日期選取器工作正常,如果有只有一個Excel應用程序打開。 問題是有多個Excel應用程序打開。
的代碼只需要第一個Excel應用程序,但我想要得到的Excel工作簿這就是所謂的「測試」。
我認爲這個問題是這樣的說法:
objExcel = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
爲了更好地理解代碼從Excel和DatePicker的應用低於:
這就是我如何打開vb.net應用在Excel中:
Set wsh = VBA.CreateObject("WScript.Shell")
arg = ActiveWorkbook.Path & ";" & ActiveWorkbook.Name & ";" & ActiveSheet.Name & ";" & Target.Address
' Wait for the shelled application to finish:
errorCode = wsh.Run(strPathDatePicker & " " & arg, windowStyle, waitOnReturn)
DatePciker的代碼是:
Public Sub frmDatePicker_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Par() As String
Dim strTemp As String
Try
Dim arg = Environment.GetCommandLineArgs
strTemp = arg(1)
Par = Split(strTemp, ";")
'Split arg, to get the Information from the Excel Workbook
strWbPath = Par(0)
strWbName = Par(1)
strWsName = Par(2)
strAdresse = Par(3)
Catch ex As Exception
Me.Close()
End Try
End Sub
如果你現在點擊一個日期vb.net應用程序將運行這段代碼插入選定的日期到Excel:
Private Sub MonthCalendar1_DateSelected(sender As Object, e As DateRangeEventArgs) Handles MonthCalendar1.DateSelected
Dim objExcel As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Try
'Get the Excel Object
objExcel = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
For Each wb In objExcel.Workbooks
If wb.Name = strWbName Then
ws = wb.Sheets(strWsName)
ws.Range(strAdresse.ToString).Value = e.Start()
Me.Close()
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
Me.Close()
End Try
End Sub
謝謝。這正是我需要的!並感謝您與Chr(0)的提示。而且我已經對命令行中的空格產生了問題。我只是做了一個搜索和替換的解決方法。 – Moosli