Results 1 to 5 of 5

Thread: Passing Function Values

  1. #1
    Frenzied Member
    Join Date
    Aug 09
    Location
    Los Angeles
    Posts
    1,308

    Passing Function Values

    I need a little help with using a Function

    I have put this in a module:


    Code:
    Public Function FontInfo()
    
    Application.Dialogs(xlDialogFontProperties).Show
    
        Worksheets("Sheet1").Range("AC1").Select
        EmailForm.TextBox3.Font.name = Selection.Font.name
        EmailForm.TextBox3.Font.size = Selection.Font.size
        EmailForm.TextBox3.Font.Bold = Selection.Font.Bold
        EmailForm.TextBox3.Font.Italic = Selection.Font.Italic
        EmailForm.TextBox3.ForeColor = Selection.Font.ColorIndex
    
    Dim Fcolor As Integer, fsize As String, fstyle As String
        Fcolor = Selection.Font.ColorIndex
        fsize = Selection.Font.size
        fstyle = Selection.Font.name
    
    End Function
    And trying to get the values here:

    Private Sub CommandButton4_Click()

    Dim c As Integer, s As Integer, n As String

    c = FontInfo(Fcolor)

    s = FontInfo(fsize)

    n = FontInfo(fname)

    MsgBox Str(s)
    MsgBox Str(s)
    MsgBox (n)

    End Sub


    The come up empty

    Can someone show me what I am doing wrong?

  2. #2
    Frenzied Member
    Join Date
    Aug 09
    Location
    Los Angeles
    Posts
    1,308

    Re: Passing Function Values

    I think I solved it, at least its working for what I need. If there is abetter/more correct way of doing this please let me know:


    Code:
    Public Type FontInfo
    Fcolor As Integer
    Fsize As Integer
    Fname As String
    End Type
    
    Public Function FontInfo() As FontInfo
    
    FontInfo.Fcolor = EmailForm.TextBox3.ForeColor
    FontInfo.Fsize = EmailForm.TextBox3.Font.size
    FontInfo.Fname = EmailForm.TextBox3.Font.Name
    
    End Function

  3. #3
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,528

    Re: Passing Function Values

    using a type instead of 3 variables is a very good idea, you could, if needed, also use variables to hold different instances of your type
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    Frenzied Member
    Join Date
    Aug 09
    Location
    Los Angeles
    Posts
    1,308

    Re: Passing Function Values

    Quote Originally Posted by westconn1 View Post
    using a type instead of 3 variables is a very good idea, you could, if needed, also use variables to hold different instances of your type
    Thank you it seemed like the best approach but just for my own knowledge can you give me an example using variables?

  5. #5
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,528

    Re: Passing Function Values

    Public Type FontInfo
    Fcolor As Integer
    Fsize As Integer
    Fname As String
    End Type

    Public Function FontInfo() As FontInfo

    FontInfo.Fcolor = EmailForm.TextBox3.ForeColor
    FontInfo.Fsize = EmailForm.TextBox3.Font.size
    FontInfo.Fname = EmailForm.TextBox3.Font.Name

    End Function
    note there are some issues with your code as posted that i failed to notice before
    you should not have a procedure name the same as the name for your type, fcolor may overflow as integer, change to long, the values for fontinfo are not retained, but returned each time fontinfo is referenced, thiat is it always gets the current state

    assuming you want to save the vales for future reference, try like
    vb Code:
    1. Public Type FontInfo
    2. Fcolor As Long
    3. Fsize As Integer
    4. Fname As String
    5. End Type
    6. Public tbfi As FontInfo     ' public is only required if you need to be able to access this variable from any other form or module, else use Dim
    7.  
    8. Public Sub getFontInfo()
    9.  
    10. tbfi.Fcolor = UserForm1.TextBox1.ForeColor
    11. tbfi.Fsize = UserForm1.TextBox1.Font.Size
    12. tbfi.Fname = UserForm1.TextBox1.Font.Name
    13.  
    14. End Sub
    15. Sub anyprocedure()
    16. getFontInfo     ' use this to get the font state at any time, the values are retained
    17. End Sub
    18. Sub any()
    19. MsgBox tbfi.Fname   ' use like this to return the retained values at any time from any procedure
    20. ' or return the textbox to original state
    21. End Sub

    you can define several variables to retain the font values from different points to return later, just declare them all as type fontinfo as above
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •