Word 2007 VBA to insert text
I want to insert 开发者_JAVA百科text with custom formatting, then change the font style back 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 anyone explain a way to do what I'm looking for?
Edit: I should have been more specific. If I am typing text, I have a certain formatting that I am typing in that is shown on the Home Tab (eg. Comic Sans Ms, Size 22, Bold). When I insert text with the code, this changes the formatting that I am typing with, so if I continue typing it will be in the NEW font type, not the Comic Sans MS. I am trying to make it so if I continue typing after I have inserted the text via VBA code, it will retain my old formatting.
One simple solution is to store all properties that you are going to change, and to 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
The trick I find helpful when writing Word macros is simply to replicate what I'd be doing if I was using the Word GUI. When I want to paste formatted text but keep my current format, I type a space, paste in the text before the space then delete the space. As the space has my original format that's how I get it back.
So, doing this as 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 preserve any properties of the text you originally had.
I can't quite figure out exactly what you're trying to do there, but Selection.TypeText
will collapse the selection down to the insertion point, so you effectively have no characters selected by the time you try to "revert the font". You either need to re-select the text, or use a Range
object instead of the Selection to identify the text to be affected.
The reason that you get an error at the line:
Set Selection.Font = oldFont
...is because - unusually, and perversely - you should not use the Set
keyword when assigning to the Font
property. Rather than storing a reference to a Font
object, the assignment simply applies the properties of the assigned font.
This is very confusing API design, made all the more confusing because you do need to use the Set
keyword when reading the Font
property, and because that does assign a reference to a Font
object!
And that's the other reason why your code won't work - you're taking a reference to a Font
object which you then modify, and your reference points to the same Font
object that has now changed.
What you actually need to do is create a new Font
object to store the original font details, as follows:
Set oldFont = Selection.Font.Duplicate
The Selection.Font object is read only.
This means that there is no way to restore all the settings in one assignment. Since you are only changing a few properties the easiest solution is to save each individual value and restore them afterwards as stephan suggests.
I.e. Save properties:
oldFontName = Selection.Font.Name
oldFontSize = Selection.Font.Size
oldFontBold = Selection.Font.Bold
Do you stuff and then restore properties:
Selection.Font.Name = oldFontName
Selection.Font.Size = oldFontSize
Selection.Font.Bold = oldFontBold
See, if this piece of code gives you enough hint.
CopyFormat
picks up the existing formatting by moving left from current cursor.
PasteFormat
applies it to a character & from there on, the original formatting (which was copied) comes into 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"
Sub No_Format()
'
' No_Format Macro
'
'
Selection.PasteSpecial Link:=False, DataType:=wdPasteText
End Sub
this will allow you to paste the text and adopting the new formatting.
精彩评论