Word 2007 VBA to insert text

I want to insert custom formatted text, and then change the font style to what it was before the code was run.

Dim myText As String Dim oldFont As Object 'Save old font Set oldFont = Selection.Font 'Insert text with custom font myText = "CUSTOM STRING" Selection.Font.Name = "Comic Sans MS" Selection.Font.Size = 26 Selection.Font.Bold = True Selection.TypeText (myText) 'Revert font back to original Set Selection.Font = oldFont 

Can someone explain a way to do what I'm looking for?

Edit: I should have been more specific. If I type, I have a specific formatting that I print on what is shown on the Home tab (for example, Comic Sans Ms, Size 22, Bold). When I paste the text with the code, it changes the formatting that I print, so if I continue to print, it will be in type NEW, and not in Comic Sans MS. I try to do this, if I keep typing after pasting the text through VBA code, it will keep my old formatting.

+4
source share
6 answers

One simple solution is to save all the properties you are about to change and reset them at the end:

 Dim myText As String Dim oldFont As String Dim oldSize As Integer Dim oldBold As Boolean 'Save old font oldFont = Selection.Font.Name oldSize = Selection.Font.Size oldBold = Selection.Font.Bold 'Insert text with custom font myText = "CUSTOM STRING" Selection.Font.Name = "Comic Sans MS" Selection.Font.Size = 26 Selection.Font.Bold = True Selection.TypeText (myText) 'Revert font back to original Selection.Font.Name = oldFont Selection.Font.Bold = oldBold Selection.Font.Size = oldSize 
+4
source

The trick that comes in handy when writing Word macros is to simply repeat what I would do if I used the Word GUI. When I want to insert formatted text, but keep the current format, I type a space, insert text before the space, and then remove the space. Since space has my original format, how will I get it back.

So, doing it like a macro:

 'Type a space Selection.TypeText Text:=" " 'Move Cursor back one character Selection.MoveLeft Unit:=wdCharacter, Count:=1 'Insert text with custom font myText = "CUSTOM STRING" Selection.Font.Name = "Comic Sans MS" Selection.Font.Size = 26 Selection.Font.Bold = True Selection.TypeText (myText) 'Move Cursor forward one character Selection.MoveRight Unit:=wdCharacter, Count:=1 'Delete the space Selection.TypeBackspace 

This will save any properties of the text that you originally had.

+2
source

I can’t understand exactly what you are trying to do there, but Selection.TypeText will hide the selection to the insertion point, so you actually don’t have the characters selected by the time you try to “return the font”. You need to either reselect the text or use the Range object instead of the selection to identify the text that should be affected.

The reason you get the error in the line:

 Set Selection.Font = oldFont 

... because - unusual and perverted - you should not use the Set keyword when assigning to the Font property. Instead of storing a reference to the Font object, the assignment simply applies the properties of the assigned font.

This is a very confusing API design, which is even more confusing because you do have to use the Set keyword when reading the Font property, but because it does assign a reference to the Font object!

And that is another reason why your code will not work, you are referring to the Font object, which you then modify, and your link points to the same Font object, which has now changed.

What you really need to do is create a new Font object to store the original font data, as shown below:

 Set oldFont = Selection.Font.Duplicate 
0
source

The Selection.Font object is read-only.

This means that there is no way to restore all settings in one destination. Since you are only changing a few properties, the easiest solution is to save each individual value and then restore it after Stefan suggests.

those. Save Properties:

 oldFontName = Selection.Font.Name oldFontSize = Selection.Font.Size oldFontBold = Selection.Font.Bold 

Do you comply and then restore the properties:

 Selection.Font.Name = oldFontName Selection.Font.Size = oldFontSize Selection.Font.Bold = oldFontBold 
0
source

See if this piece of code gives you enough hint.

CopyFormat selects the existing formatting by moving to the left of the current cursor.
PasteFormat applies it to the character and from there the initial formatting (which was copied) takes effect.

 Selection.MoveLeft unit:=wdWord, Count:=1 Selection.EndKey Extend:=wdExtend Selection.CopyFormat Selection.MoveRight unit:=wdWord '* New text and new formatting Selection.Font.Bold = True Selection.Font.Italic = True Selection.Font.Size = 28 Selection.TypeText "hello world" Selection.TypeText " " Selection.MoveLeft unit:=wdCharacter, Count:=1 Selection.EndKey Extend:=wdExtend Selection.PasteFormat Selection.TypeText "original formatting here" 
0
source
 Sub No_Format() ' ' No_Format Macro ' ' Selection.PasteSpecial Link:=False, DataType:=wdPasteText End Sub 

this will allow you to insert text and accept the new formatting.

0
source

All Articles