Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can macro's refer to a setup (ini) file to define colors and categorie 1

Status
Not open for further replies.

AlexFeddes

Programmer
Jan 17, 2009
22
NL
I want to distribute a template (*.dot file) to enable people to automate certain tasks of marking selected text and copying that to an overview in the document.

I have written certain code to color and add a symbol to selected text, then automatically placing this selected text in a section of the document with a category called "Hightlight (**)" - thereby trying to gather and categorise notes that I glean from text.

I have received some feedback that people already use colorcoding and would like to use their own colors and categories instead of the ones I've defined - is there a way to use a setup document / file with definitions which colors and categories to use

A sample of the code for one categorie is as follows:
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = wdColorYellow
Selection.Font.ColorIndex = wdAuto
With Selection.Font
.Bold = True
End With

Dim Msg, Style, Title, Response, MyString
Msg = "Wilt u de geselecteerde text markeren EN kopiëren naar de sectie 'Studie overzicht'? Klik 'No' als u alleen de geselecteerde text wilt markeren."
Style = vbYesNo + vbDefaultButton1
Title = "Kopieer HIGHLIGHT naar Studie overzicht" '> Replace Word

Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then
Selection.Copy

Selection.Find.ClearFormatting
With Selection.Find
.Text = "Highlight (**)" '> Replace Word
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute

Selection.MoveUp Unit:=wdLine, Count:=1
' Selection.TypeText Text:=Chr(11) & Chr(11)
Selection.TypeParagraph
Selection.TypeParagraph
Selection.MoveUp Unit:=wdLine, Count:=1

Selection.PasteAndFormat (wdPasteDefault)


Else
Selection.MoveLeft Unit:=wdCharacter, Count:=1

'> Replace sectie
Selection.InsertSymbol Font:="Wingdings", CharacterNumber:=-4030, Unicode _
:=True
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Font.Superscript = True
Selection.Font.Bold = True
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = wdColorYellow
Selection.Font.ColorIndex = wdAuto

Selection.MoveRight Unit:=wdCharacter, Count:=1
End If


The sections I want to refer to in an setup file are:

Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = wdColorYellow
Selection.Font.ColorIndex = wdAuto

Selection.Find.ClearFormatting
With Selection.Find
.Text = "Highlight (**)"

Hope you understand my question.

Regards,
Alex
 
It looks like you only have 1 color: yellow. Why not just use an input box to get the color from the user?

_________________
Bob Rashkin
 
Hello Bob,
in actual fact I have about 20 colors and categories - I just gave one sample.

Is there a way to call up a msg box to set this parameter? In other words to have a macro that changes the color / category?

Regards,
Alex
 
There is. I can't find a "color palette" dialog in Word VBA so you'd have to build your own. I assume there are some set categories to which the user would assign colors. I'd use ...ColorIndex rather than ...Color since you can just use a number. I don't really understand your process but I guess I'd use an InputBox whose prompt lists the categories, or asks for 1-20 if the categories are "known", then another InputBox to set the color for that category.

_________________
Bob Rashkin
 
The short answer to your question is yes. You could store category-to-color assignments in a .ini file and refer to that.

Now, you write that you gave a sample of one category. Do you mean to say that you have 19 other chunks of code that are essentially identical to that, except for the color?
 
Hello Mintjulep and Bong,
thanks for your answers.

It's true that I've duplicated whole chunks of code to achieve what I wanted to achieve so far - minus the flexibility.

An ini file would really be great, though it probably wouldn't change the reference in my personal toolbar to 1 of the 20 color / categories.

I'll give you an example of a few of my categories:
1. application (Alt A)
2. question (Ctrl Shift ?)
3. warning (Alt W)
4. principle (Alt P)

These appear on my personal toolbar and are assigned with a shortcut key as listed above.

So either when I click on the toolbar or use the shortcut key, it sets this macro in motion: coloring, copying and pasting at a selected location in my study file.

I would be wonderfully helped if I could achieve with an ini file these 3 things:
1. assign color to font and background
2. assing names to categories
3. update the category name in my toolbar

Perhaps a fourth thing would be possible too?
4. Assign your own shortcut key to this macro function

I'm sorry to tell it's hard to show you fully what I've done, as I've written my website and study tool functions initially in Dutch. w.acfstudies.nl

If I get ahead and people are helped by these automations I will re-write in English too.

Thanks for your replies and interest so far.

Alex.
 
So, there are two programming issues:
1. reading a text file
2. using variables to assign colors

First, lets say your text (ini) file comprises lines like: varname = varvalue

Code:
    fname = [red]<your file name>[/red]
    dim parmcll as new collection
    Open fname For Input As #1
    Line Input #1, a;'now "a" is a variable containing the first line
    parmarr = a.split("=")
    parmcll.add item:=parmarr(1), key:=parmarr(0)
    Line Input #1, a;'now the 2nd line

now you have a collection whose keys are the variable names and whose items are the values.

In your macros, you need to substitute absolute color assignments with those variable names.

_________________
Bob Rashkin
 
