ms-access vba - read from excel and also update th

2020-03-30 07:01发布

问题:

Created a simple access DB with only 1 form and 1 one button to run code that opens an existing empty excel (with 1 worksheet) and writes "X" in its 1st cell. It does the job but the workbook is hidden and I have to manually unhide it. That is, after the VBA code is executed I open the excel file and it is all grayed out. I have to click the "view" tab and then select the "Unhide" option and all is fine and I can see that the cell was updated as needed. If I take out the VBA line that writes "X" in the excel file, it doesn't hide the workbook. How do I solve the problem of the workbook being hidden?

Windows 7 and Office2013.

Thank you!!!

Here is the code:

Private Sub Command0_Click()
    Dim my_xl_app As Object
    Dim my_xl_worksheet As Object
    Dim my_xl_workbook As Object
    Set my_xl_app = CreateObject("Excel.Application")
    my_xl_app.UserControl = True
    my_xl_app.Visible = False    ' yes. I know it's the default
    Set my_xl_workbook = GetObject("D:\Dropbox\MASAV\HIYUVIM\AAA.xlsx")
    Set my_xl_worksheet = my_xl_workbook.Worksheets(1)
    my_xl_worksheet.Cells(1, "A") = "V"
    my_xl_workbook.Close SaveChanges:=True
    Set my_xl_app = Nothing
    Set my_xl_workbook = Nothing
    Set my_xl_worksheet = Nothing
End Sub

回答1:

S o l v e d !!!

Here is the code that works without hiding my entire workbook :

Private Sub Command0_Click()
    Dim my_xl_app As Object
    Dim my_xl_worksheet As Object
    Dim my_xl_workbook As Object
    Set my_xl_app = CreateObject("Excel.Application")
    Set my_xl_workbook = my_xl_app.Workbooks.Open("D:\Dropbox\MASAV\HIYUVIM\AAA.xlsx")
    Set my_xl_worksheet = my_xl_workbook.Worksheets(1)
    my_xl_workbook.Sheets(1).Range("A1").Value = "V"
    my_xl_workbook.Close SaveChanges:=True
    Set my_xl_app = Nothing
End Sub

Got the answer right here in this this forum, in another thread which escaped my eyes...

Thanks a lot to all in this wonderful forum!!!!



回答2:

Use this:

Workbooks(1).Windows(1).Visible = True