Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to change Font in Word by C#

How to change Font in Word by C#

Scheduled Pinned Locked Moved C#
graphicscsharpcomhelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    naylynn
    wrote on last edited by
    #1

    Hello Every Body!
    I just try to text analysis program by C# .In the program i uses RichTextEdit . I want to load word file, text file , pdf to Richtext Edit Box. Now i can load these file to RichText Edit . But i can't change font these text. Just i used Microsoft.Office.Interop.Word; . Check my code below. Thanks for your kindness!

    public void ReadMsWord()
    {
    // variable to store file path
    string filePath=null;
    // open dialog box to select file
    OpenFileDialog file= new OpenFileDialog();
    // dilog box title name
    file.Title="Word File";
    // set initial directory of computer system
    file.InitialDirectory= "c:\\";
    // set restore directory
    file.RestoreDirectory= true;
    // execute if block when dialog result box click ok button
    if (file.ShowDialog() ==DialogResult.OK)
    {
    // store selected file path
    filePath=file.FileName.ToString();
    }
    try
    {
    // create word application
    Microsoft.Office.Interop.Word.Application word =new Microsoft.Office.Interop.Word.Application();
    // create object of missing value
    object miss= System.Reflection.Missing.Value;
    // create object of selected file path
    object path=filePath;
    // set file path mode
    object readOnly= false;
    //Microsoft.Office.Interop.Word.Font f = new Microsoft.Office.Interop.Word.Font();
    //f.Name = "Zawgyi-One, 8.25pt";
    //object fo = f;
    // open document
    Microsoft.Office.Interop.Word.Document docs=word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss,ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
    // select whole data from active window document
    docs.ActiveWindow.Selection.WholeStory();
    // handover the data to cllipboard
    docs.ActiveWindow.Selection.Copy();
    // clipboard create reference of idataobject interface which transfer the data
    IDataObject data= Clipboard.GetDataObject();
    //Microsoft.Office.Interop.Word.Range rng = docs.Paragraphs[1].Range;
    //rng.Font.Name = "Zawgyi-One";
    //set data into richtextbox control in text format
    richTextBox1.Text=data.GetData(DataFormats.Text).ToString();
    //word.Selection.Font.Name = "Zawgyi-One";
    // Microsoft.Office.Interop.Word.Document document = new Microsoft.Office.Interop.Word.Document();
    // this.richTextBox1.SelectAll();
    //System.Drawing.Font currentFont = richTextBox1.SelectionFont;
    // richTextBox1.SelectionFont = new System.Drawing.Font(currentFont.FontFamily,currentFont.Size,newFontStyle);
    // read bitmap image from clipboard with help of iddataobject interface
    Image img= (Image)data.GetData(DataFor

    L 1 Reply Last reply
    0
    • N naylynn

      Hello Every Body!
      I just try to text analysis program by C# .In the program i uses RichTextEdit . I want to load word file, text file , pdf to Richtext Edit Box. Now i can load these file to RichText Edit . But i can't change font these text. Just i used Microsoft.Office.Interop.Word; . Check my code below. Thanks for your kindness!

      public void ReadMsWord()
      {
      // variable to store file path
      string filePath=null;
      // open dialog box to select file
      OpenFileDialog file= new OpenFileDialog();
      // dilog box title name
      file.Title="Word File";
      // set initial directory of computer system
      file.InitialDirectory= "c:\\";
      // set restore directory
      file.RestoreDirectory= true;
      // execute if block when dialog result box click ok button
      if (file.ShowDialog() ==DialogResult.OK)
      {
      // store selected file path
      filePath=file.FileName.ToString();
      }
      try
      {
      // create word application
      Microsoft.Office.Interop.Word.Application word =new Microsoft.Office.Interop.Word.Application();
      // create object of missing value
      object miss= System.Reflection.Missing.Value;
      // create object of selected file path
      object path=filePath;
      // set file path mode
      object readOnly= false;
      //Microsoft.Office.Interop.Word.Font f = new Microsoft.Office.Interop.Word.Font();
      //f.Name = "Zawgyi-One, 8.25pt";
      //object fo = f;
      // open document
      Microsoft.Office.Interop.Word.Document docs=word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss,ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
      // select whole data from active window document
      docs.ActiveWindow.Selection.WholeStory();
      // handover the data to cllipboard
      docs.ActiveWindow.Selection.Copy();
      // clipboard create reference of idataobject interface which transfer the data
      IDataObject data= Clipboard.GetDataObject();
      //Microsoft.Office.Interop.Word.Range rng = docs.Paragraphs[1].Range;
      //rng.Font.Name = "Zawgyi-One";
      //set data into richtextbox control in text format
      richTextBox1.Text=data.GetData(DataFormats.Text).ToString();
      //word.Selection.Font.Name = "Zawgyi-One";
      // Microsoft.Office.Interop.Word.Document document = new Microsoft.Office.Interop.Word.Document();
      // this.richTextBox1.SelectAll();
      //System.Drawing.Font currentFont = richTextBox1.SelectionFont;
      // richTextBox1.SelectionFont = new System.Drawing.Font(currentFont.FontFamily,currentFont.Size,newFontStyle);
      // read bitmap image from clipboard with help of iddataobject interface
      Image img= (Image)data.GetData(DataFor

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      naylynn wrote:

      Now i can load these file to RichText Edit

      That is not entirely what the code does; it copies it to the clipboard, and pastes that in the RichTextBox. That means that it is a copy, in RTF format, and will no longer support everything that Word does. You can change the font, it's style and color, by programmatically setting a selection (like SelectAll would do) and setting its SelectionFont. If you want to do it using Word-interop, then you will have to make the changes before copying the data to the clipboard. After that, it becomes RTF.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

      N 1 Reply Last reply
      0
      • L Lost User

        naylynn wrote:

        Now i can load these file to RichText Edit

        That is not entirely what the code does; it copies it to the clipboard, and pastes that in the RichTextBox. That means that it is a copy, in RTF format, and will no longer support everything that Word does. You can change the font, it's style and color, by programmatically setting a selection (like SelectAll would do) and setting its SelectionFont. If you want to do it using Word-interop, then you will have to make the changes before copying the data to the clipboard. After that, it becomes RTF.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

        N Offline
        N Offline
        naylynn
        wrote on last edited by
        #3

        Thanks

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups