PDA

View Full Version : Solved: Bold some text a string



CCkfm2000
02-15-2011, 09:53 AM
hello,
I'm new to word vba and would like some help with this problem.

when the user enters data in an inputbox, I would like part of the text to be in bold.

here's some example for data which could be inputted.

000 Basic Adult Resuscitation Non-Registered 09-May-2011 REC 10:20
000 Manual Handling Practical Update Registered 09-Jun-2011 SEC 10:20
000 Basic Adult Resuscitation Non-Registered 09-May-2011
000 Manual Handling Practical Update Registered 09-Jun-2011 SEC

the 1st part 000 is the id
the 2nd part is the course name
the 3rd part is date
the 4th part is the location
the 5th part is the time

as you can see from the example above the formats is not always the same.

I want to bold the 3rd, 4th and 5th part

Thanks for you help

fumei
02-15-2011, 10:11 AM
Directly from the InputBox, this is not possible. Inputbox returns a non-formatted string. Parts (nor all) of it can not be Bold, or any other format.

The only thing you can do is take the returned string from the InputBox, and then logically figure out what you want to format after the fact.

CCkfm2000
02-15-2011, 10:31 AM
sorry can you give me an example.:dunno

Tinbendr
02-15-2011, 11:31 AM
What are you doing with the value from the Inputbox?

You will have to handle the bold AFTER inserting it into the document.

Because the components are random in length/type, it will be a challange to do this with code.

David

CCkfm2000
02-15-2011, 12:31 PM
the inputbox is oble use to input some info and write back into word.

the idea is that i've got a macro which i run and it will ask the user to input some information this will create a letter in word, i've got most of this working
only thing thats left is to get this part.


how can i split the data into two?

000 Basic Adult Resuscitation Non-Registered

and

09-May-2011 REC 10:20

this might be the way to go!!!

fumei
02-15-2011, 01:19 PM
This is not trivial, and YOU will have to work out the entire logic.

To repeat, to Word (and more accurately, the Inputbox) there is absolutely NO difference between:

000 Basic Adult Resuscitation Non-Registered

and

09-May-2011 REC 10:20

- except of course the length of the string. Therefore if you want to do something with the string, you have to determine EXACTLY what the logic is.

So. You have this string:

000 Basic Adult Resuscitation Non-Registered 09-May-2011 REC 10:20

To Word, these are ALL just ASCII characters. They have no meaning, nor structure. Now if the date portion was a REAL date (i.e. a field), then you could work with that, but it is not. It is simply text, exactly the same as any other part of the string.

The point I am making is that:

the 1st part 000 is the id
the 2nd part is the course name
the 3rd part is date
the 4th part is the location
the 5th part is the time

means nothing to Word. Those are "parts" in your mind. There are no "parts". IF all of them had the "location and time (REC 10:20), then you could subtract 9 characters (REC 10:20), to get:

000 Basic Adult Resuscitation Non-Registered 09-May-2011

but then because of the variability of the date "part", you can not easily get further extractions of what you want.

In other words, while theoretically possible, it would take massive amounts of fiddly logic (with no guarantee it would work in all cases).

May I suggest a userform? That way you could build up your inserted text one at at time (ID, then course name, then date etc.). As they would be inserted into the document separately, then could be formatted - again, after the fact.

CCkfm2000
02-15-2011, 01:32 PM
Thanks for the explanation, will try a userform.

Thankyou all for your help

Frosty
02-17-2011, 10:09 AM
A user form is the best way to do it... but if you wanted to try a proof of concept, you could still use the input box... just explain in the input box that you need to separate your "fields" with a special character.

My suggestion would be the pipe character.

So instead of:
000 Basic Adult Resuscitation Non-Registered 09-May-2011 REC 10:20

You could instruct your users to input:
000|Basic Adult Resuscitation|Non-Registered|09-May-2011|REC 10:20

But I agree that a custom user form is probably the best way to go, although adding some text to your input box telling users to separate the input with a pipe character would certainly be the quick and dirty solution.

fumei
02-17-2011, 11:26 AM
Forsty, that is a very good idea, and may be worth a try.

CCkfm2000
02-18-2011, 12:01 AM
i've gone with a useform now and got the user to split the data.:thumb

Shred Dude
02-18-2011, 05:00 PM
If Your data from the Input Box will always be similar to as you've shown, there does seem to be a discernable pattern that you could leverage. Your last 3 "parts" as you call them, do not have any spaces in them.

Thus, you might be able to employ the SPLIT function to identify the pieces you want.

For example:

Public Sub splitparts()
Dim strInput As String
Dim parts As Variant
Dim last As Long
Dim strLast3 As String

strInput = InputBox("Do your thing...")

parts = Split(strInput, " ")

last = UBound(parts)

strLast3 = parts(last - 2) & " " & parts(last - 1) & " " & parts(last)

'now you can insert strLast3 and apply formatting as desired...


End Sub


As suggested earlier however, a userform will give you much more control over the process, as well as facilitating some drop down choices for the users. Seems like a lot to have to type in an InputBox.

Keep the Split function in mind though, its often very handy.