473,383 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Open Excel at 90% zoom

13
Hello
I was able to create/open an Excel ss from vb.net but how do I open it at 90% something like...
ActiveWindow.Zoom = 90

but I get "Activewindow is not declared." I'm really new at this, please help
Sep 6 '10 #1
11 15638
Stewart Ross
2,545 Expert Mod 2GB
Although the Activewindow property can be referred to on its own from within Excel, where the Application object is implied, you can't do this outside of Excel. You must fully-qualify the Excel application server object instead.

Using VBA as an illustration

Expand|Select|Wrap|Line Numbers
  1. Dim objExcel as Excel.Application
  2. Set objExcel as New Excel.Application
  3. .
  4. .
  5. objExcel.Activewindow.Zoom = 90
-Stewart
Sep 7 '10 #2
Stevecb
13
Hello - thanks for the reply. I tried your suggestion and
I'm getting "Object reference not set to an instance of an object."

I have this within a procedure that creates the ss
Expand|Select|Wrap|Line Numbers
  1.  Private Sub Button1_Click(ByVal sender As System.Object, _
  2.         ByVal e As System.EventArgs) Handles Button1.Click
  3.  
  4.         Dim objRange As Excel.Range = Nothing
  5.         xlApp = New Excel.ApplicationClass
  6.         xlWorkBook = xlApp.Workbooks.Add(misValue)
  7.         xlWorkSheet = xlWorkBook.Sheets("Sheet1")
  8.         objExcel = New Excel.Application
  9.         objExcel.ActiveWindow.Zoom = 90
  10.  
I thought it would reference the open Excel window, or so I thought
Sep 7 '10 #3
Stewart Ross
2,545 Expert Mod 2GB
I was using objExcel as an example of an object variable name. You have in effect declared a second Excel application object, which has no connection at all to the object called xlApp you have already instantiated. Even worse, until you open a workbook in the objExcel instance there is no active window to refer to.

Now that I know what objects you have declared there are several choices, as the Application object is available from a Worksheet object as well as directly. Perhaps simplest in your case is to replace lines 8 and 9 above with

Expand|Select|Wrap|Line Numbers
  1. xlWorkSheet.Application.ActiveWindow.Zoom = 90
-Stewart
Sep 7 '10 #4
Stevecb
13
I changed it to this and it doesn't error but it doesn't work either...
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, _
  2.         ByVal e As System.EventArgs) Handles Button1.Click
  3.  
  4.         Dim objRange As Excel.Range = Nothing
  5.         xlApp = New Excel.ApplicationClass
  6.         xlWorkBook = xlApp.Workbooks.Add(misValue)
  7.         xlWorkSheet = xlWorkBook.Sheets("Sheet1")
  8.         objExcel = New Excel.Application
  9.         'objExcel.ActiveWindow.Zoom = 90
  10.         xlApp.ActiveWindow.Zoom = 90
  11.  
Sep 7 '10 #5
Stevecb
13
Sorry, getting cross threaded. It works on sheet1 could I bother you to explain how to get it applied to all sheets not just the first. I tried...

xlWorkSheet.Application.ActiveWorkbook.zoom = 90

but it didn't like it.
Sep 7 '10 #6
Stewart Ross
2,545 Expert Mod 2GB
I've adapted what you provided to my VBA environment. I notice that the editor is not happy with variable names xlWorksheet, for example, as these appear to clash with system constants. I have renamed all such instances below.

I have tested the code below running from MS Access in VBA, with the application set Visible to cross check, and it does indeed zoom the active worksheet window (sheet 1) to 90%.

Expand|Select|Wrap|Line Numbers
  1. Public Sub Test()
  2.     Dim xlApp As New Excel.Application
  3.     Dim objxlWorkbook As Excel.Workbook
  4.     Dim objxlWorksheet As Excel.Worksheet
  5.     xlApp.Visible = True
  6.     Set objxlWorkbook = xlApp.Workbooks.Add()
  7.     Set objxlWorksheet = objxlWorkbook.Sheets("Sheet1")
  8.     objxlWorksheet.Application.ActiveWindow.Zoom = 90
  9. End Sub
I removed the name of the variable misValue from the Workbooks.Add method - I cannot see where it is declared or passed to the sub, and without knowing what its value is I do not know what template it is referring to. For test purposes I'm simply adding a new workbook.

-Stewart
Sep 7 '10 #7
Stevecb
13
Thanks for your patience it has been very helpful. I was hoping to open all the sheets at 90% and tried to loop through with this...

With xlWorkBook
For intCount = 1 To 7
xlApp.ActiveWindow.Zoom = 90 ' open sheet at 90%
Next
End With

