WUGNET, the Windows User Group Network
Your Complete Resource Center for "The Best" in Shareware, Computing Tips and Support, Windows Industry News... and much more!
Home Forums Shareware Windows Tips Hot Offers FREE Newsletters Arcade Contact Us About Partners
Search WUGNET: RSS Feeds RSS Feeds Advertise with WUGNET    |    Shareware eBooks
HomeHome FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Explain Complie error

 
Goto page 1, 2
   Home -> Office -> User Forms RSS
Next:  mail Merge not recognizing all Excel merge fields  
Author Message
Abel MacAdam

External


Since: Dec 17, 2007
Posts: 10



(Msg. 1) Posted: Mon Mar 16, 2009 8:28 am
Post subject: Explain Complie error
Archived from groups: microsoft>public>word>vba>userforms (more info?)

Hi,

I introduced a piece of code into my code. (see
http://www.dreamincode.net/forums/showtopic34776.htm)

Part of the code generated an error:

Public Sub ResizeForm(frm As Form)
'Set the forms height
frm.height = Screen.height / 2
'Set the forms width
frm.width = Screen.width / 2
'Resize all of the controls
'based on the forms new size
ResizeControls frm
End Sub

I introduced this code so I got a form that would automatically resize
depending on the screen dimensions of the user.

When I compile the code, I get a 'Compile error' on the following line:
'Public Sub ResizeForm(frm As Form'
Notice it did not include the last bracket.

How should I look at this error? What is wrong with this piece of code. Is
frm unknown, or is it Form. Or is a piece of this Sub used without it being
defined. I know frm is not defined, but is frm something that should have
been "known" by VB?

This must be very basic. But I'm not that versed in Visual Basic.

TIA,
Abel

Note: FWIW, I'm using Word 2002/XP. My VB version is 6.3
Back to top
Login to vote
Jay Freedman

External


Since: Sep 16, 2003
Posts: 2309



(Msg. 2) Posted: Mon Mar 16, 2009 12:54 pm
Post subject: Re: Explain Complie error [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi Abel,

The problem here is that you've grabbed some VB6 code and tried to plug it
directly into VBA. That often works with code that isn't related to VB forms
and VBA userforms. But VB forms are very different from VBA userforms, so
that kind of code cannot be directly ported.

The first thing you tripped over is that Form is not a valid object type in
VBA; instead you need to use the specific UserForm1 or whatever you may have
renamed that to. (In this case, you can't even use the generic 'UserForm'
type.)

Once you get that fixed, you would find that there is no Screen object in
VBA; instead, the System object has the properties you need, named
HorizontalResolution and VerticalResolution. Further, those two properties
are measured in pixels, while the Height and Width properties of the
UserForm1 object are measured in points. Fortunately, there's a built-in
function to convert pixels to points. You may need to make some similar
adjustments in the ResizeControls procedure.

Finally, in VBA userform-land, the proper place to put code of this kind is
not in the macro code that calls the userform, but in the procedure
UserForm_Initialize within the userform itself. That procedure runs
automatically between the call to the userform's .Show method and the time
the userform actually appears on the screen. Since the procedure is in the
userform, the built-in object Me refers to the userform, so you don't have
to pass the userform as a parameter. The code would be

Private Sub UserForm_Initialize()
Me.Height = PixelsToPoints(System.VerticalResolution / 2)
Me.Width = PixelsToPoints(System.HorizontalResolution / 2)
ResizeControls
End Sub

The ResizeControls procedure can also be made part of the
UserForm_Initialize procedure, and then instead of the line

For Each curr_obj In frm

you would use

For Each curr_obj In Me.Controls

Here's the whole enchilada:

Private Sub UserForm_Initialize()
Dim curr_obj As Control
Dim iHeight As Long, iWidth As Long
Dim x_size As Single
Dim y_size As Single

' save initial values
iHeight = Me.Height
iWidth = Me.Width

Me.Height = PixelsToPoints(System.VerticalResolution / 2)
Me.Width = PixelsToPoints(System.HorizontalResolution / 2)

' calculate resize factors
x_size = CSng(Me.Width) / CSng(iWidth)
y_size = CSng(Me.Height) / CSng(iHeight)

For Each curr_obj In Me.Controls
With curr_obj
.Left = .Left * x_size
.Width = .Width * x_size
.Top = .Top * y_size
.Height = .Height * y_size
End With
Next
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.


Abel MacAdam wrote:
> Hi,
>
> I introduced a piece of code into my code. (see
> http://www.dreamincode.net/forums/showtopic34776.htm)
>
> Part of the code generated an error:
>
> Public Sub ResizeForm(frm As Form)
> 'Set the forms height
> frm.height = Screen.height / 2
> 'Set the forms width
> frm.width = Screen.width / 2
> 'Resize all of the controls
> 'based on the forms new size
> ResizeControls frm
> End Sub
>
> I introduced this code so I got a form that would automatically resize
> depending on the screen dimensions of the user.
>
> When I compile the code, I get a 'Compile error' on the following
> line: 'Public Sub ResizeForm(frm As Form'
> Notice it did not include the last bracket.
>
> How should I look at this error? What is wrong with this piece of
> code. Is frm unknown, or is it Form. Or is a piece of this Sub used
> without it being defined. I know frm is not defined, but is frm
> something that should have been "known" by VB?
>
> This must be very basic. But I'm not that versed in Visual Basic.
>
> TIA,
> Abel
>
> Note: FWIW, I'm using Word 2002/XP. My VB version is 6.3
Back to top
Login to vote
Abel MacAdam

External


Since: Dec 17, 2007
Posts: 10



(Msg. 3) Posted: Mon Mar 16, 2009 11:58 pm
Post subject: Re: Explain Complie error [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Jay,

Thanks very much for your solution. As soon as I have the time I'm going to
look into it.

Abel

"Jay Freedman" wrote:

> Hi Abel,
>
> The problem here is that you've grabbed some VB6 code and tried to plug it
> directly into VBA. That often works with code that isn't related to VB forms
> and VBA userforms. But VB forms are very different from VBA userforms, so
> that kind of code cannot be directly ported.
>
> The first thing you tripped over is that Form is not a valid object type in
> VBA; instead you need to use the specific UserForm1 or whatever you may have
> renamed that to. (In this case, you can't even use the generic 'UserForm'
> type.)
>
> Once you get that fixed, you would find that there is no Screen object in
> VBA; instead, the System object has the properties you need, named
> HorizontalResolution and VerticalResolution. Further, those two properties
> are measured in pixels, while the Height and Width properties of the
> UserForm1 object are measured in points. Fortunately, there's a built-in
> function to convert pixels to points. You may need to make some similar
> adjustments in the ResizeControls procedure.
>
> Finally, in VBA userform-land, the proper place to put code of this kind is
> not in the macro code that calls the userform, but in the procedure
> UserForm_Initialize within the userform itself. That procedure runs
> automatically between the call to the userform's .Show method and the time
> the userform actually appears on the screen. Since the procedure is in the
> userform, the built-in object Me refers to the userform, so you don't have
> to pass the userform as a parameter. The code would be
>
> Private Sub UserForm_Initialize()
> Me.Height = PixelsToPoints(System.VerticalResolution / 2)
> Me.Width = PixelsToPoints(System.HorizontalResolution / 2)
> ResizeControls
> End Sub
>
> The ResizeControls procedure can also be made part of the
> UserForm_Initialize procedure, and then instead of the line
>
> For Each curr_obj In frm
>
> you would use
>
> For Each curr_obj In Me.Controls
>
> Here's the whole enchilada:
>
> Private Sub UserForm_Initialize()
> Dim curr_obj As Control
> Dim iHeight As Long, iWidth As Long
> Dim x_size As Single
> Dim y_size As Single
>
> ' save initial values
> iHeight = Me.Height
> iWidth = Me.Width
>
> Me.Height = PixelsToPoints(System.VerticalResolution / 2)
> Me.Width = PixelsToPoints(System.HorizontalResolution / 2)
>
> ' calculate resize factors
> x_size = CSng(Me.Width) / CSng(iWidth)
> y_size = CSng(Me.Height) / CSng(iHeight)
>
> For Each curr_obj In Me.Controls
> With curr_obj
> .Left = .Left * x_size
> .Width = .Width * x_size
> .Top = .Top * y_size
> .Height = .Height * y_size
> End With
> Next
> End Sub
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so
> all may benefit.
>
>
> Abel MacAdam wrote:
> > Hi,
> >
> > I introduced a piece of code into my code. (see
> > http://www.dreamincode.net/forums/showtopic34776.htm)
> >
> > Part of the code generated an error:
> >
> > Public Sub ResizeForm(frm As Form)
> > 'Set the forms height
> > frm.height = Screen.height / 2
> > 'Set the forms width
> > frm.width = Screen.width / 2
> > 'Resize all of the controls
> > 'based on the forms new size
> > ResizeControls frm
> > End Sub
> >
> > I introduced this code so I got a form that would automatically resize
> > depending on the screen dimensions of the user.
> >
> > When I compile the code, I get a 'Compile error' on the following
> > line: 'Public Sub ResizeForm(frm As Form'
> > Notice it did not include the last bracket.
> >
> > How should I look at this error? What is wrong with this piece of
> > code. Is frm unknown, or is it Form. Or is a piece of this Sub used
> > without it being defined. I know frm is not defined, but is frm
> > something that should have been "known" by VB?
> >
> > This must be very basic. But I'm not that versed in Visual Basic.
> >
> > TIA,
> > Abel
> >
> > Note: FWIW, I'm using Word 2002/XP. My VB version is 6.3
>
>
>
Back to top
Login to vote
Abel MacAdam

External


Since: Dec 17, 2007
Posts: 10



(Msg. 4) Posted: Wed Mar 18, 2009 5:48 am
Post subject: Re: Explain Complie error [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi all,

I got the time to look into Jay's suggestion. It did not work for me, but in
the end I figured it out myself. The only problem I now have is that I want
to try to change the font size. My solution is however not working. Does
anyone have a suggestion?

Abel

Option Explicit
Const screenHeight As Single = 1024 ' Constant height of screen on which
this form was developed
Const screenWidth As Single = 1280 ' Constant width of screen on which
this form was developed

Private Sub UserForm_Initialize()
Dim curr_obj As Control
Dim iHeight As Long, iWidth As Long
Dim x_size As Single
Dim y_size As Single

' Get values screen size of user
iHeight = System.VerticalResolution
iWidth = System.HorizontalResolution

' Calculate resize factor
x_size = CSng(iWidth) / CSng(screenWidth)
y_size = CSng(iHeight) / CSng(screenHeight)

For Each curr_obj In Me.Controls
With curr_obj
.Left = .Left * x_size
.width = .width * x_size
.Top = .Top * y_size
.height = .height * y_size
End With
Next

' Change font size too!
Me.Font = SetFontSize(x_size)

' Change form size as well.
Me.width = Me.width * x_size
Me.height = Me.height * y_size

End Sub

'
' Change font size. Sad to say, it does nt work
'
Private Function SetFontSize(x_size) As Integer
If Int(x_size) > 0 Then
' Change font size
SetFontSize = Int(x_size * Cool
End If

End Function

"Jay Freedman" wrote:

> Hi Abel,
>
> The problem here is that you've grabbed some VB6 code and tried to plug it
> directly into VBA. That often works with code that isn't related to VB forms
> and VBA userforms. But VB forms are very different from VBA userforms, so
> that kind of code cannot be directly ported.
>
> The first thing you tripped over is that Form is not a valid object type in
> VBA; instead you need to use the specific UserForm1 or whatever you may have
> renamed that to. (In this case, you can't even use the generic 'UserForm'
> type.)
>
> Once you get that fixed, you would find that there is no Screen object in
> VBA; instead, the System object has the properties you need, named
> HorizontalResolution and VerticalResolution. Further, those two properties
> are measured in pixels, while the Height and Width properties of the
> UserForm1 object are measured in points. Fortunately, there's a built-in
> function to convert pixels to points. You may need to make some similar
> adjustments in the ResizeControls procedure.
>
> Finally, in VBA userform-land, the proper place to put code of this kind is
> not in the macro code that calls the userform, but in the procedure
> UserForm_Initialize within the userform itself. That procedure runs
> automatically between the call to the userform's .Show method and the time
> the userform actually appears on the screen. Since the procedure is in the
> userform, the built-in object Me refers to the userform, so you don't have
> to pass the userform as a parameter. The code would be
>
> Private Sub UserForm_Initialize()
> Me.Height = PixelsToPoints(System.VerticalResolution / 2)
> Me.Width = PixelsToPoints(System.HorizontalResolution / 2)
> ResizeControls
> End Sub
>
> The ResizeControls procedure can also be made part of the
> UserForm_Initialize procedure, and then instead of the line
>
> For Each curr_obj In frm
>
> you would use
>
> For Each curr_obj In Me.Controls
>
> Here's the whole enchilada:
>
> Private Sub UserForm_Initialize()
> Dim curr_obj As Control
> Dim iHeight As Long, iWidth As Long
> Dim x_size As Single
> Dim y_size As Single
>
> ' save initial values
> iHeight = Me.Height
> iWidth = Me.Width
>
> Me.Height = PixelsToPoints(System.VerticalResolution / 2)
> Me.Width = PixelsToPoints(System.HorizontalResolution / 2)
>
> ' calculate resize factors
> x_size = CSng(Me.Width) / CSng(iWidth)
> y_size = CSng(Me.Height) / CSng(iHeight)
>
> For Each curr_obj In Me.Controls
> With curr_obj
> .Left = .Left * x_size
> .Width = .Width * x_size
> .Top = .Top * y_size
> .Height = .Height * y_size
> End With
> Next
> End Sub
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so
> all may benefit.
>
>
> Abel MacAdam wrote:
> > Hi,
> >
> > I introduced a piece of code into my code. (see
> > http://www.dreamincode.net/forums/showtopic34776.htm)
> >
> > Part of the code generated an error:
> >
> > Public Sub ResizeForm(frm As Form)
> > 'Set the forms height
> > frm.height = Screen.height / 2
> > 'Set the forms width
> > frm.width = Screen.width / 2
> > 'Resize all of the controls
> > 'based on the forms new size
> > ResizeControls frm
> > End Sub
> >
> > I introduced this code so I got a form that would automatically resize
> > depending on the screen dimensions of the user.
> >
> > When I compile the code, I get a 'Compile error' on the following
> > line: 'Public Sub ResizeForm(frm As Form'
> > Notice it did not include the last bracket.
> >
> > How should I look at this error? What is wrong with this piece of
> > code. Is frm unknown, or is it Form. Or is a piece of this Sub used
> > without it being defined. I know frm is not defined, but is frm
> > something that should have been "known" by VB?
> >
> > This must be very basic. But I'm not that versed in Visual Basic.
> >
> > TIA,
> > Abel
> >
> > Note: FWIW, I'm using Word 2002/XP. My VB version is 6.3
>
>
>
Back to top
Login to vote
Jay Freedman

External


Since: Sep 16, 2003
Posts: 2309



(Msg. 5) Posted: Wed Mar 18, 2009 9:50 am
Post subject: Re: Explain Complie error [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi Abel,

Again, you have a couple of problems, with one hiding another.

The first problem is that Me.Font represents a Font object that has many
properties and methods. (Put your cursor on the word Font in your code in
the VBA window and press F1 to read the help topic about it.) You can't
assign an Integer value to a Font object -- you'll just get a compile error
about a 'type mismatch'. Instead, you need to assign that Integer to the
Size property of the font object.

If you assign it to the expression Me.Font.Size, though, apparently nothing
will happen. That's because Me.Font.Size applies only to the Font object of
the userform itself. Each control on the userform has its own Font object
that has its own Font.Size property, and they're all independent.

What you really need to do is to set the font size for each control
separately. Unfortunately, you can't do that in the For Each loop, because
the generic Control object doesn't include a Font object among its
properties. This won't work:

For Each curr_obj In Me.Controls
With curr_obj
.Left = .Left * x_size
' ...
.Font.Size = .Font.Size * x_size ' <-- compile error here!
End With
Next

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Abel MacAdam wrote:
> Hi all,
>
> I got the time to look into Jay's suggestion. It did not work for me,
> but in the end I figured it out myself. The only problem I now have
> is that I want to try to change the font size. My solution is however
> not working. Does anyone have a suggestion?
>
> Abel
>
> Option Explicit
> Const screenHeight As Single = 1024 ' Constant height of screen on
> which this form was developed
> Const screenWidth As Single = 1280 ' Constant width of screen on
> which this form was developed
>
> Private Sub UserForm_Initialize()
> Dim curr_obj As Control
> Dim iHeight As Long, iWidth As Long
> Dim x_size As Single
> Dim y_size As Single
>
> ' Get values screen size of user
> iHeight = System.VerticalResolution
> iWidth = System.HorizontalResolution
>
> ' Calculate resize factor
> x_size = CSng(iWidth) / CSng(screenWidth)
> y_size = CSng(iHeight) / CSng(screenHeight)
>
> For Each curr_obj In Me.Controls
> With curr_obj
> .Left = .Left * x_size
> .width = .width * x_size
> .Top = .Top * y_size
> .height = .height * y_size
> End With
> Next
>
> ' Change font size too!
> Me.Font = SetFontSize(x_size)
>
> ' Change form size as well.
> Me.width = Me.width * x_size
> Me.height = Me.height * y_size
>
> End Sub
>
> '
> ' Change font size. Sad to say, it does nt work
> '
> Private Function SetFontSize(x_size) As Integer
> If Int(x_size) > 0 Then
> ' Change font size
> SetFontSize = Int(x_size * Cool
> End If
>
> End Function
>
> "Jay Freedman" wrote:
>
Back to top
Login to vote
Abel MacAdam

External


Since: Dec 17, 2007
Posts: 10



(Msg. 6) Posted: Wed Mar 18, 2009 9:50 am
Post subject: Re: Explain Complie error [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi Jay,

Thanks again for your help.

So if I understand you correctly, "because the generic Control object
doesn't include a Font object among its properties" it will not work? Pity.
Well, I probably already have it whipped, by moving around the labels. Sad I
could not find a "nice" solution for this problem *shrug*.

Abel

"Jay Freedman" wrote:

> Hi Abel,
>
> Again, you have a couple of problems, with one hiding another.
>
> The first problem is that Me.Font represents a Font object that has many
> properties and methods. (Put your cursor on the word Font in your code in
> the VBA window and press F1 to read the help topic about it.) You can't
> assign an Integer value to a Font object -- you'll just get a compile error
> about a 'type mismatch'. Instead, you need to assign that Integer to the
> Size property of the font object.
>
> If you assign it to the expression Me.Font.Size, though, apparently nothing
> will happen. That's because Me.Font.Size applies only to the Font object of
> the userform itself. Each control on the userform has its own Font object
> that has its own Font.Size property, and they're all independent.
>
> What you really need to do is to set the font size for each control
> separately. Unfortunately, you can't do that in the For Each loop, because
> the generic Control object doesn't include a Font object among its
> properties. This won't work:
>
> For Each curr_obj In Me.Controls
> With curr_obj
> .Left = .Left * x_size
> ' ...
> .Font.Size = .Font.Size * x_size ' <-- compile error here!
> End With
> Next
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the newsgroup so
> all may benefit.
>
> Abel MacAdam wrote:
> > Hi all,
> >
> > I got the time to look into Jay's suggestion. It did not work for me,
> > but in the end I figured it out myself. The only problem I now have
> > is that I want to try to change the font size. My solution is however
> > not working. Does anyone have a suggestion?
> >
> > Abel
> >
> > Option Explicit
> > Const screenHeight As Single = 1024 ' Constant height of screen on
> > which this form was developed
> > Const screenWidth As Single = 1280 ' Constant width of screen on
> > which this form was developed
> >
> > Private Sub UserForm_Initialize()
> > Dim curr_obj As Control
> > Dim iHeight As Long, iWidth As Long
> > Dim x_size As Single
> > Dim y_size As Single
> >
> > ' Get values screen size of user
> > iHeight = System.VerticalResolution
> > iWidth = System.HorizontalResolution
> >
> > ' Calculate resize factor
> > x_size = CSng(iWidth) / CSng(screenWidth)
> > y_size = CSng(iHeight) / CSng(screenHeight)
> >
> > For Each curr_obj In Me.Controls
> > With curr_obj
> > .Left = .Left * x_size
> > .width = .width * x_size
> > .Top = .Top * y_size
> > .height = .height * y_size
> > End With
> > Next
> >
> > ' Change font size too!
> > Me.Font = SetFontSize(x_size)
> >
> > ' Change form size as well.
> > Me.width = Me.width * x_size
> > Me.height = Me.height * y_size
> >
> > End Sub
> >
> > '
> > ' Change font size. Sad to say, it does nt work
> > '
> > Private Function SetFontSize(x_size) As Integer
> > If Int(x_size) > 0 Then
> > ' Change font size
> > SetFontSize = Int(x_size * Cool
> > End If
> >
> > End Function
> >
> > "Jay Freedman" wrote:
> >
>
>
>
Back to top
Login to vote
Jay Freedman

External


Since: Sep 16, 2003
Posts: 2309



(Msg. 7) Posted: Wed Mar 18, 2009 12:51 pm
Post subject: Re: Explain Complie error [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I didn't mean that you can't change the font size of the controls, just that
you can't do it in the loop for all controls. You would have to change the
font size of each control individually:

Label1.Font.Size = Label1.Font.Size * x_size
CommandButton1.Font.Size = CommandButton1.Font.Size * x_size

and so on. If you add or remove controls, you must also edit the code to add
or remove the resizing statements to match. This is unfortunate but
necessary.


Abel MacAdam wrote:
> Hi Jay,
>
> Thanks again for your help.
>
> So if I understand you correctly, "because the generic Control object
> doesn't include a Font object among its properties" it will not work?
> Pity. Well, I probably already have it whipped, by moving around the
> labels. Sad I could not find a "nice" solution for this problem
> *shrug*.
>
> Abel
>
> "Jay Freedman" wrote:
>
>> Hi Abel,
>>
>> Again, you have a couple of problems, with one hiding another.
>>
>> The first problem is that Me.Font represents a Font object that has
>> many properties and methods. (Put your cursor on the word Font in
>> your code in the VBA window and press F1 to read the help topic
>> about it.) You can't assign an Integer value to a Font object --
>> you'll just get a compile error about a 'type mismatch'. Instead,
>> you need to assign that Integer to the Size property of the font
>> object.
>>
>> If you assign it to the expression Me.Font.Size, though, apparently
>> nothing will happen. That's because Me.Font.Size applies only to the
>> Font object of the userform itself. Each control on the userform has
>> its own Font object that has its own Font.Size property, and they're
>> all independent.
>>
>> What you really need to do is to set the font size for each control
>> separately. Unfortunately, you can't do that in the For Each loop,
>> because the generic Control object doesn't include a Font object
>> among its properties. This won't work:
>>
>> For Each curr_obj In Me.Controls
>> With curr_obj
>> .Left = .Left * x_size
>> ' ...
>> .Font.Size = .Font.Size * x_size ' <-- compile error
>> here! End With
>> Next
>>
>> --
>> Regards,
>> Jay Freedman
>> Microsoft Word MVP FAQ: http://word.mvps.org
>> Email cannot be acknowledged; please post all follow-ups to the
>> newsgroup so all may benefit.
>>
>> Abel MacAdam wrote:
>>> Hi all,
>>>
>>> I got the time to look into Jay's suggestion. It did not work for
>>> me, but in the end I figured it out myself. The only problem I now
>>> have is that I want to try to change the font size. My solution is
>>> however not working. Does anyone have a suggestion?
>>>
>>> Abel
>>>
>>> Option Explicit
>>> Const screenHeight As Single = 1024 ' Constant height of screen
>>> on which this form was developed
>>> Const screenWidth As Single = 1280 ' Constant width of screen on
>>> which this form was developed
>>>
>>> Private Sub UserForm_Initialize()
>>> Dim curr_obj As Control
>>> Dim iHeight As Long, iWidth As Long
>>> Dim x_size As Single
>>> Dim y_size As Single
>>>
>>> ' Get values screen size of user
>>> iHeight = System.VerticalResolution
>>> iWidth = System.HorizontalResolution
>>>
>>> ' Calculate resize factor
>>> x_size = CSng(iWidth) / CSng(screenWidth)
>>> y_size = CSng(iHeight) / CSng(screenHeight)
>>>
>>> For Each curr_obj In Me.Controls
>>> With curr_obj
>>> .Left = .Left * x_size
>>> .width = .width * x_size
>>> .Top = .Top * y_size
>>> .height = .height * y_size
>>> End With
>>> Next
>>>
>>> ' Change font size too!
>>> Me.Font = SetFontSize(x_size)
>>>
>>> ' Change form size as well.
>>> Me.width = Me.width * x_size
>>> Me.height = Me.height * y_size
>>>
>>> End Sub
>>>
>>> '
>>> ' Change font size. Sad to say, it does nt work
>>> '
>>> Private Function SetFontSize(x_size) As Integer
>>> If Int(x_size) > 0 Then
>>> ' Change font size
>>> SetFontSize = Int(x_size * Cool
>>> End If
>>>
>>> End Function
>>>
>>> "Jay Freedman" wrote:
Back to top
Login to vote
Abel MacAdam

External


Since: Dec 17, 2007
Posts: 10



(Msg. 8) Posted: Wed Mar 25, 2009 2:19 am
Post subject: Re: Explain Complie error [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks. It worked.

"Jay Freedman" wrote:

> I didn't mean that you can't change the font size of the controls, just that
> you can't do it in the loop for all controls. You would have to change the
> font size of each control individually:
>
> Label1.Font.Size = Label1.Font.Size * x_size
> CommandButton1.Font.Size = CommandButton1.Font.Size * x_size
>
> and so on. If you add or remove controls, you must also edit the code to add
> or remove the resizing statements to match. This is unfortunate but
> necessary.
>
>
> Abel MacAdam wrote:
> > Hi Jay,
> >
> > Thanks again for your help.
> >
> > So if I understand you correctly, "because the generic Control object
> > doesn't include a Font object among its properties" it will not work?
> > Pity. Well, I probably already have it whipped, by moving around the
> > labels. Sad I could not find a "nice" solution for this problem
> > *shrug*.
> >
> > Abel
> >
> > "Jay Freedman" wrote:
> >
> >> Hi Abel,
> >>
> >> Again, you have a couple of problems, with one hiding another.
> >>
> >> The first problem is that Me.Font represents a Font object that has
> >> many properties and methods. (Put your cursor on the word Font in
> >> your code in the VBA window and press F1 to read the help topic
> >> about it.) You can't assign an Integer value to a Font object --
> >> you'll just get a compile error about a 'type mismatch'. Instead,
> >> you need to assign that Integer to the Size property of the font
> >> object.
> >>
> >> If you assign it to the expression Me.Font.Size, though, apparently
> >> nothing will happen. That's because Me.Font.Size applies only to the
> >> Font object of the userform itself. Each control on the userform has
> >> its own Font object that has its own Font.Size property, and they're
> >> all independent.
> >>
> >> What you really need to do is to set the font size for each control
> >> separately. Unfortunately, you can't do that in the For Each loop,
> >> because the generic Control object doesn't include a Font object
> >> among its properties. This won't work:
> >>
> >> For Each curr_obj In Me.Controls
> >> With curr_obj
> >> .Left = .Left * x_size
> >> ' ...
> >> .Font.Size = .Font.Size * x_size ' <-- compile error
> >> here! End With
> >> Next
> >>
> >> --
> >> Regards,
> >> Jay Freedman
> >> Microsoft Word MVP FAQ: http://word.mvps.org
> >> Email cannot be acknowledged; please post all follow-ups to the
> >> newsgroup so all may benefit.
> >>
> >> Abel MacAdam wrote:
> >>> Hi all,
> >>>
> >>> I got the time to look into Jay's suggestion. It did not work for
> >>> me, but in the end I figured it out myself. The only problem I now
> >>> have is that I want to try to change the font size. My solution is
> >>> however not working. Does anyone have a suggestion?
> >>>
> >>> Abel
> >>>
> >>> Option Explicit
> >>> Const screenHeight As Single = 1024 ' Constant height of screen
> >>> on which this form was developed
> >>> Const screenWidth As Single = 1280 ' Constant width of screen on
> >>> which this form was developed
> >>>
> >>> Private Sub UserForm_Initialize()
> >>> Dim curr_obj As Control
> >>> Dim iHeight As Long, iWidth As Long
> >>> Dim x_size As Single
> >>> Dim y_size As Single
> >>>
> >>> ' Get values screen size of user
> >>> iHeight = System.VerticalResolution
> >>> iWidth = System.HorizontalResolution
> >>>
> >>> ' Calculate resize factor
> >>> x_size = CSng(iWidth) / CSng(screenWidth)
> >>> y_size = CSng(iHeight) / CSng(screenHeight)
> >>>
> >>> For Each curr_obj In Me.Controls
> >>> With curr_obj
> >>> .Left = .Left * x_size
> >>> .width = .width * x_size
> >>> .Top = .Top * y_size
> >>> .height = .height * y_size
> >>> End With
> >>> Next
> >>>
> >>> ' Change font size too!
> >>> Me.Font = SetFontSize(x_size)
> >>>
> >>> ' Change form size as well.
> >>> Me.width = Me.width * x_size
> >>> Me.height = Me.height * y_size
> >>>
> >>> End Sub
> >>>
> >>> '
> >>> ' Change font size. Sad to say, it does nt work
> >>> '
> >>> Private Function SetFontSize(x_size) As Integer
> >>> If Int(x_size) > 0 Then
> >>> ' Change font size
> >>> SetFontSize = Int(x_size * Cool
> >>> End If
> >>>
> >>> End Function
> >>>
> >>> "Jay Freedman" wrote:
>
>
>
Back to top
Login to vote
Display posts from previous:   
       Home -> Office -> User Forms All times are: Eastern Time (US & Canada) (change)
Goto page 1, 2
Page 1 of 2

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Categories:
 Windows XP
 Windows Vista
 Windows Other
  Office
 Office Other
 Security
  • Home |
  • Shareware |
  • Windows Tips |
  • Hot Offers |
  • FREE Newsletters |
  • Arcade |
  • Forums |
  • eBooks |
  • About WUGNET |
  • Partners |
  • Contact

  • WUGNET Privacy Policy |
  • Link to WUGNET |
  • IT Support