#

 
 
Das folgende Beispiel zeigt, wie man Fotos mit einem FileDialog in einer Word Addin als Ribbonbar einfügen kann.

 
Zunächst wurde in einem VSTO Addin in einer Ribbonbar ein Button eingefügt, welcher für das Importieren von Bildern zuständig sein sollte.
Die ausgelöste Methode (nicht sub oder function) öffnet dann in einem System.Forms OpenFileDialog ein Auswahl-Fenster, mit welchem man Fotos auswählen kann

//-----------------< a0_Insert_Photos_by_Selection() >-----------------
//< init >
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
Word.Application app = Globals.ThisAddIn.Application;
//</ 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 >--
 

 
 
Anschliessend werden alle filedialog.filenames als InlineShapes eingefügt

//< insert picture from Link >
Word.InlineShape inlineShape = doc.InlineShapes.AddPicture(sfileName, LinkToFile: false, SaveWithDocument: true, Range: act_Image_Range);
//</ insert picture from Link >
 

 
Damit die Originalgröße der Fotos komprimiert wird, wende ich folgenden Trick an:
Ich schneide die eingefügten Fotos nochmal aus und füge diese dann als dargestellte Bitmap ein

//--< 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();
 

 
Zur Laufzeit sieht der Code dann in C# wie hier aus.

 
 
Als Video Tutorial

 
 
 
Hier der komplette C# Code für Visual Studio VSTO Addin

private void insert_Photos_by_Selection_InLINE(int intSize)
{
//-----------------< a0_Insert_Photos_by_Selection() >-----------------
//< init >
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
Word.Application app = Globals.ThisAddIn.Application;
//</ 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 >
 
 
app.Selection.TypeText("\n");//Convert.ToChar(11).ToString()
 
 
//-------< @Loop: Insert all Images >--------
foreach (String sfileName in fileDialog.FileNames)
{
//------< Loop.Item >------
//< get selection >
Word.Range act_Image_Range = app.Selection.Range;
//</ get selection >
 
//----< Insert Image >----
//< insert picture from Link >
Word.InlineShape inlineShape = doc.InlineShapes.AddPicture(sfileName, LinkToFile: false, SaveWithDocument: true, Range: act_Image_Range);
//</ insert picture from Link >
 
//< scale >
inlineShape.LockAspectRatio = Office.MsoTriState.msoTrue;
inlineShape.Width = 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 >--
//< add spacer >
//Selection.MoveRight
app.Selection.TypeText("\n");
app.Selection.TypeText("\n");
//</ add spacer >
 
 
}
//-------</ @Loop: Insert all Images >--------
 
//-------< @Loop: create all JPG Thumbnails >--------
set_Borders_all_Images(1);
//-------</ @Loop: create all JPG Thumbnails >--------
//-----------------</ a0_Insert_Photos_by_Selection() >-----------------
}

 
 
Folgende Namespaces müssen eingebunden werden

using Microsoft.Office.Tools.Ribbon;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;//OpenfileDialog

 
Mobile

.

123movies