Random Font

thenman

Registered User.
Local time
Today, 04:32
Joined
Aug 29, 2009
Messages
23
I have been searching for an answer to this all day, I hope someone here can help.

I am trying to write code in Access 2007 VBA. What I want this code to do is to randomize the font properties of a Rich Text Box. (I know this is possible in Excel via the .Character, but not in Access)

To break it down as simply as I can...

I have 5 fonts to choose from: Lets say Arial, Times, Calibri, Custom, Other. For each character in my RTF textbox (textbox1) I want a font to be randomly chosen...

So the word Supercalifonication would have a random font from these 5 fonts assigned to each letter in the word (and for every letter of every other word in the box, including spaces unless it causes errors). It would be nice to control font size as well as a few other properties. If this can be done with a "With" statement I am sure I can figure it out.



Thank you in advance for any help you provide. If you can sort this out with me, I will be eternally greatfull.
 
Maybe my code thus far will help...

Dim font1 As String
Dim font2 As String
Dim font3 As String
Dim FontR As String
Dim FontNum As String
Dim Counter As Integer
Dim MyString As String

FontNum = Int((3 - 1 + 1) * Rnd + 1)

font1 = "Custom"
font2 = "Arial"
font3 = "Chiller"
FontR = "Font" & FontNum


MyString = OWNERADR 'define string
For Counter = 1 To Len(MyString)
'do something to each character in string
Text4 = Text4 & " " & Mid(MyString, Counter, 1)' Breaks characters up, and then adds them one after another to the box.
-----The bolded text could be part of the problem, I see that now

----With each character in the richtext box
.font= fontR
.fontsize = 12
------------Etc....

Next


I have read about using a datagrid to accomplish this. Let me know if any of you have any ideas.

Thanks again.
 
No one here has an answer? I thought there were experts on this site.... Guess I was wrong. Maybe it is because I am a newbie. Either way, thanks for the help.
 
No one here has an answer? I thought there were experts on this site.... Guess I was wrong. Maybe it is because I am a newbie. Either way, thanks for the help.
That attitude is unlikely to persuade people to help you. Remember that a lot of the experts don't visit the site over the weekend.
 
Hi

I'd agree with Rabbie. The whole idea of the forum is to help not do. It'd be so easy to have a portal where you ask a question and the answer magically appears. Can you give me next next weeks lottery numbers? It can't be that hard, it's just a bunch of numbers.......

Anyway,

you have offered an example of what your doing which is more than most so you may have just save a little redemption :)

I would

set a variable to get a random number between 1 & 5

Code:
Dim intRndNum As Integer
Dim strUseFont As String
intRndNum = Int(5*rnd)

then use a select to get the font
Code:
select intRndNum

case 1
strUseFont ="arial"
case 2
strUseFont ="times new roman"
case 3
strUseFont ="other font"
case 4
strUseFont ="other font"
case 5
strUseFont ="other font"
case else
strUseFont ="arial"
end select

then, use strUseFont as the font to use

you would save this as a seperate routine and call it everytime you wanted a random font
 
That attitude is unlikely to persuade people to help you. Remember that a lot of the experts don't visit the site over the weekend.
also remember too, that a lot of experts don't even exist, because in reality, they are not "experts"...they are students just like you


and NIGEL...just one thing....dont' forget the RANDOMIZE statement before the rnd() function in your code. necessary in order to reset the seed, so you don't risk getting the same number all the time, remember? ;)
 
Last edited:
Ha haaa

I must have missed that bit out. Did it on my phone see!

Nidge
 
Ha haaa

I must have missed that bit out. Did it on my phone see!

Nidge
is that still legal while driving in this country? have any states banned that yet, or have they still left it on the back burner for the next congress session? last i hear, the cellular-while-driving problem was going to be tackled during the next calendar year in the US...
 
You know, you may just be right. However, I live on a farm and had just got back from a long horse ride. Fortunately for me, I get away with that one ;)

nidge
 
First, thanks to both of you for your answers. I am sorry to sound rude, it just gets frustrating searching literally 20+ hours for an answer to one tiny piece of code. I am sure everyone here can relate. It is hard to see that your post has been viewed 15 or more times and to not even have one person says as little as "yep that sucks to be you".

