473,385 Members | 1,370 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,385 software developers and data experts.

Changing screen resolution in Excel by VBA?

I have a computer that can manually be switched to 2 monitors. Both monitors do not have the same screen resolution. I am running a macro controlled presentation in Excel 2010 and I want to change the screen resolution when switching to the other monitor. In a previous post, “Change Screen Resolution using VB”, I found the following solution:

Expand|Select|Wrap|Line Numbers
  1. '************************************************* ****************
  2. ' DECLARATIONS SECTION
  3. '************************************************* ****************
  4.  
  5. Option Compare Database
  6. Option Explicit
  7.  
  8. Type RECT
  9. x1 As Long
  10. y1 As Long
  11. x2 As Long
  12. y2 As Long
  13. End Type
  14.  
  15. ' NOTE: The following declare statements are case sensitive.
  16.  
  17. Declare Function GetDesktopWindow Lib "User32" () As Long
  18. Declare Function GetWindowRect Lib "User32" _
  19. (ByVal hWnd As Long, rectangle As RECT) As Long
  20. '================================================= =====
  21.  
  22. 'This code shows how to change the screen resolution.
  23.  
  24. 'Call the function like this:
  25.  
  26. ' ChangeResolution 640, 480
  27.  
  28. 'This would change the screen resolution to 640 pixels x 480 pixels. Note
  29. that
  30. 'you can only change the resolution to values supported by the display.
  31.  
  32. 'Paste the following code into a module:'
  33.  
  34. Private Declare Function ChangeDisplaySettings Lib "User32" Alias _
  35. "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwFlags As Long) As Long
  36. Private Declare Function EnumDisplaySettings Lib "User32" Alias _
  37. "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As _
  38. Long, lpDevMode As Any) As Boolean
  39.  
  40. Const DM_PELSWIDTH = &H80000
  41. Const DM_PELSHEIGHT = &H100000
  42. Const CCFORMNAME = 32
  43. Const CCDEVICENAME = 32
  44.  
  45. Private Type DEVMODE
  46. dmDeviceName As String * CCDEVICENAME
  47. dmSpecVersion As Integer
  48. dmDriverVersion As Integer
  49. dmSize As Integer
  50. dmDriverExtra As Integer
  51.  
  52. dmFields As Long
  53. dmOrientation As Integer
  54. dmPaperSize As Integer
  55. dmPaperLength As Integer
  56. dmPaperWidth As Integer
  57. dmScale As Integer
  58. dmCopies As Integer
  59. dmDefaultSource As Integer
  60. dmPrintQuality As Integer
  61. dmColor As Integer
  62. dmDuplex As Integer
  63. dmYResolution As Integer
  64. dmTTOption As Integer
  65. dmCollate As Integer
  66.  
  67. dmFormName As String * CCFORMNAME
  68. dmUnusedPadding As Integer
  69. dmBitsPerPel As Integer
  70. dmPelsWidth As Long
  71. dmPelsHeight As Long
  72. dmDisplayFlags As Long
  73. dmDisplayFrequency As Long
  74. End Type
  75.  
  76. Public Function Change_Resolution(iWidth As Single, iHeight As Single)
  77.  
  78. Dim DevM As DEVMODE
  79. Dim a As Boolean
  80. Dim i As Long
  81. Dim b As Long
  82.  
  83. i = 0
  84.  
  85. 'Enumerate settings
  86. Do
  87. a = EnumDisplaySettings(0&, i&, DevM)
  88. i = i + 1
  89. Loop Until (a = False)
  90.  
  91. 'Change settings
  92. DevM.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT
  93.  
  94. DevM.dmPelsWidth = iWidth
  95. DevM.dmPelsHeight = iHeight
  96.  
  97. b = ChangeDisplaySettings(DevM, 0)
  98.  
  99. End Function
  100.  
  101. '************************************************* ****************
  102. ' FUNCTION: GetScreenResolution()
  103. '
  104. ' PURPOSE:
  105. ' To determine the current screen size or resolution.
  106. '
  107. ' RETURN:
  108. ' The current screen resolution. Typically one of the following:
  109. ' 640 x 480
  110. ' 800 x 600
  111. ' 1024 x 768
  112. '
  113. '************************************************* ****************
  114. Function GetScreenResolution() As String
  115.  
  116. Dim R As RECT
  117. Dim hWnd As Long
  118. Dim RetVal As Long
  119.  
  120. hWnd = GetDesktopWindow()
  121. RetVal = GetWindowRect(hWnd, R)
  122. GetScreenResolution = (R.x2 - R.x1) & "x" & (R.y2 - R.y1)
  123.  
  124. End Function
I pasted the code to a module from a workbook but this does not function. Does anybody know what I am doing wrong an how to achieve my goal: Changing screen resolution in Excel by VBA?
Oct 4 '13 #1
4 6703
Rabbit
12,516 Expert Mod 8TB
You didn't call your function anywhere.
Oct 4 '13 #2
Thanks Rabbit for your quick response. This was a little bit stupid indeed. But now I am immediately facing the next problem. When I write a procedure to call the function to change the screen resolution, the automatic fill up function of VBA wants me to add a sign of equality behind the expression "change_resolution(1280, 1024)". But I do not know what to add behind the sign of equality. I now put a x behind it, and the screen resolution is changing by running this procedure. Nevertheless I get a failure message at the end:
"Failure 424 during running: Object necessary"
What do I need to change in the procedure as shown below to prevent the failure message?

Expand|Select|Wrap|Line Numbers
  1. Sub change()
  2. change_resolution(1280, 1024) = x '????
  3. End Sub
Oct 5 '13 #3
Rabbit
12,516 Expert Mod 8TB
Subs are called without parentheses. Functions are called with parentheses.
Oct 5 '13 #4
Thanks Rabbit for your help
I have changed the code as shown below. This works

Expand|Select|Wrap|Line Numbers
  1. Sub change()
  2. Call change_resolution(1280, 1024)
  3. End Sub
  4.  
Oct 7 '13 #5

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

Similar topics

0
by: Erik Bethke | last post by:
Hello All, I am trying to clean up some polish bugs with the Shanghai game I am working on and I am currently stuck on trying to get the right event for detecting when the user has changed the...
4
by: Ronny Sigo | last post by:
Hello all, Ik have made an Access application on my computer which has a screen resolution of 1024 x 768. However the goal is to run it on my girlfriend's computer which has a screen resolution of...
4
by: pjac | last post by:
I need some help with some VB language that will change the screen resolution on a monitor when a MS-Access 2000 database is opened from 1024 x 768 to 800 x 600. Any help with this effort would be...
5
by: jlombardo | last post by:
I've read previous posts and know it is "bad form" to change a user's screen resolution to fit your program's needs. My situation is very different. I have a user with a laptop resolution of...
2
by: VMI | last post by:
How can I change the size of the forms in my Windows application when the screen resolution the user has is higher than the one I developed the application in? I have a customer that has a screen...
4
by: Bill Nguyen | last post by:
My VB.Net app requires a minimum monitor resolution of 1024 x 768. How to get user's screen resolution and set it to the minimum at runtime? Thanks Bill
1
by: Scott | last post by:
Hi I need to develop a simple application to simply convert my screen resolution to 1024 by 768 Anybody have any ideas??? Scott
0
by: A. User | last post by:
All, I created an Access 2000 MDE application in 1024x768. A user of mine, who is running Vista with Office 2007, can not see the entire application- the right most side is off the screen. No...
1
by: Tea Maker | last post by:
Hi, I have an application that is best viewed at 1024x768. I know that changing the screen resolution might cause some problems, but it's very important that I go on with this. So my plan is,...
3
by: angelicdevil | last post by:
say the screen solution is 800 *600 how can i get the browser to change the resolution so that my site appears in the resolution it was made but on closing the or changing the browser tab it changes...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.