简体   繁体   中英

VBA Word Selection.TypeText changing font?

I have a font which has lots of special unicode characters in, I would like to type them into a word document using a macro.

A very simplified version looks like this:

Sub Macro2()
    Selection.TypeText (ChrW(58081))
End Sub

I can see using BabelMap (a font viewer), that the character at decimal 58081 is the one I want to enter.

The Issue

When I run this macro, the font changes from the font with the special characters to "Calibri" which is the default. This causes the character to show up as a square box similar to 

If I then manually highlight the square box and change it to my desired font, it corrects itself and shows up.

What I have tried

I have tried setting the font again before typing the text, it doesn't make a difference:

With Selection
        .font.Name = "MyFont"
End With
Selection.TypeText (ChrW(58081))

I have also tried adding to the macro to go back and change the font after inserting it. This doesn't make a difference either, the code I use is this:

With Selection
        .font.Name = "MyFont"
End With

Selection.TypeText (ChrW(58081))

Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.font.Name = "MyFont"
Selection.MoveRight Unit:=wdCharacter, Count:=1

A useful observation

When I enter a character within a normal set, eg:

Selection.TypeText (ChrW(97))

This works without changing the font as I expect - why does the font change when I enter 58081?

This is only an idea: there is a difference between a paragraph having a font and a [sequence of] letter[s] having a font. If the paragraph has your myFont font, then the inserted text should be your font; otherwise it may revert to the paragraph's font (default for all text of the paragraph unless set to a different font).

If the selection is empty, ie you are refering to the insertion point, and you set the font, then the font for the next character is set. However, the TypeText method inserts it before the insertion point, so your set font has no effect.

As for your last attempt, to select the inserted character and set its font, I have no idea why that shouldn't work. Try following setting the font to print the size and font name of the selection (the selection should now be 1 characters in size).

You might also try using the InsertAfter or InsertBefore methods of the Selection object.

If you want to use Selection, this here worked just fine for me:

With Selection
    .Font.Name = "Arial Unicode MS"
    .Text = "Umlaut test ä ö ü ß"
End With

Many thanks for these ideas, they gave me another thing to look at. In the end, I found even if I copy/pasted a character from another location into the document that it was changing to font to Calibri. When creating a brand new document with nothing else into it and running the macro in my question, I had the same issue. However, I have an old document that was created around 2003 and I found when running even on the same version of office (2013 I am on), the macro works. I think something is going on when creating word documents, perhaps some default setting or perhaps some internal format differences, as my above macros work fine on the old document but not on any new documents I create.

My final solution is to use the old document, copy all my macros into it, and it works fine, even saved as a dotm file and when saved using my 2013 version of office. I am still totally at a loss as to why this is. I can still create new documents on which my macro does not work. Both files are .dotm, the only difference is the machine they were originally created on the version of office.

I hope this helps anyone else with the same problem, it's cost me about a day of pulling my hair out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM