Error 424 when opening .xlsx from Access 2010 (1 Viewer)

wilsation

Registered User.
Local time
Today, 04:52
Joined
Jul 28, 2014
Messages
44
Here it is with WindowState

Code:
Private Sub txtWorksPriceLink_Click()
 Dim xlApp As Object
Dim xlBook As Object
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("Z:\" & Me.txtProjID & "\WorksPrice.xlsx")
xlApp.Visible = True
 xlApp.Windowstate = xlMaximized
Set xlApp = Nothing
 End Sub
 

vbaInet

AWF VIP
Local time
Today, 04:52
Joined
Jan 22, 2010
Messages
26,374
Is that the exact code that you tested? Can you re-test what you pasted.

The reason I ask is because one of them has UserControl and the other doesn't.
 

wilsation

Registered User.
Local time
Today, 04:52
Joined
Jul 28, 2014
Messages
44
Ok, I run this code:
Code:
Private Sub txtWorksPriceLink_Click()
 Dim xlApp As Object
Dim wb As Object
 Set xlApp = CreateObject("Excel.Application")
 Set wb = xlApp.Workbooks.Open("Z:\" & Me.txtProjID & "\WorksPrice.xlsx")
xlApp.Visible = True
xlApp.Windowstate = xlMaximized
Set wb = Nothing
Set xlApp = Nothing
End Sub
The spreadsheet opens as desired, behind it is the 1004 error - Unable to set windowstate.
When I click Debug it highlights
Code:
xlApp.Windowstate = xlMaximized
 

vbaInet

AWF VIP
Local time
Today, 04:52
Joined
Jan 22, 2010
Messages
26,374
Let's try this:
Code:
Private Sub txtWorksPriceLink_Click()
    Dim xlApp As Object
    Dim xlBook As Object
    
    Set xlApp = CreateObject("Excel.Application")
    
    With xlApp
        Set xlBook = .Workbooks.Open("Z:\" & Me.txtProjID & "\WorksPrice.xlsx")
        .Visible = True
        .Windows(1).Activate
        .WindowState = xlMaximized
        .UserControl = True
    End With
    
    Set xlApp = Nothing
    Set xlBook = Nothing
End Sub
 

wilsation

Registered User.
Local time
Today, 04:52
Joined
Jul 28, 2014
Messages
44
Hi, I've tried your code but get this error when it runs:

Compile Error: User -defined type not defined

And stops at this line:

Dim xlBook As Excel.Workbook

I've googled this error which suggested missing references, I've added DAO lib, ActiveX lib and Excel lib but it didn't make any difference.

Many thanks for your continued help.
 

winshent

Registered User.
Local time
Today, 04:52
Joined
Mar 3, 2008
Messages
162
Hi, I've tried your code but get this error when it runs:

Compile Error: User -defined type not defined

And stops at this line:

Dim xlBook As Excel.Workbook

That line is not part of the code posted by vbaInet
 

wilsation

Registered User.
Local time
Today, 04:52
Joined
Jul 28, 2014
Messages
44
Apologies, the error I get when running

Code:
Private Sub txtWorksPriceLink_Click()
    Dim xlApp As Object
    Dim xlBook As Object
    
    Set xlApp = CreateObject("Excel.Application")
    
    With xlApp
        Set xlBook = .Workbooks.Open("Z:\" & Me.txtProjID & "\WorksPrice.xlsx")
        .Visible = True
        .Windows(1).Activate
        .WindowState = xlMaximized
        .UserControl = True
    End With
    
    Set xlApp = Nothing
    Set xlBook = Nothing
End Sub

is Run-time error 1004:
Unable to set the WindowState property of the Application class
 

winshent

Registered User.
Local time
Today, 04:52
Joined
Mar 3, 2008
Messages
162
So if you comment that line out the code runs fine ?
 

wilsation

Registered User.
Local time
Today, 04:52
Joined
Jul 28, 2014
Messages
44
If I comment out
Code:
Dim xlBook As Object
I still get the 1004 WindowState error which highlights this line
Code:
.WindowState = xlMaximized
 

winshent

Registered User.
Local time
Today, 04:52
Joined
Mar 3, 2008
Messages
162
If you comment out....

Code:
.WindowState = xlMaximized
 

wilsation

Registered User.
Local time
Today, 04:52
Joined
Jul 28, 2014
Messages
44
Tried that but then the spreadsheet just opens minimised, which is back where we started....
 

winshent

Registered User.
Local time
Today, 04:52
Joined
Mar 3, 2008
Messages
162
I tested the code with Office 2003 (don't have 2010 here)...

As long as Excel Object Library is added to the list of references, code works fine here.

Suggest maybe an install problem.. try another machine..
 

wilsation

Registered User.
Local time
Today, 04:52
Joined
Jul 28, 2014
Messages
44
Hi Winshent, I agree that it looks like it could be a setup/config issue rather than a coding issue, I'll just have to keep looking.

As an aside, when I add in the Excel lib reference the spreadsheet will only open minimised regardless of code used.

thanks for your help.
 

winshent

Registered User.
Local time
Today, 04:52
Joined
Mar 3, 2008
Messages
162
Try
Code:
.Parent.WindowState = xlMaximized
 

wilsation

Registered User.
Local time
Today, 04:52
Joined
Jul 28, 2014
Messages
44
Produces the same 1004 windowstate error and highlights .Parent.WindowState = xlMaximized

Note that the spreadsheet opens maximised with or without the .Parent but this dialog box is behind it.
 

winshent

Registered User.
Local time
Today, 04:52
Joined
Mar 3, 2008
Messages
162
Security related issue ?

Is it a trusted document/ location ?
 

wilsation

Registered User.
Local time
Today, 04:52
Joined
Jul 28, 2014
Messages
44
Nope, I've purposely kept the filepath as short as possible - I hate long filepaths .
 

vbaInet

AWF VIP
Local time
Today, 04:52
Joined
Jan 22, 2010
Messages
26,374
It's nothing to do with code but with your Excel file. If you try it on a new Excel file it should work. If you want, you can upload the exact Excel file that you're trying to open and I'll see what is causing it.
 

Users who are viewing this thread

Top Bottom