but I think it just loops through numbers 1 - 7 and doesn't zoom each sheet
Sep 8 '10 #8
Stewart Ross
2,545 Expert Mod 2GB
You need to loop through the worksheets collection of the application object and activate each sheet before it can be zoomed. Using your counter approach, this would be done like this:

Expand|Select|Wrap|Line Numbers
  1. With xlApp
  2.     For intCount = 1 To .Worksheets.Count
  3.         .Worksheets(intCount).Activate
  4.         .ActiveWindow.Zoom = 90
  5.     Next
  6. End With
  7.  
Note the change to the application object from the workbook for the With, as the Workbook object was not being referred to in the With you showed.

There is a similar approach which does not use a counter as such, but a worksheet object within a FOR..EACH loop:

Expand|Select|Wrap|Line Numbers
  1. Dim objExcelWS As Excel.Worksheet
  2. With xlApp
  3.     For Each objExcelWS In .Worksheets
  4.         objExcelWS.Activate
  5.         .ActiveWindow.Zoom = 90
  6.     Next
  7. End With
These are general approaches which zoom all the worksheets in a workbook, regardless of how many or how few are in the current workbook. Using a constant such as 7 as the limit of the sheet count invites errors when opening workbooks that do not have 7 worksheets.

-Stewart
Sep 8 '10 #9
Stevecb
13
Thank you very much Stewart for your patience and expertise. It was a huge help!
Sep 8 '10 #10
Stevecb
13
I think I spoke too soon. It appears that the first 3 sheets are indeed zoomed to 90% but all the rest are not.
I wonder if they need to be activated before the zoom can occur. I see the code has...

.ActiveWindow.Zoom = 90

but maybe since the first 3 are added by default or whenever a ss is opened they are affected to 90%. I'm grasping at straws here
Sep 9 '10 #11
Stewart Ross
2,545 Expert Mod 2GB
It isn't an issue with the code; it is likely to be about precisely when you are using it. If you are using FOR intCount = 1 to xlApp.Worksheets.Count or the FOR..EACH method then the sub will zoom all the sheets that are present at the time. I've tried it with a default of 3 and a default of 7 with results exactly as expected.

If you subsequently add worksheets to the workbook you will need to activate each one then set its zoom setting accordingly.

You could separate out the zooming of all worksheets into a single subroutine and call that whenever you need to, or devise a general subroutine which will zoom any named sheet in a workbook:

Expand|Select|Wrap|Line Numbers
  1. Public Sub Zoom (xlApp as Excel.Application, ByVal WSName as string)
  2. Dim strActiveSheetName as String
  3. With xlApp
  4.   strActiveSheetName = .ActiveSheet.Name
  5.   .Worksheets(WSName).Activate
  6.   .ActiveWindow.Zoom = 90
  7.   .Worksheets(strActiveSheetName).Activate
  8. End With
  9. End Sub
This is called when required like this:

Expand|Select|Wrap|Line Numbers
  1. Zoom xlApp, "Sheet2"
-Stewart
Sep 9 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: JM | last post by:
Hi I have been trying to open Excel 2003 but I am having rotten luck. Just can't see to get it working. I looked at this article... ...
1
by: Micke | last post by:
I want to open Excel from an aspx page, put some records in there, format them and close the excel file. I can't open the Excel application. It says Cannot create ActiveX component. Here is the...
6
by: Micke | last post by:
Hi, I'm trying to open an Excel file from an aspx with following code: Imports Excel = Microsoft.Office.Interop.Excel Dim oExcel As New Excel.Application Dim oBooks As Excel.Workbooks...
3
by: Jennyfer Barco | last post by:
Hello, I have a question, how can I open Microsoft Excel from .NET. I only need to open a new file in Excel and paste some information and set the Microsoft Excel as the enabled aplication, so the...
1
by: Bon | last post by:
Hello all I create a form with three buttons in MS Access 2000. They are Open Excel Template, Save Draft and Save Final. When I click the Open Excel Template button, the Excel template will be...
2
by: k-w | last post by:
Hi all In vb I do: Dim Ex As New Excel.Application Ex.Workbooks.Open "c:\tmp\Book1.xls" Similar code in VC doesn't work:
9
by: Looch | last post by:
Was hoping I could get some insight on this. I added a text box and command button to a web page and added the code in the .vb file to open an existing Excel file based on what was typed in the...
1
by: trandtun | last post by:
Hello, I use VS2005,Excel2003,WinXP-sp2 and create asp.net web application project to open the excel file. I want to show excel application. If I use visual development server type from...
3
by: Mel | last post by:
How do I open an Excel file as a read only document? It would be okay if it opened in the browser window but I don't want the user to save any changes and stomp over my original file on the...
4
by: mld01s | last post by:
Hi all!! I need help, I have been stuck for a few days on this one. I am trying to open an excel table from a command button in Access. The excel table has an auto_open macro, that is supposed to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.