Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send Unicode text from MATLAB into a Word document via the ActiveX interface?

I'm using MATLAB to programmatically create a Microsoft Word document on Windows. In general this solution works fine, but it is having trouble with non-ASCII text. For example, take this code:

wordApplication = actxserver('Word.Application');
wordApplication.Visible = 1;
wordApplication.Documents.Add;
selection = wordApplication.Selection;
umbrella = char(9730);
disp(umbrella)
selection.TypeText(umbrella)

The Command Window displays the umbrella character correctly, but the character in the Word document is the "question mark in a box" missing character symbol. I can cut-and-paste the character from the Command Window into Word, so that character is indeed available in that font.

The TypeText method must be assuming ASCII. There are resources on how to set Unicode flags for similar operations from other languages, but I don't know how to translate them into the syntax I have available in MATLAB.

Clarification: My use case is sending an unknown Unicode string (char array), not just a single character. It would be ideal to be able to send it all at once. Here is better sample code:

% Define a string to send with a non-ASCII character.
umbrella = char(9730);
toSend = ['Have you seen my ' umbrella '?'];
disp(toSend)

% Open a new Word document.
wordApplication = actxserver('Word.Application');
wordApplication.Visible = 1;
wordApplication.Documents.Add;

% Send the text.
selection = wordApplication.Selection;
selection.TypeText(toSend)

I was hoping I could simply set the encoding of the document itself, but this doesn't seem to help:

wordApplication = actxserver('Word.Application');
wordApplication.Visible = 1;
wordApplication.Documents.Add;
disp(wordApplication.ActiveDocument.TextEncoding)
wordApplication.ActiveDocument.TextEncoding = 65001;
disp(wordApplication.ActiveDocument.TextEncoding)
selection = wordApplication.Selection;
toSend = sprintf('Have you seen my \23002?');
selection.TypeText(toSend)
like image 849
Matthew Simoneau Avatar asked May 08 '15 21:05

Matthew Simoneau


1 Answers

Method 1. Valid for a single character (original question)

Taken from here:

umbrella = 9730; %// Unicode number of the desired character
selection.InsertSymbol(umbrella, '', true); %// true means use Unicode

The second argument specifies the font (so you could use 'Arial' etc), and '' apparently means use current font. The third argument 'true' means use Unicode.

Method 2. Valid for a single character (original question)

A less direct way, taken from here:

umbrella = 9730; %// Unicode number of the desired character
selection.TypeText(dec2hex(umbrella));
selection.ToggleCharacterCode;

Method 3. Valid for a string (edited question)

You can work with a string at once if you don't mind using the clipboard:

umbrella = char(9730);
toSend = ['Have you seen my ' umbrella '?'];
clipboard('copy', toSend); %// copy the Unicode string contained in variable `toSend`
selection.Paste %// paste it onto the Word document
like image 139
Luis Mendo Avatar answered Sep 30 '22 20:09

Luis Mendo