0
本当にうまくいかないコードで問題が発生しました。Word interopで作業していましたが、Excelに変更しても私には私用のCreateWordDocument(Excel .document ADOCとexcelapp.document.open)Excelを見つけて置き換える際の問題C#
private void FindAndReplace(Microsoft.Office.Interop.Excel.Application wordApp, object findText, object replaceWithText)
{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundLike = false;
object nmatchAllForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiactitics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
wordApp.Selection.Find.Execute(ref findText,
ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundLike,
ref nmatchAllForms, ref forward,
ref wrap, ref format, ref replaceWithText,
ref replace, ref matchKashida,
ref matchDiactitics, ref matchAlefHamza,
ref matchControl);
}
string pathImage = null;
private void CreateWordDocument(object filename, object savaAs, object image)
{
List<int> processesbeforegen = getRunningProcesses();
object missing = Missing.Value;
Excel.Application excelApp = new Excel.Application();
Excel.Document aDoc = null;
if (File.Exists((string)filename))
{
DateTime today = DateTime.Now;
object readOnly = false; //default
object isVisible = false;
excelApp.Visible = false;
aDoc = excelApp.Document.Open(ref filename, ref missing, ref readOnly,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
aDoc.Activate();
//Find and replace:
this.FindAndReplace(excelApp, "$$firstname$$", txtFirstName.Text);
}
else
{
MessageBox.Show("file dose not exist.");
return;
}
//Save as: filename
aDoc.SaveAs2(ref savaAs, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
//Close Document:
//aDoc.Close(ref missing, ref missing, ref missing);
MessageBox.Show("File created.");
List<int> processesaftergen = getRunningProcesses();
killProcesses(processesbeforegen, processesaftergen);
}
public List<int> getRunningProcesses()
{
List<int> ProcessIDs = new List<int>();
//here we're going to get a list of all running processes on
//the computer
foreach (Process clsProcess in Process.GetProcesses())
{
if (Process.GetCurrentProcess().Id == clsProcess.Id)
continue;
if (clsProcess.ProcessName.Contains("WINEXCEL"))
{
ProcessIDs.Add(clsProcess.Id);
}
}
return ProcessIDs;
}
private void killProcesses(List<int> processesbeforegen, List<int> processesaftergen)
{
foreach (int pidafter in processesaftergen)
{
bool processfound = false;
foreach (int pidbefore in processesbeforegen)
{
if (pidafter == pidbefore)
{
processfound = true;
}
}
if (processfound == false)
{
Process clsProcess = Process.GetProcessById(pidafter);
clsProcess.Kill();
}
}
}
//Méthode Enabled Controles:
private void tEnabled(bool state)
{
txtFirstName.Enabled = state;
}
//Load the Template Document:
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
tFilename.Text = openFileDialog1.FileName;
tEnabled(true);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
CreateWordDocument(tFilename.Text, saveFileDialog1.FileName, pathImage);
tEnabled(false);
//printDocument1.DocumentName = SaveDoc.FileName;
}
}
}
}
Stackoverflowへようこそ。 http://stackoverflow.com/help/how-to-askを読んで、あなたが何を達成しようとしているのか、どこに止まっているのかを正確に知るように質問を精査してください。具体的には、「Issues」と「Error Messages」はあまり役に立ちません。少なくとも、例外が何を言ったのか、どこで正確に起こされたのかを教えてください。 – nozzleman
オフトピックですが、.NET 4+では 'ref missing 'が廃止されました – MickyD