Here is what I have gone for:
I shifted gears to just sending the data to excel and controling it through there. After reading countless posts and combing the MSDN, I couldn't find any way to control the text in a RTF Textbox (I am a newbie though, so take that for what it is worth). I tried to use an unbound excel object in my access file, but it sucked and I moved to opening an instance of excel and controling it. Now I should have the functionality that I wanted, at the expense of having to open open, update the workbook, print it and close excel for each selected record. Not pretty but it will get the job done for now.

I will post the code once I am finished. Maybe it will help someone else later on down the line. Even better, maybe someone will know a way to do it in access.... :) that would be great.

To NigelShaw and ajetrumpet, the random code suggestion is better than what I was trying to use, and it is something I needed for this new code. So... thank you both again for the help. I will let you know how it goes once I get there.

Thanks again.
 
Hiya mate

ate you trying to randomize the font of the whole field or randomize the text in the field? In other words,

will the field have one random font or many fonts in the one field? One thing to look out for is the rtf field will store fonts set inside the field as html and this is included in the character count / field length so if this is a text field, the max is 255. The text format gets saved as

"["font= "arial""]" your text here "["/font"]"
(without the "quotes". Html code wanted to change the font in the post so this was the only to show the format!)

let me know exactly what you are trying to achieve and I'll do a quick demo

nidge
 
Last edited:
The field will have many random fonts.

Here is why, so that you understand what it is that I'm trying to accomplish...

I have a business that sends out handwritten letters to people. Each letter is the same format, with only the name and their address changing. Writing 100 letters a week is a chore. So, I scanned in the letter, and left the name and address blank on the scanned letter. Now all I have to do is print the letter out and fill in the persons first name and their street address. Now, on the envelope, the return address is always the same so I scanned that in as well, but again, the name and full address of the person needs to be written in. This is still more than I want to do, and I expect that I will be sending out a larger volume of these letters as time goes on. I figured I would "programatically" solve this problem before it got to be too big.

Obviously when you write, every similar character is written a little bit different... a little bigger, smaller, wider, thinner. The only way I could think to *Easily* (and within my programing capabilites) accomplish this is to have a set of 5 fonts created from my handwriting. Each one would still be my handwriting, but by having 5 samples of my handwriting, I could add the human aspect because no two fonts would look exactly the same. When each line of the address parsed, the font for each character would be randomly assigned, making the text look semi-sloppy and random, like a human.

Now to anwser the question about the RTF Box itself. There are three fields on the envelope that will be filled in (in the format of an address on an envelope in the U.S.)....

RTFbox1 = Owner Name
RTFBox2 = Owner Address
RTFbox3 = Owner City & ", " & Owner State & " " & Owner Zip

Each RTFbox wouldn't approach the 255 mark, even with HTML written in the box


By the way, when ajetrumpet said that you needed to add the RANDOMIZE statement before the rnd(), what did he mean? I am not sure how that code is written correctly so the ammended example would help.

Thanks guys.
 
Last edited:
By the way, for those of you that are wondering why go through all of the trouble of making something look handwritten... this is marketing material that I am sending out. I have a tenfold response rate for my marketing if the material is handwritten VS. typed. The scanned letter looks EXACTLY like a handwritten letter when I print it out, and I would like to be able to do the rest of the task with a computer and have it look perfect or almost perfect. It has to be, or people won't respond.
 
Hi

I'll put something up tonight however, I'm surprised you haven't downloaded the free handwriting font creator that let's you create your own handwritten font. That would be much easier I think.

I'll put a link up later too

Nigel
 
I have fontstudio to create the fonts of my handwriting. The problem is, if I have a word like "ladder" you can see the two D's are exactly the same... (they are suppose to be, its a font). That is why I am making more than one font, so that there is variation between characters that are the same. Make sense. I am sure it seems over the top, but it costs me quite a bit of time and money to do this marketing. It needs to be as close to handwritten as possible. If they see that it is a computer doing the writing, they think I don't care about them and they won't call.
 
Hi

so I'm going to guess then you have created 5 custom fonts based on your handrwiting yes?

I'll do a demo that can sift through a rtf field etc etc. I need to put my kiddie to bed first though.


Regs

nidge
 
Yes I do. Thanks for all of the help, i appreciate it
 
Ok,

ive come up with this. db attached.

i have set this up so you type in a standard plain text field and then press the button to randomize the fonts in the RTF below.

1. make this public ( under Option Explicit )
Code:
Public TargetChar As String
Public strFormattedString As Variant

2. add this to your form. it reads your field per letter and stores it with html
Code:
Public Sub ChangeFont()
    Dim i As Integer
    
    On Error GoTo ChangeFont_ERR

    'CLEAR THE PUBLIC VALUES TO PREVENT DATA BUILDUP
    strFormattedString = ""
    Me.txtMemoTarget.Value = ""
    TargetChar = ""
    
    'SET FOCUS TO THE TEXT BOX YOU WILL TYPE IN
    txtOrigin.SetFocus

    'CREATE A COUNTER ACCORDING TO THE FIELD TEXT LENGTH
    For i = 1 To Nz(Trim(Len(Me.txtOrigin)), 0)
    'SET THE CURSOR TO THE BEGINNING
        txtOrigin.SelStart = i
    'SET UP THE CURSOR TO READ ONLY ONLY 1 CHARACTER AT A TIME
        Select Case i
        Case 1
            TargetChar = Left(txtOrigin, i)
            ButtonPress
        Case Nz(Trim(Len(Me.txtOrigin)), 0)
            TargetChar = Right(txtOrigin, 1)
            ButtonPress
        Case Else
            TargetChar = Mid(txtOrigin, i, 1)
            ButtonPress
        End Select
        'MOVE THE CURSOR TO THE NEXT LETTER
        txtOrigin.SelStart = i + 1
    Next i
    'FILL THE RTF BOX WITH RANDOM FONT
    FillBox
    
    Exit Sub
    
ChangeFont_ERR:
    MsgBox ("There has been an error changing your font!")

End Sub
3. add this to your form ( it creates the html string and randomizes your font )

Code:
Private Sub ButtonPress()
    Dim Font As String
    Dim strFontEnd As String
    Dim intUserRand As Integer
    
    On Error GoTo ButtonPress_ERR
    
    'SET UP RANDOM NUMBERS BETWEEN 1 & 5
    intUserRand = Int((5 * Rnd) + 1)
    'CREATE A HTML END
    strFontEnd = "</Font>"
    
    'SET THE VARIABLE "FONT" TO A RANDOM FONT
    Select Case intUserRand
    Case 1
        Font = "<font face=" & "Arial" & ">"
    Case 2
        Font = "<font face=" & "Calibri" & ">"
    Case 3
        Font = "<font face=" & "Corbel" & ">"
    Case 4
        Font = "<font face=" & "Gill Sans MT" & ">"
    Case 5
        Font = "<font face=" & "Tahoma" & ">"
    Case Else
        Font = "<font=" & "Arial" & ">"
    End Select

    'BUILD THE STRING
    strFormattedString = strFormattedString & Font & TargetChar & strFontEnd
    
    Exit Sub
    
ButtonPress_ERR:
    MsgBox ("there has been an error filling in the RTF field!")

End Sub
4. add this to your form ( it adds the updated string to the RTF )

Code:
Private Sub FillBox()
Me.txtMemoTarget.Value = strFormattedString
End Sub

5. use this on the button

Code:
ChangeFont

hope this helps you and please let me know how you get on.

regs


Nidge
 

Attachments

You my friend, ARE THE SHIT. You are a cool version of the mister wizard of access programming. You solved my enigma! It works beautifully, and from this I think I can create the rest of the fuctionality I need. Thank you so much. And no need to use excel, I think I was about to open up another can of worms.

Thanks again, awesome!
 
Last edited:
so for anyone who runs into this and needs to use it, rename txtOrigin to the name of your initial textbox (the one the text is comming from), and txtMemoTarget to the name of the textbox that you want the letters in.
 

Users who are viewing this thread

Back
Top Bottom