Writing a value in a cel of Excel
Writing a value in a cel of Excel
(OP)
Hello,
In Access I have the follow VBA-code:
sub test()
Dim ex as New Excel.Application
ex.DisplayAlerts =False
ex.Workbooks.Open "c:\test.xls
ex.Range("A1").value= "Example"
ex.ActiveWorkbook.Save
ex.Quit
ex.DisplayAlerts = True
Set ex = Nothing
End Sub
If Excel is get starting and the workbook test.xls is not open
this code workt perfect.
But if test.xls is open this code do not work, the value of
cel A1 is empty.
I want to allways write the value to Cel A1 if test.xls is
open or not opened.
Can somebody helpme?
Greetings,
mulderm
In Access I have the follow VBA-code:
sub test()
Dim ex as New Excel.Application
ex.DisplayAlerts =False
ex.Workbooks.Open "c:\test.xls
ex.Range("A1").value= "Example"
ex.ActiveWorkbook.Save
ex.Quit
ex.DisplayAlerts = True
Set ex = Nothing
End Sub
If Excel is get starting and the workbook test.xls is not open
this code workt perfect.
But if test.xls is open this code do not work, the value of
cel A1 is empty.
I want to allways write the value to Cel A1 if test.xls is
open or not opened.
Can somebody helpme?
Greetings,
mulderm





RE: Writing a value in a cel of Excel
Dim ex as New Excel.Application
ex.DisplayAlerts =False
ex.Workbooks.Open "c:\test.xls
if excel is not open it works because you are able to create a new excel app named test
if it is already open then you are not able to create the new app name test because it is already open
open a excel file with a different name than test ..see if the macro runs
looks like you need a if - then check to bypass creating a new file if it is running
RE: Writing a value in a cel of Excel
Is there code to first close the open test.xls (if opened)
and then running my code?
mulderm
RE: Writing a value in a cel of Excel
CODE
a) Excel is already running
b) Test.xls is open
and respond accordingly:
CODE
Dim XL As Excel.Application
Dim Wkb As Excel.Workbook
Dim MyWkb As Excel.Workbook
Dim TargetWorkbook As String
Dim ExcelRunning As Boolean
Dim MyWorkbookOpen As Boolean
TargetWorkbook = "C:\Test.xls"
On Error Resume Next
Set XL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear
ExcelRunning = False
Set XL = CreateObject("Excel.Application")
Else
ExcelRunning = True
End If
MyWorkbookOpen = False
If ExcelRunning Then
For Each Wkb In XL.Workbooks
If Wkb.FullName = TargetWorkbook Then
MyWorkbookOpen = True
Set MyWkb = Wkb
Exit For
End If
Next Wkb
End If
If Not MyWorkbookOpen Then
Set MyWkb = XL.Workbooks.Open(TargetWorkbook)
End If
MyWkb.ActiveSheet.Range("A1").Value = "Example"
MyWkb.Save
' The following is optional, depending if you want to
' leave the workbook open if it was open already.
If Not MyWorkbookOpen Then
MyWkb.Close
End If
If Not ExcelRunning Then
XL.Quit
End If
Set XL = Nothing
End Sub
Regards,
Mike
RE: Writing a value in a cel of Excel
RE: Writing a value in a cel of Excel
You´re the greatest!!!!
This code is exact what I means,works perfect.
Very, very thanks from the netherlands.
Greetings,
mulderm