Cela a fonctionné pour moi sur Windows 7 et Word 2007:
public static void pasteHTML(this Range range, string html) { Clipboard.SetData(
"HTML Format", string.Format("Version:0.9\nStartHTML:80\nEndHTML:{0,8}\nStart" +
"Fragment:80\nEndFragment:{0,8}\n", 80 + html.Length) + html + "<"); range.Paste(); }
utilisation Exemple: range.pasteHTML("a<b>b</b>c");
Probablement Un peu plus fiable sans utiliser le presse-papiers est de sauvegarder le fragment HTML dans un fichier et d'utiliser InsertFile
. Quelque chose comme:
public static void insertHTML(this Range range, string html) {
string path = System.IO.Path.GetTempFileName();
System.IO.File.WriteAllText(path, "<html>" + html); // must start with certain tag to be detected as html: <html> or <body> or <table> ...
range.InsertFile(path, ConfirmConversions: false);
System.IO.File.Delete(path); }
Excellent! Vous pouvez accepter votre propre réponse avec la coche pour supprimer ce message de la liste "sans réponse". –
Le lien est rompu. – Chris