2
使用.NET 4.0/VS2010,Office 2007的將多個文檔與Word.Application(COM互操作)結合導致格式不正確?
注意,我接受任何其他的方法來完成最後的結果。我沒有與Word結婚,但我的確有限制,無法包含任何許可的第三方工具(無Aspose),並且希望儘量減少用戶機器上的任何安裝。這是一個企業環境,所以我確實有一些控制權。
我需要打印2個或3個字的文件,作爲一個單一的文件。我的代碼如下,它的作品....除了第二個文檔的格式總是有點關閉(段落間距,字體,邊距)。奇怪的是,第三個文檔(它有不同的頁面方向)很好。有時用戶需要將文檔發送到傳真服務器,因此第一頁將是傳真封面。
我在哪裏失敗?方法KillCom(object o)調用System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
public void CombineMultipleDocuments(string coverSheetPath, string[] documentPaths, string destinationFile, bool makeVisible)
{
List<string> docs = new List<string>();
if (string.IsNullOrWhiteSpace(destinationFile))
throw new ArgumentException("The destinationfile is required");
try
{
//strip invalid paths
foreach (string p in documentPaths)
{
if (!string.IsNullOrWhiteSpace(p) && File.Exists(p))
{
docs.Add(p);
}
}
if (docs.Count == 0)
throw new ArgumentException("There are no documents to print");
//open the cover sheet
wrdApp = new Word.Application();
wrdApp.Visible = makeVisible;
//if there is no cover sheet open the first document and append the remaining docs to it
if (string.IsNullOrWhiteSpace(coverSheetPath))
{
coverSheetPath = docs[0];
docs.RemoveAt(0);
}
wrdDoc = wrdApp.Documents.Open(coverSheetPath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing
, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing
, ref oMissing, ref oMissing);
wrdRange = wrdDoc.Content;
//a little space
wrdRange.InsertParagraphAfter();
//attach the rest of the documents
foreach (string path in docs)
{
//open the new source document to be inserted
Word._Document newDoc = wrdApp.Documents.Open(path, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing
, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing
, ref oMissing, ref oMissing);
//continues page break
wrdRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
wrdRange.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage);
//create a new section for our new content
Word.Section sec = wrdRange.Sections.Add(ref oMissing, ref oMissing);
//copy the source document's styles, fonts, etc
sec.PageSetup.Orientation = newDoc.PageSetup.Orientation;
sec.Range.Font = newDoc.Content.Font;
//unlink footer and headers
sec.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false;
sec.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.FormattedText = newDoc.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.FormattedText;
sec.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false;
sec.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.FormattedText = newDoc.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.FormattedText;
wrdRange.FormattedText = newDoc.Content.FormattedText;
//close
newDoc.Saved = true;
newDoc.Close(ref oFalse, ref oMissing, ref oMissing);
newDoc = null;
KillCOM(newDoc);
}
//the diary function needs these variables to be set
Word.Variables wrdVars = wrdDoc.Variables;
wrdVars.Add("Letter", WrdPropLETTER_NAME);
wrdVars.Add("FileKey", WrdPropFILE_KEY);
wrdVars.Add("LetterTo", WrdPropLETTER_TO);
wrdVars.Add("LetterFirstName", WrdPropLETTER_FNAME);
wrdVars.Add("LetterLastName", WrdPropLETTER_LNAME);
wrdVars.Add("LetterCompany", WrdPropLETTER_COMPANY);
wrdVars.Add("LetterCategory", WrdPropLETTER_CATEGORY);
wrdVars.Add("CurrentHeaderDoc",WrdPropCUR_HEADERDOC);
wrdVars.Add("CurrentDataDoc", WrdPropCUR_DATADOC);
wrdDoc.Protect(Word.WdProtectionType.wdAllowOnlyFormFields, ref oMissing, "mojojojo");
wrdDoc.SaveAs(destinationFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing
, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing
, ref oMissing, ref oMissing);
//close if hidden
if (!makeVisible)
{
wrdDoc.Saved = true;
wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);
wrdApp.Quit(ref oFalse, ref oMissing, ref oMissing);
wrdDoc = null;
wrdApp = null;
}
//clean up
if (File.Exists(coverSheetPath))
File.Delete(coverSheetPath);
foreach (string path in documentPaths)
{
if (File.Exists(path))
File.Delete(path);
}
}
catch (Exception e)
{
string error = e.ToString();
}
finally
{
wrdRange = null;
wrdDoc = null;
wrdApp = null;
KillCOM(wrdRange);
KillCOM(wrdDoc);
KillCOM(wrdApp);
}
}
你可能會合並不同的樣式定義。 – SLaks 2012-04-19 21:27:36