Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long Private Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustFilter As Long nFilterIndex As Long lpstrFile As String nMaxFile As Long lpstrFileTitle As String nMaxFileTitle As Long lpstrInitialDir As String lpstrTitle As String flags As Long nFileOffset As Integer nFileExtension As Integer lpstrDefExt As String lCustData As Long lpfnHook As Long lpTemplateName As String End Type Private Sub Command3_Click() Dim XLS As New Excel.Application Dim WRK As Excel.Workbook Dim SHT As Excel.Worksheet Dim RNG As Excel.Range '建立Excel新实例 Set XLS = CreateObject("Excel.Application") '打开 XLS 文件 Set WRK = XLS.Workbooks.Open(Text1.Text, False, False) '把当前选择的工作表赋值给SHT Set SHT = WRK.Worksheets(1) SHT.Cells(1, 1) = 1 WRK.save '退出 Excel XLS.Quit
'变量释放 Set XLS = Nothing Set WRK = Nothing Set SHT = Nothing
End Sub我就是给excel表A1B1中赋值1,帮我看看。我想如果已打开了excel文件,那么代码中关于保存和退出会和当前的窗口excel文件发生冲突。请问怎么解决。
在Set SHT = WRK.Worksheets(1)前加个判断,避免这个情况出现 If XLS.ActiveWorkbook.IsInplace = False Then MsgBox "该excel文件已经在Microsoft Excel 中打开" Set XLS= Nothing Exit Sub End If
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Sub Command3_Click()
Dim XLS As New Excel.Application
Dim WRK As Excel.Workbook
Dim SHT As Excel.Worksheet
Dim RNG As Excel.Range
'建立Excel新实例
Set XLS = CreateObject("Excel.Application")
'打开 XLS 文件
Set WRK = XLS.Workbooks.Open(Text1.Text, False, False)
'把当前选择的工作表赋值给SHT
Set SHT = WRK.Worksheets(1)
SHT.Cells(1, 1) = 1
WRK.save
'退出 Excel
XLS.Quit
'变量释放
Set XLS = Nothing
Set WRK = Nothing
Set SHT = Nothing
End Sub我就是给excel表A1B1中赋值1,帮我看看。我想如果已打开了excel文件,那么代码中关于保存和退出会和当前的窗口excel文件发生冲突。请问怎么解决。
Application.DisplayAlerts = False
保存之后再变成
Application.DisplayAlerts = true
这样就能不消失 对话框!试试看但是一般的对excel操作都会做一个中间的文件操作完成后再整体拷贝到最终文件中。
If XLS.ActiveWorkbook.IsInplace = False Then
MsgBox "该excel文件已经在Microsoft Excel 中打开"
Set XLS= Nothing
Exit Sub
End If
操作完成后 XLS.Visible = True VB自动打开
好像不行,没反应。