none
WPF & Ms Word Book marks RRS feed

  • Question

  • Hello 
    i am Still new to WPF
    I have created  a project and its working fine. Now I wanted to extend the project where I should be able to get input from screen and fill a MS Word bookmarks
    Eg.
    User Enter  First Name, Last Name and Address using WPF Window
    Now I wanted to open a word document template where the file got some bookmarks as FirstName, LastName,PhoneNumber,Address

    So I wanted to replace the bookmarks with the user inputs

    Is there anybody know a sample project with do something like above or any assistance would be appreciated

    Thursday, March 15, 2012 12:25 PM

Answers

  • Hi Rushdy,

    Thanks for posting in the MSDN Forum.

    I create two bookmarks "FirstName" and "LastName" in my Word document. And use following code to fill it. Please remember add Word PIA into your WPF project.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Word = Microsoft.Office.Interop.Word;
    using Microsoft.Win32;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private object path = null;
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                if (path == null)
                {
                    OpenFileDialog dialog = new OpenFileDialog();
                    dialog.Filter = "Document|*.docx";
                    dialog.ShowDialog();
                    path = dialog.FileName;
                }
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                Word.Application app = null;
                Process[] ps = Process.GetProcessesByName("WINWORD");
                if (ps.Length > 1)
                {
                    app = (Word.Application)Marshal.GetActiveObject("Word.Application");
                }
                else
                {
                    app = new Word.Application();
                }
                object missing = Type.Missing;
                Word.Document doc = app.Documents.Open(ref path, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing);
                doc.Bookmarks["FirstName"].Range.Text = textBox1.Text;
                doc.Bookmarks["LastName"].Range.Text = textBox2.Text;
                textBox1.Text = "";
                textBox2.Text = "";
                app.Visible = true;
                Marshal.ReleaseComObject(app);
            }
        }
    }

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Rushdy Najath Monday, March 19, 2012 9:05 AM
    Monday, March 19, 2012 5:58 AM
    Moderator