using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using word=Microsoft.Office.Interop.Word;
using System.IO;
using System.Globalization;
namespace 字源小篆批量插入圖片
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (checkout() == true)
{
InsertPictoDocx();
MessageBox.Show("完成","",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
bool checkout()
{
if (textBox1.Text != "" && textBox2.Text != "")
{
return true;
}
else
{
return false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
//textBox1.Text = @"C:\Users\123\Music\@@@小篆\小篆(全)";
//textBox2.Text=@"C:\Users\123\Desktop\@20180427字源\A漢字書240字(缺033)";
}
void InsertPictoDocx()
{
word.Application wd = new word.Application();
var picFls = Directory.GetFiles(textBox1.Text);
var wordFls = Directory.GetFiles(textBox2.Text);
string w = ""; bool ok=false;
//word.Dialog dlg = word._Application.Dialogs[Word.WdWordDialog.wdDialogFileOpen];
//word._ApplicationClass.Dialogs[Word.WdWordDialog.wdDialogFileOpen];
//word.Dialogs wDlgs;
//var wDlgss = wDlgs.GetEnumerator();
//word.Dialog dlg = wDlgss[word.WdWordDialog.wdDialogFileOpen];
//dlg.Show(); dlg.Show();
foreach (var pf in picFls)
{
StringInfo uw = new StringInfo(pf);
w=uw.SubstringByTextElements(uw.LengthInTextElements - 5, 1);//取得字
//找word檔docx
foreach (var f in wordFls)
{
if (f.IndexOf(w) > -1 && f.Substring( f.LastIndexOf(".")) ==".docx" && f.Substring(0,2)!="~$")//找到的話
{
word.Document d = wd.Documents.Open(f);
foreach (word.Table t in d.Tables)
{
if (t.Cell(2, 6).Range.Characters[1].Text == w)
{
d.ActiveWindow.Visible = true;
if (t.Cell(2, 3).Range.InlineShapes.Count>0)
{
foreach (word.InlineShape item in t.Cell(2, 3).Range.InlineShapes)
{
item.Delete();
}
}
t.Cell(2, 3).Range.Text = "";
t.Cell(2, 3).Range.InlineShapes.AddPicture(pf);
t.Cell(2, 3).Range.InlineShapes[1].Height = float.Parse("35.4");
t.Cell(2, 3).Range.InlineShapes[1].Width = float.Parse("26");
}
}
//https://www.google.com.tw/search?ei=9-frWqWtNMiI8wXcoZBY&q=word+%22C%23%22+%22option%22+pic+do+not+compress&oq=word+%22C%23%22+%22option%22+pic+do+not+compress&gs_l=psy-ab.3...4979.6442.0.6712.2.2.0.0.0.0.81.137.2.2.0....0...1.1.64.psy-ab..0.0.0....0.8TaNEzUEcnI
//d.Application.Options.comp
//顯示選項設定詩話框//https://stackoverflow.com/questions/42392585/office-interop-access-ms-word-advanced-options
//d.Application.Dialogs[word.WdWordDialog.wdDialogToolsAdvancedSettings].Show();選項進階對話框
//d.Application.Dialogs[word.WdWordDialog.wdDialogFileOpen].Show();//開啟舊檔對話框
d.Application.ScreenRefresh();
d.Application.Dialogs[word.WdWordDialog.wdDialogToolsOptions].Show();
/*https://msdn.microsoft.com/en-us/library/ahzbkf8e.aspx
*/
//System.Diagnostics.Debugger.Break();//https://social.msdn.microsoft.com/Forums/vstudio/en-US/db9dfe97-c98d-4f4b-bb8f-ba2edffee988/can-i-do-a-visual-basic-vb-stop-in-c?forum=csharpgeneral
d.SaveAs2(Type.Missing, Type.Missing, Type.Missing, Type.Missing, false);
d.Close();
File.Move(f, f.Replace(textBox2.Text, textBox3.Text));//https://tinyurl.com/y8h6wxqa
ok = true;
break;
}//以上為word檔案處理
}
if (ok==false)
{
//MessageBox.Show("找不到「" + w + "」的字源檔案!");
////System.Diagnostics.Debugger.Break();
}
}
ok = false;
}
}
}