#

Download:

Datei 1: vsto_word_addin_files.zip

VSTO Addin Code für Word-Addin

 

VSTO COM Word Addin als Ribbonbar

Zum Einfügen von Fotos in eine Tabelle

 

VSTO Addin Code in C#

Für Word Addin

In Ribbon1.cs ->Code

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Tools.Ribbon;

 

//----< using >----

//using Microsoft.Office.Tools.Ribbon;        //*word ribbon

using Word = Microsoft.Office.Interop.Word; //*word document

using System.Windows.Forms;                 //*word OpenfileDialog

using Office = Microsoft.Office.Core;       //*lock ratio width height

using Microsoft.Office.Interop.Word;

//----</ using >----

 

namespace Addin_Word_Photos_into_Table

{

    public partial class Ribbon1

    {

        //< init >

        Document _doc = null;

        Word.Application _app = null;

        //</ init >

 

        #region Region: Ribbonbar

        //===================< RibbonBar >===================

        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)

        {

            tbxPhotos_Tablename.Text = Settings1.Default.Photos_Tablename;            

        }

        //===================</ RibbonBar >===================

        #endregion /Ribbonbar

 

        #region --Button--

        //===================< Buttons >===================

        private void BtnPhotos_Insert_Click(object sender, RibbonControlEventArgs e)

        {

            _doc = Globals.ThisAddIn.Application.ActiveDocument;

            _app = Globals.ThisAddIn.Application;

 

            Word.Table table_Photos = find_Table_with_Header(tbxPhotos_Tablename.Text);

            if (table_Photos != null)

            {

                insert_Photos_at_Table(table_Photos);

            }

        }

 

 

        //===================</ Buttons >===================

        #endregion /Buttons

 

        #region --Controls--

        //===================< Controls >===================

        private void tbxPhotos_Tablename_TextChanged(object sender, RibbonControlEventArgs e)

        {

            Settings1.Default.Photos_Tablename = tbxPhotos_Tablename.Text;

        }

 

 

 

        //===================</ Controls >===================

        #endregion /Controls

 

        #region --Methods--

        //===================< Methods >===================

 

        //===================</ Methods >===================

        #endregion /Methods

 

 

 

        #region --Methods--

        //===================< Methods >===================

        private Word.Table find_Table_with_Header(string sHeader)

        {

            //-----------------< find_Table_with_Header() >-----------------

 

            //--< find Table Header >--

            //*Loop all tables and look for header

            foreach (Word.Table table in _doc.Tables)

            {

                if (table.Rows.Count > 0)

                {

                    int intColumns_Count = table.Columns.Count;

                    for (int iCol = 1; iCol <= intColumns_Count; iCol++)

                    {

                        //----< @Loop: Columns in Table_Header >----

                        Cell cell = null;

                        try

                        {

                            cell = table.Cell(1, iCol);

                            if (cell.Range.Text.IndexOf(sHeader) == 0)

                            {

                                return table;

                            }

                        }

                        catch (Exception)

                        {

                            //< endofcolumns >

                            break;

                            //</ endofcolumns >

                        }

                        //----</ @Loop: Columns in Table_Header >----

                    }

                }

            }

            //< check >

            System.Windows.Forms.MessageBox.Show("Tabelle mit Header nicht gefunden. " + Environment.NewLine + "gesuchter Header:" + sHeader, "Header nicht gefunden");

            return null;

            //--</ find Table Header >--

            //-----------------</ find_Table_with_Header() >-----------------

        }

 

        private void insert_Photos_at_Table(Word.Table table_Photos)

        {

            //-----------------< insert_Photos_by_TableHeader() >-----------------

 

            //< init >

            int intColumns_count = table_Photos.Columns.Count;

            //</ init >

 

 

 

            //--< Word-FileDialog >--

            OpenFileDialog fileDialog = new OpenFileDialog();

            fileDialog.Filter = "image files (*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tif;*.tiff)|*.bmp;*.jpg;*.jpeg;*.png;*.gif;*.tif;*.tiff";

            fileDialog.Multiselect = true;

            //fileDialog.InitialDirectory = "B:\\2016";

            var result = fileDialog.ShowDialog();

            //--</ Word-FileDialog >--

 

            //< check >

            //*no file

            if (result != System.Windows.Forms.DialogResult.OK)

            { return; }

            //</ check >

 

 

            //--< check exits Entries >--

            try

            {

                Row row = table_Photos.Rows.Last;

                if (row.Range.InlineShapes.Count > 0)

                {

                    table_Photos.Rows.Add();

                }

            }

            catch (Exception)

            {

            }

            //--</ check exits Entries >--

 

 

 

            //-------< @Loop: Insert all Images >--------

            int iCol = 0;

            foreach (String sfileName in fileDialog.FileNames)

            {

                //------< Loop.Item  >------

                iCol++;

                if (iCol > 2)

                {

                    iCol = 1;

                    table_Photos.Rows.Add();

                }

                int iRow = table_Photos.Rows.Count;

 

                Cell cell = table_Photos.Cell(iRow, iCol);

                float image_Width = cell.Width - cell.RightPadding - cell.LeftPadding;

                //< get selection >

                Range act_Image_Range = cell.Range;

                //</ get selection >

 

                //----< Insert Image  >----

                //< insert picture from Link >

                Word.InlineShape inlineShape = _doc.InlineShapes.AddPicture(sfileName, LinkToFile: true, SaveWithDocument: true, Range: act_Image_Range);

                //</ insert picture from Link >

 

                //< scale >

                inlineShape.LockAspectRatio = Office.MsoTriState.msoTrue;

                inlineShape.Width = image_Width;

                //</ scale >

                //--< replace as png >--

                //*reduce memory 1 MB to 1kb

                //< cut >

                inlineShape.Select();

                _app.Selection.Cut();

                //</ cut >

 

                //*pasteBitmap is much smaller

                act_Image_Range.PasteSpecial(Link: false, DataType: Word.WdPasteDataType.wdPasteBitmap, Placement: Word.WdOLEPlacement.wdInLine, DisplayAsIcon: false);

                act_Image_Range.Select();

                //--</ replace as png >--

 

            }

            //-------</ @Loop: Insert all Images >--------

 

 

            //-----------------</ insert_Photos_at_Table_with_Header() >-----------------

        }

 

        //===================</ Methods >===================

        #endregion /Methods

 

    }

}

 

 

 

Ribbon Design

Den Designer-Code des Ribbons

VSTO Ribbonbar Addin->

Ribbon1.cs->Designer-View

namespace Addin_Word_Photos_into_Table

{

    partial class Ribbon1 : Microsoft.Office.Tools.Ribbon.RibbonBase

    {

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.IContainer components = null;

 

        public Ribbon1()

            : base(Globals.Factory.GetRibbonFactory())

        {

            InitializeComponent();

        }

 

        /// <summary> 

        /// Clean up any resources being used.

        /// </summary>

        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

 

        #region Component Designer generated code

 

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.tab1 = this.Factory.CreateRibbonTab();

            this.groupPhotos = this.Factory.CreateRibbonGroup();

            this.BtnPhotos_Insert = this.Factory.CreateRibbonButton();

            this.tbxPhotos_Tablename = this.Factory.CreateRibbonEditBox();

            this.tab1.SuspendLayout();

            this.groupPhotos.SuspendLayout();

            this.SuspendLayout();

            // 

            // tab1

            // 

            this.tab1.ControlId.ControlIdType = Microsoft.Office.Tools.Ribbon.RibbonControlIdType.Office;

            this.tab1.Groups.Add(this.groupPhotos);

            this.tab1.Label = "TabAddIns";

            this.tab1.Name = "tab1";

            // 

            // groupPhotos

            // 

            this.groupPhotos.Items.Add(this.BtnPhotos_Insert);

            this.groupPhotos.Items.Add(this.tbxPhotos_Tablename);

            this.groupPhotos.Label = "Photos";

            this.groupPhotos.Name = "groupPhotos";

            // 

            // BtnPhotos_Insert

            // 

            this.BtnPhotos_Insert.ControlSize = Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge;

            this.BtnPhotos_Insert.Label = "Fotos einfügen";

            this.BtnPhotos_Insert.Name = "BtnPhotos_Insert";

            this.BtnPhotos_Insert.OfficeImageId = "ActiveXImage";

            this.BtnPhotos_Insert.ShowImage = true;

            this.BtnPhotos_Insert.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(this.BtnPhotos_Insert_Click);

            // 

            // tbxPhotos_Tablename

            // 

            this.tbxPhotos_Tablename.Label = "Bilder Tabelle:";

            this.tbxPhotos_Tablename.Name = "tbxPhotos_Tablename";

            this.tbxPhotos_Tablename.Text = "Bilddokumentation";

            this.tbxPhotos_Tablename.TextChanged += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(this.tbxPhotos_Tablename_TextChanged);

            // 

            // Ribbon1

            // 

            this.Name = "Ribbon1";

            this.RibbonType = "Microsoft.Word.Document";

            this.Tabs.Add(this.tab1);

            this.Load += new Microsoft.Office.Tools.Ribbon.RibbonUIEventHandler(this.Ribbon1_Load);

            this.tab1.ResumeLayout(false);

            this.tab1.PerformLayout();

            this.groupPhotos.ResumeLayout(false);

            this.groupPhotos.PerformLayout();

            this.ResumeLayout(false);

 

        }

 

        #endregion

 

        internal Microsoft.Office.Tools.Ribbon.RibbonTab tab1;

        internal Microsoft.Office.Tools.Ribbon.RibbonGroup groupPhotos;

        internal Microsoft.Office.Tools.Ribbon.RibbonButton BtnPhotos_Insert;

        internal Microsoft.Office.Tools.Ribbon.RibbonEditBox tbxPhotos_Tablename;

    }

 

    partial class ThisRibbonCollection

    {

        internal Ribbon1 Ribbon1

        {

            get { return this.GetRibbon<Ribbon1>(); }

        }

    }

}

 

 

Mobile

.

yesmovies