Hello Bob & Tony,
thanks once more for advice, though it is a little beyond me as is.

The below code text should be in the ini text file or ...?
fname = <your file name>
dim parmcll as new collection
Open fname For Input As #1
Line Input #1, a;'now "a" is a variable containing the first line
parmarr = a.split("=")
parmcll.add item:=parmarr(1), key:=parmarr(0)
Line Input #1, a;'now the 2nd line

Would the second line then be?
second line
parmarr = b.split("=")
parmcll.add item:=parmarr(1), key:=parmarr(0)
Line Input #1, b;

If this code is in an ini file, would this slow down the process of input everytime the macro is used?




Tony makes it sound like really easy - can you give me a sample for just one category.




What I'm testing myself is the below code just put prior to every chunk of code for each category:

CODE -->
Dim BackgroundColor, FontColor, Category
BackgroundColor = wdColorAutomatic
'Copy and paste one of the below colors behind the “=” sign at the BackgroundColor: wdColorBlack / wdColorDarkRed / wdColorRed / wdColorPink / wdColorRose / wdColorBrown / wdColorOrange / wdColorLightOrange / wdColorGold / wdColorTan / wdColorOliveGreen / wdColorDarkYellow / wdColorLime / wdColorYellow / wdColorLightYellow / wdColorDarkGreen / wdColorGreen / wdColorSeaGreen / wdColorBrightGreen / wdColorLightGreen / wdColorDarkTeal / wdColorTeal / wdColorAqua / wdColorTurquoise / wdColorLightTurquoise / wdColorDarkBlue / wdColorBlue / wdColorLightBlue / wdColorSkyBlue / wdColorPaleBlue / wdColorIndigo / wdColorBlueGray / wdColorViolet / wdColorPlum / wdColorLavender / wdColorGray80 / wdColorGray50 / wdColorGray40 / wdColorGray25 / wdColorWhite

FontColor = wdColorAutomatic
'Copy and paste one of the below colors behind the “=” sign at the FontColor: wdColorBlack / wdColorDarkRed / wdColorRed / wdColorPink / wdColorRose / wdColorBrown / wdColorOrange / wdColorLightOrange / wdColorGold / wdColorTan / wdColorOliveGreen / wdColorDarkYellow / wdColorLime / wdColorYellow / wdColorLightYellow / wdColorDarkGreen / wdColorGreen / wdColorSeaGreen / wdColorBrightGreen / wdColorLightGreen / wdColorDarkTeal / wdColorTeal / wdColorAqua / wdColorTurquoise / wdColorLightTurquoise / wdColorDarkBlue / wdColorBlue / wdColorLightBlue / wdColorSkyBlue / wdColorPaleBlue / wdColorIndigo / wdColorBlueGray / wdColorViolet / wdColorPlum / wdColorLavender / wdColorGray80 / wdColorGray50 / wdColorGray40 / wdColorGray25 / wdColorWhite

Category = "Copy CATEGORY 1 to Study Overview" 'replace the WORD IN CAPITALS with a category of your own choice

Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = FontColor
Selection.Shading.BackgroundPatternColor = BackgroundColor
Selection.Font.Bold = True

Dim Msg, Style, Title, Response, MyString
Msg = "Wilt u de geselecteerde text markeren EN kopiëren naar de sectie 'Studie overzicht'? Klik 'No' als u alleen de geselecteerde text wilt markeren."
Style = vbYesNo + vbDefaultButton1
Title = Category


So if I want to make this flexible, people will have to open the VBA editor themselves and make the changes.

This I wanted to achieve in writing thie above code in an ini file, so people don't have to go into the code themselves. Which is not possible anyway if you startup the dot file in your WORD/STARTUP folder. You would then have to replace the normal.dot file with my (renamed) template - too complicated to make it flexible.


Regards,
Alex
 
Suppose you have a file: "C:\Colours.ini", with contents:

[Colours]
Background=Green
Font=Red

Then you could code something like this:

Code:
[blue]Select Case System.PrivateProfileString("C:\Colours.ini", "Colours", "Background")
    Case "Green": BackgroundColor=wdColorGreen
    case "Red": Backgroundcolor=wdcolorRed
End Select

Select Case System.PrivateProfileString("C:\Colours.ini", "Colours", "Font")
    Case "Green": FontColor=wdColorGreen
    case "Red": FontColor=wdcolorRed
End Select[/blue]

You can have whatever values you like in the ini file, provided you cater for them in your code.

Is that enough to get you started?


Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Hello Tony,
thanks for the info and code - starting to look like I'm getting there.

I have a question though:

Below I have my ini file:

Macro.ini file
[Category01]
BackgroundColor=wdColorRed
FontColor=wdColorLightBlue
Category=PRINCIPLE
Shortcut=Alt P

[Category02]
BackgroundColor=wdColorTan
FontColor=wdColorDarkBlue
Category=APPLICATION
Shortcut=Ctrl Shift A



Then I have in my macro code:

Dim BackgroundColor, FontColor, Category (Code Line #1)
BackgroundColor = ???Select BackgroundColor from [Category01] BackgroundColor in Macro.ini file = in this case wdColorRed



Select Case System.PrivateProfileString("C:\Users\Alex\Documents\1-ACF Studytools\Macro.ini", "Category01", "BackgroundColor")
' Case "Green": BackgroundColor=wdColorGreen
' case "Red": Backgroundcolor=wdcolorRed

Is it possible to insert the value for BackgroundColor from Category01 straight into the BackgroundColor under Code Line #1 - Dim BackgroundColor, FontColor, Category
BackgroundColor = Value from *.ini file [Category01]

End Select


There's one 'variable' that is left to update, which is the toolbar: it will now say Mark as Principle or as Application or Category01.
Would there be anyway that the toolbar can be set to read the ini file too and update the category and shortcuts to definitions in the ini file?

Sounds like a hughe challenge to me, but I'm impressed how far you guys have helped me already.

Another question:
how do you mark text as code in this post?

Many thanks,
Alex


 
Your ini file is just a lot of text - if you want to store the data in this way (so that it is easily editable by people) then you must code to translate that text into appropriate values yourself; the Word constants are translated at compile time and cannot (easily) be used at run time, so the answer to your question, can you assign the value directly, is, basically: no.

You can create, or amend, your toolbars as and when you will, in response to any data you care to check, so, yes, you could tailor them according to data in an ini file. Look at the various properties of the various types of CommandBar Controls.

----------------

To format code in your posts use [&#91;]code] and [&#91;]/code] tags. Click on the "Process TGML" link just below the posting box for more information on this and other tags.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Thanks Tony,
I'm going to give the ini file a good try and will add the code to translate the values accordingly at each category.
I'll give you some feedback in due time.

The toolbar is a seperate story I can see. I will have to look into that after the weekend.

But thanks once more for your very helpful advice.

Alex
 



Alex,

Let me point out that a very important part of extending your thanks to contributors that have given you valuable help is to

[blue]
Thank Tek-Tip Contributor
for this valuable post!
[/blue].

These Little Purple Stars accomplish several important things.

First, they give positive feedback to contributors, that their posts have been helpful.

Second, they identify threads as containing helpful posts, so that other members can benefit.

And third, they identify the original poster (that's YOU, BTW), as a grateful member, that not only receives, but is willing to give tokens of thanks.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Hello Bong (Bob Rashkin),
re-reading your post, you seem to offer a way to directly import from an macro.ini file a value - do I understand that correctly?

Could you give me an example of the first two lines in an ini file?

Few things I didn't understand though:
1. parmarr = a.split("=") - does this refer to two values written in the ini file? And how would the first line then look like?
2. how and where I should write vanname=varvalue in the ini file - could you also give me an example of that?
3. Does the key in the following line: parmcll.add item:=parmarr(1), key:=parmarr(0) - refer to shortcut key that I could assign?

Regards,
Alex
 
As Tony pointed out, for a file whose structure is conformable with what is a "formal" .ini file, there are tools to get the data easily.

What I propose, as I tend to do everything by brute force, is a simple text file, each line of which is of the form: "name" = "value". So consider your file:
[tt][Category01]
BackgroundColor=wdColorRed
FontColor=wdColorLightBlue
Category=PRINCIPLE
Shortcut=Alt P

[Category02]
BackgroundColor=wdColorTan
FontColor=wdColorDarkBlue
Category=APPLICATION
Shortcut=Ctrl Shift A[/tt]

The "[category..]" lines and the blank lines won't split so they'll need to be caught. All in all you'd be better off with Tony's approach.

If on the other hand your file were to look like:
[tt]
BackgroundColor1=wdColorRed
FontColor1=wdColorLightBlue
Category1=PRINCIPLE
Shortcut1=Alt P
BackgroundColor2=wdColorTan
FontColor2=wdColorDarkBlue
Category2=APPLICATION
Shortcut2=Ctrl Shift A[/tt]
Then my method would build a collection, parmcll,
whose items were the values: wdColorRed, wdColorLightBlue, ...
and whose keys were the parameter names: BackgroundColor1, FontColor1, ...

In my snippet, when you read a line from the file, that line (string) is made the value of the variable, "a" (not very imaginative or descriptive). Then, a is a string with a "=" somewhere in it.
Code:
parmarr = a.split("=")
An intermediate array, parmarr, is made by splitting the string, a, at the "=". So in the first instance, parmarr(0) would be BackgroundColor1 and parmarr(1) would be wdColorRed. Those values, as key/item, are added to the collection.
Then, they are retrieved from the collection with the item method:
Code:
parmcll.item("BackgroundColor1")
returns "wdColorRed".

Note that the file reads, Line Input #1, a, should be in a loop: Do While Not EOF(1)...Loop.


_________________
Bob Rashkin
 
Hello Bob,
many thanks for adding your wisdom to this issue.

After the weekend I will give it a try, though the last line about the loop is for me clouded in mystery.

Let me give it a try and I'll thank you after for this valuable post.

Alex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top