Christian Geiselmann
Christian Geiselmann

Reputation: 622

VBA in Word: CopyAsPicture method ignores text size as well as some font faces

The CopyAsPicture method in VBA in Word (I am using Word 2010 and 2016) is to transform a given piece of text (some string selected in the document) into a picture.

Microsoft provides this sample script in its tutorials:

Sub CopyPasteAsPicture()

'Origin: https://learn.microsoft.com/en-us/office/vba/api/word.selection.copyaspicture

 ActiveDocument.Content.Select
  With Selection
   .CopyAsPicture
   .Collapse Direction:=wdCollapseEnd
   .PasteSpecial DataType:=wdPasteMetafilePicture
 End With
End Sub

For testing the CopyAsPicture method I used however the following script which I found in a related question in this forum:

Sub CopySelPasteAsPicture()
' Take a picture of a selection and paste it at the document end

' Source: https://stackoverflow.com/questions/8888145/convert-text-to-image-in-microsoft-word

With Selection
    .CopyAsPicture
End With

ActiveDocument.Content.Select
With Selection
    .Collapse Direction:=wdCollapseEnd
    .TypeParagraph
    .TypeParagraph
    .PasteSpecial DataType:=wdPasteMetafilePicture
End With

End Sub 

I tested this but I find two problems with it:

1) Text size is ignored. Text sized, say, 28 pt will be rendered in the resulting picture as 12 pt or something like that

2) Not all font faces get recognized. I have some special fonts (both in otf and ttf) that I want to picturize that way. These fonts are ignored, and instead I get a picture of a standard font (Times New Roman or whatever).

I do not find help for these issues in Microsoft's VBA documentation.

Additional information:

1) Why do I want to do this? I use some rare fonts that people do not have usually, and including them into the .docx also does not work reliably. I have to share these documents with colleagues, and they should be able to see also the characters from the special fonts.

2) Picture generation works with cyrillic characters in standard fonts. That's good so far.

3) Picture generation also works with fonts that have cliparts instead of pictures (such special fonts are used e.g. for childrens books or textbooks for people to train reading and writing)

4) The font that in my case does not get picturized (but replaced by pictures of some standard system font) is a font I got as .otf, but I transformed it to .ttf (because I thought I could then include into the documents, but this did not work either). - I did so far not test if it would work with the original otf, but I doubt it because there was so far no problem with ttf versus otf fonts.

Question:

I would like to understand why font size and some font faces are ignored.

Of course, ideas how to solve this are also welcome.

Upvotes: 0

Views: 465

Answers (1)

Timothy Rylatt
Timothy Rylatt

Reputation: 7860

.PasteSpecial DataType:=wdPasteMetafilePicture

A metafile picture is meant to retain font data. So when the document is opened on a device that doesn't have that font Word will follow its font substitution rules. I also vaguely recall that there is a font related bug in the metafile format.

I suggest that you play with the other paste data types and see what works best for you.

Upvotes: 1

Related Questions