Jump to content
Macro Express Forums

Changing font color in scripting


Recommended Posts

Hi,

 

I've created a macro to design Templates through MEP in Microsoft word. It captures certain info. from another application and stores in some variables.

 

The macro is working fine but now i've recieved a request to color the font in a different color while designing the template.There are 3 colors to be used in the template.

 

I found this can be done through the Text Display option box but it can only be used to display the colored font in a message box whereas i need to change it in the template.

 

Is there any way through which the macro can change the text color in the template? I've attached the snapshot to make it more clear.

 

fmurk4.jpg

Thanks in advance.
Gagan
Link to comment
Share on other sites

I'm trying to understand the mechanism you are using. I think what you are saying is that MEP goes to some app and gathers some data then displays that data in Word. Is that correct? If so then is MEP pasting the text into an open Word window?

Link to comment
Share on other sites

Yes,I think you have got fair idea. The macro opens the word docx saved on the desktop and starts putting the information gathered from the other apps.There are mainly 3 font colors(i.e. red, blue and black) used in my document.

 

The default font color used is Black, now i'm not sure how to change the font color at various positions in the document.

Link to comment
Share on other sites

I was wondering if you could paste RTF tagging from the clipboard but it doesn't appear to work. I was hoping for once the over-cleverness of Word would kick in but alas, no. MEP interacts only as text with the clipboard so all data is identified as text for the destination application. You could paste it in then come up with the complicated arithmetic to index the targets and uses CTRL+Arrow and CTRL+Shift+Arrow to highlight, then figure out how to format the text with keyboard or mouse movements but what a pain that would be. To do it in Word I think you would be better off making it a VBA macro. See the sample below from MSDN of how one can simply create a doc, insert text, and make the 6th word bold.

Set newDoc = Documents.Add
Set myRange = newDoc.Content
myRange.InsertAfter "This is a test of bold."
myRange.Words(6).Bold = True

Another possibility would be to create an RTF file from scratch. IE as a text file. EG "This is \b bold\b0 text" will make the word "bold" bold. I've not done this with RTF but I have done it with HTML which might be something else to consider.

 

As an aside a cool trick I sometimes use in cases like this is to use Word Mail Merge. I create a pretty mail merge document and make all the variable fields merge fields. Even if they are entire paragraphs. Then I link it to a delimited text file. When the macro runs it replaces the text file and launches Word. When Word appears it automatically displays the variable content in the merge fields. BTW tab is a better delimiter than commas in this case. However this doesn't help you issue with conditional formatting.

 

Link to comment
Share on other sites

You can always use the commands in Word to change colors between text type commands. This macro works for Word 2007.

// Intialization
Variable Set String %SampleText1% to "This sample text is in the default color
"
Variable Set String %SampleText2% to "This sample text is dark red
"
Variable Set String %SampleText3% to "This sample text is black
"
Variable Set String %SampleText4% to "This sample text is blue
"
 
If Not Window "- Microsoft Word" is focused
  Window Activate: - Microsoft Word
End If
 
 
Text Type (Simulate Keystrokes): %SampleText1%
 
// Change to red
Text Type (Simulate Keystrokes): <ALTD>h<ALTU> // 'Home' ribbon
Text Type (Simulate Keystrokes): fc // Bring up font color
Text Type (Simulate Keystrokes): <SHIFTD><TAB><TAB><TAB><SHIFTU> // Move to standard colors
Text Type (Simulate Keystrokes): <ARROW RIGHT> // Choose dark red
Text Type (Simulate Keystrokes): <ENTER>
Text Type (Simulate Keystrokes): %SampleText2%
 
// Change to black
Text Type (Simulate Keystrokes): <ALTD>h<ALTU> // 'Home' ribbon
Text Type (Simulate Keystrokes): fc // Bring up font color
Text Type (Simulate Keystrokes): <TAB><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW RIGHT> // Choose black
Text Type (Simulate Keystrokes): <ENTER>
Text Type (Simulate Keystrokes): %SampleText3%
 
// Change to blue
Text Type (Simulate Keystrokes): <ALTD>h<ALTU> // 'Home' ribbon
Text Type (Simulate Keystrokes): fc // Bring up font color
Text Type (Simulate Keystrokes): <SHIFTD><TAB><TAB><TAB><SHIFTU> // Move to standard colors
Text Type (Simulate Keystrokes): <ARROW LEFT><ARROW LEFT> // Choose blue
Text Type (Simulate Keystrokes): <ENTER>
Text Type (Simulate Keystrokes): %SampleText4%
 
// Back to black
Text Type (Simulate Keystrokes): <ALTD>h<ALTU> // 'Home' ribbon
Text Type (Simulate Keystrokes): fc // Bring up font color
Text Type (Simulate Keystrokes): <TAB><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW RIGHT> // Choose black
Text Type (Simulate Keystrokes): <ENTER>
Text Type (Simulate Keystrokes): 
This concludes this sample macro

To try it copy and paste this into your macro:

<COMMENT Value="Intialization"/>
<VARIABLE SET STRING Option="\x00" Destination="%SampleText1%" Value="This sample text is in the default color\r\n" NoEmbeddedVars="FALSE"/>
<VARIABLE SET STRING Option="\x00" Destination="%SampleText2%" Value="This sample text is dark red\r\n" NoEmbeddedVars="FALSE"/>
<VARIABLE SET STRING Option="\x00" Destination="%SampleText3%" Value="This sample text is black\r\n" NoEmbeddedVars="FALSE"/>
<VARIABLE SET STRING Option="\x00" Destination="%SampleText4%" Value="This sample text is blue\r\n" NoEmbeddedVars="FALSE"/>
<COMMENT/>
<IF NOT WINDOW Option="\x00" Title="- Microsoft Word" Partial="TRUE" Wildcards="FALSE"/>
<WINDOW ACTIVATE Title="- Microsoft Word" Exact_Match="FALSE" Wildcards="FALSE"/>
<END IF/>
<COMMENT/>
<COMMENT/>
<TEXT TYPE Action="0" Text="%SampleText1%"/>
<COMMENT/>
<COMMENT Value="Change to red"/>
<TEXT TYPE Action="0" Text="<ALTD>h<ALTU>" _COMMENT="'Home' ribbon"/>
<TEXT TYPE Action="0" Text="fc" _COMMENT="Bring up font color"/>
<TEXT TYPE Action="0" Text="<SHIFTD><TAB><TAB><TAB><SHIFTU>" _COMMENT="Move to standard colors"/>
<TEXT TYPE Action="0" Text="<ARROW RIGHT>" _COMMENT="Choose dark red"/>
<TEXT TYPE Action="0" Text="<ENTER>"/>
<TEXT TYPE Action="0" Text="%SampleText2%"/>
<COMMENT/>
<COMMENT Value="Change to black"/>
<TEXT TYPE Action="0" Text="<ALTD>h<ALTU>" _COMMENT="'Home' ribbon"/>
<TEXT TYPE Action="0" Text="fc" _COMMENT="Bring up font color"/>
<TEXT TYPE Action="0" Text="<TAB><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW RIGHT>" _COMMENT="Choose black"/>
<TEXT TYPE Action="0" Text="<ENTER>"/>
<TEXT TYPE Action="0" Text="%SampleText3%"/>
<COMMENT/>
<COMMENT Value="Change to blue"/>
<TEXT TYPE Action="0" Text="<ALTD>h<ALTU>" _COMMENT="'Home' ribbon"/>
<TEXT TYPE Action="0" Text="fc" _COMMENT="Bring up font color"/>
<TEXT TYPE Action="0" Text="<SHIFTD><TAB><TAB><TAB><SHIFTU>" _COMMENT="Move to standard colors"/>
<TEXT TYPE Action="0" Text="<ARROW LEFT><ARROW LEFT>" _COMMENT="Choose blue"/>
<TEXT TYPE Action="0" Text="<ENTER>"/>
<TEXT TYPE Action="0" Text="%SampleText4%"/>
<COMMENT/>
<COMMENT Value="Back to black"/>
<TEXT TYPE Action="0" Text="<ALTD>h<ALTU>" _COMMENT="'Home' ribbon"/>
<TEXT TYPE Action="0" Text="fc" _COMMENT="Bring up font color"/>
<TEXT TYPE Action="0" Text="<TAB><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW DOWN><ARROW RIGHT>" _COMMENT="Choose black"/>
<TEXT TYPE Action="0" Text="<ENTER>"/>
<TEXT TYPE Action="0" Text="\r\nThis concludes this sample macro\r\n"/>
<COMMENT/>

Link to comment
Share on other sites

You might want to use a hybrid approach. Create three commands in VBA: one to change the selection to black, another to to red, and the third to blue:

 

Sub Black()
Selection.Font.Color = wdColorBlack
End Sub

 

Sub Red()
Selection.Font.Color = wdColorRed
End Sub

 

Sub Blue()
Selection.Font.Color = wdColorBlue
End Sub

 

Then assign these VBA macros to Word hotkeys: e.g.,

 

F2 = Black

F3 = Red

F4 = Blue

 

If text fragments are stored in Variables, say as an array %Text[n]%...

 

%Text[1]% = This is black.

%Text[2]% = This is red.

%Text[3]% = This is blue.

etc.

 

Then you could do something like this in MEP:

 

Text Type (Simulate Keystrokes): <F2>%Text[1]% ' F2 = black...

Text Type (Simulate Keystrokes): <F3>%Text[2]% ' F3 = red...

Text Type (Simulate Keystrokes): <F4>%Text[3]% ' F4 = blue...

Text Type (Simulate Keystrokes): <F2>%Text[4]% ' Change colour back to black.

 

Kludgy, but it just might work. (Contrary to what many people believe, It is not necessary to select text to change font colours in Word. Text inserted immediately after the execution of the macro will appear in the desired colour.)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...