Beispiel Code, C#
Für Entwickler:
Entwurfsmodus
Unter Visual Studio VSTO
Die Ribbon Bar als Ribbon1.cs Design Mode
In der Datei Ribbon1.cs->Code-Ansicht befindet sich der Beispielcode für die komplette Anwendung.
using System;
//----< 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 wordAddin_Photos { public partial class Fotos { #region Region: Ribbonbar //===================< RibbonBar >=================== private void Ribbon1_Load(object sender, RibbonUIEventArgs e) {
} //===================</ RibbonBar >=================== #endregion /Ribbonbar
#region --Button-- //===================< Buttons >=================== private void btnInsert_Photos_Click(object sender, RibbonControlEventArgs e) { string sHeader = tbxHeader_Photos.Text; if (sHeader == "") { sHeader = "Bild"; }
insert_Photos_at_Table_with_Header(sHeader); } //===================</ Buttons >=================== #endregion /Buttons
#region --Methods-- //===================< Methods >=================== private void insert_Photos_at_Table_with_Header(string sHeader) { //-----------------< insert_Photos_by_TableHeader() >-----------------
//< init > Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument; Word.Application app = Globals.ThisAddIn.Application; //</ init >
//--< find Table Header >-- //*Loop all tables and look for header Word.Table table_Photos = null; int intColumn_Photo = -1; float width_of_Column = 100; foreach (Word.Table table in doc.Tables) { if(table.Rows.Count > 0) { Word.Row row_Header = table.Rows[1]; foreach (Word.Cell cell in row_Header.Cells) { if(cell.Range.Text.Contains(sHeader)) { table_Photos = table; intColumn_Photo = cell.ColumnIndex; Word.Column column = table.Columns[intColumn_Photo];
width_of_Column = column.PreferredWidth; } } } } //< check > if (table_Photos == null) { System.Windows.Forms.MessageBox.Show("Tabelle mit Header nicht gefunden. " + Environment.NewLine + "gesuchter Header:" + sHeader ,"Header nicht gefunden"); return; } //</ check >
//--</ find Table Header >--
//----< delete button >---- //*delete Photo Button foreach(Word.InlineShape inlineShape in doc.InlineShapes ) { //--< InlineShape >-- if(inlineShape.Type== Word.WdInlineShapeType.wdInlineShapeOLEControlObject) { if(inlineShape.OLEFormat.ClassType.ToString()== "Forms.CommandButton.1") { //--< Button >-- var objButton = inlineShape.OLEFormat.Object; string sTitle=objButton.Caption;
if (sTitle.IndexOf("Fotos") > -1) { //< Delete > inlineShape.ScaleWidth = 1; inlineShape.Delete(); //</ Delete >
break; } //--</ Button >-- }
MessageBox.Show("Word Button"); } //--</ InlineShape >-- } //----</ delete button >----
//--< 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 >
//app.Selection.TypeText("\n");//Convert.ToChar(11).ToString()
//-------< @Loop: Insert all Images >-------- foreach (String sfileName in fileDialog.FileNames) { //------< Loop.Item >------
//< add Row > int lastRow = table_Photos.Rows.Count; if(table_Photos.Cell(lastRow, intColumn_Photo).Range.InlineShapes.Count!=0) { table_Photos.Rows.Add(); } //</ add Row >
int intRow_Photo = table_Photos.Rows.Count; //< get selection > Word.Range act_Image_Range = table_Photos.Cell(intRow_Photo,intColumn_Photo).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 = width_of_Column; //app.CentimetersToPoints(intSize); //size //</ 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 >--------
//-------< @Loop: create all JPG Thumbnails >-------- //set_Borders_all_Images(1); //-------</ @Loop: create all JPG Thumbnails >-------- //-----------------</ insert_Photos_at_Table_with_Header() >----------------- }
//===================</ Methods >=================== #endregion /Methods
} } |
Ribbon Design.cs
namespace wordAddin_SafetyCenter { partial class Fotos : Microsoft.Office.Tools.Ribbon.RibbonBase { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null;
public Fotos() : 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.tabPhotos = this.Factory.CreateRibbonTab(); this.grpPhotos_Insert = this.Factory.CreateRibbonGroup(); this.btnInsert_Photos = this.Factory.CreateRibbonButton(); this.label1 = this.Factory.CreateRibbonLabel(); this.tbxHeader_Photos = this.Factory.CreateRibbonEditBox(); this.tabPhotos.SuspendLayout(); this.grpPhotos_Insert.SuspendLayout(); this.SuspendLayout(); // // tabPhotos // this.tabPhotos.ControlId.ControlIdType = Microsoft.Office.Tools.Ribbon.RibbonControlIdType.Office; this.tabPhotos.Groups.Add(this.grpPhotos_Insert); this.tabPhotos.Label = "Photos"; this.tabPhotos.Name = "tabPhotos"; // // grpPhotos_Insert // this.grpPhotos_Insert.Items.Add(this.btnInsert_Photos); this.grpPhotos_Insert.Items.Add(this.label1); this.grpPhotos_Insert.Items.Add(this.tbxHeader_Photos); this.grpPhotos_Insert.Label = "Fotos einfügen"; this.grpPhotos_Insert.Name = "grpPhotos_Insert"; // // btnInsert_Photos // this.btnInsert_Photos.ControlSize = Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge; this.btnInsert_Photos.Label = "Auswählen"; this.btnInsert_Photos.Name = "btnInsert_Photos"; this.btnInsert_Photos.OfficeImageId = "PasteBitmap"; this.btnInsert_Photos.ScreenTip = "Fotos in Tabelle mit Bild-Spalte einfügen"; this.btnInsert_Photos.ShowImage = true; this.btnInsert_Photos.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(this.btnInsert_Photos_Click); // // label1 // this.label1.Label = "Spalte"; this.label1.Name = "label1"; // // tbxHeader_Photos // this.tbxHeader_Photos.Label = ":"; this.tbxHeader_Photos.Name = "tbxHeader_Photos"; this.tbxHeader_Photos.ScreenTip = "Header of Photos"; this.tbxHeader_Photos.ShowLabel = false; this.tbxHeader_Photos.Text = "Bild"; // // Fotos // this.Name = "Fotos"; this.RibbonType = "Microsoft.Word.Document"; this.Tabs.Add(this.tabPhotos); this.Load += new Microsoft.Office.Tools.Ribbon.RibbonUIEventHandler(this.Ribbon1_Load); this.tabPhotos.ResumeLayout(false); this.tabPhotos.PerformLayout(); this.grpPhotos_Insert.ResumeLayout(false); this.grpPhotos_Insert.PerformLayout(); this.ResumeLayout(false);
}
#endregion
internal Microsoft.Office.Tools.Ribbon.RibbonTab tabPhotos; internal Microsoft.Office.Tools.Ribbon.RibbonGroup grpPhotos_Insert; internal Microsoft.Office.Tools.Ribbon.RibbonButton btnInsert_Photos; internal Microsoft.Office.Tools.Ribbon.RibbonEditBox tbxHeader_Photos; internal Microsoft.Office.Tools.Ribbon.RibbonLabel label1; }
partial class ThisRibbonCollection { internal Fotos Ribbon1 { get { return this.GetRibbon<Fotos>(); } } } } |