简体   繁体   中英

How to replace bookmark image along with formating in word using C#?

I am automating a document using interop.word.
I have a template which contents are populated through WinForms controls. Rest all is peace of cake but when it comes to an image, the code is not replacing the image rather it inserts the image and doesn't keep any format so all my template effort goes to zero.

I want to insert image at bookmark where it should replace the existing bookmark image and keep its formatting. Can anyone give me the best way of doing it?

Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace GISv1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string address = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);


            //Creating Object for word application
            Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();

            //Creating Object for documnt
            Microsoft.Office.Interop.Word.Document doc = new Document();

            //Opening the document
            doc = app.Documents.Open(address + "//ReportTemplate11.dotx");

            //Activating for Editing
            doc.Activate();

            //Bookmarks Finding and Replacing
            Bookmarks books = doc.Bookmarks;

            books["Ref"].Range.Text = tb_Reference.Text;
            books["Id"].Range.Text = tb_RptId.Text;
            books["RptNo"].Range.Text = tb_RptNo.Text;
            books["ClientName"].Range.Text = tb_Client.Text;
            books["ProjectName1"].Range.Text = tb_Project.Text;
            books["ProjectName2"].Range.Text = tb_Project.Text;
            books["ProjectName3"].Range.Text = tb_Project.Text;
            books["ProjectArea"].Range.Text = cb_Area.Text;
            books["FieldTestName"].Range.Text = tb_FieldName.Text;
            books["FieldTestDesignation"].Range.Text = tb_FieldDesignation.Text;
            books["FieldTestDate"].Range.Text = tb_FieldDate.Text;
            books["LabTestName"].Range.Text = tb_LabName.Text;
            books["LabTestDesignation"].Range.Text = tb_LabDesignation.Text;
            books["LabTestDate"].Range.Text = tb_LabDate.Text;
            books["PreparedName"].Range.Text = tb_PreparedName.Text;
            books["PreparedDesignation"].Range.Text = tb_PreparedDesignation.Text;
            books["PreparedDate"].Range.Text = tb_PreparedDate.Text;
            books["VettedName"].Range.Text = tb_VettedName.Text;
            books["VettedDesignation"].Range.Text = tb_VettedDesignation.Text;
            books["VettedDate"].Range.Text = tb_VettedDate.Text;
            books["ApprovedName"].Range.Text = tb_ApprovedName.Text;
            books["ApprovedDesignation"].Range.Text = tb_ApprovedDesignation.Text;
            books["ApprovedDate"].Range.Text = tb_ApprovedDate.Text;


            books["TitlePagePic"].Range.InlineShapes.AddPicture(address + "//PAF SIte Water Tanks.jpg");


            MessageBox.Show("Data is saved");
            app.Visible = true;




        }
    }
}

If I understand your question correctly, Word's object model doesn't support what you want. There's no way to stick another graphic into a bookmark and have it replace the original InlineShape and take on the properties of that original. More recent versions of Word have functionality in the UI, for the user, that do that. But it hasn't been provided in the object model.

You'd need to store every relevant property and re-apply it after inserting the graphic.

A possible work-around for the sizing is to store the graphic in a one-cell table with the exact width needed for the image (the height of an inserted graphic will adjust to the width proportionally). Bookmark the entire cell, not just the graphic. The following code snippet will insert the new graphic then delete the old:

Word.Range rngImage = books["TitlePagePic"].Range;
rngImage.InlineShapes.AddPicture(address + "//PAF SIte Water Tanks.jpg");
rngImage.InlineShapes[2].Delete();

But if you have any other kind of formatting that will need to be re-applied.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM