我有一個包含佔位符(如/FirstName/, /LastName/
等)的MS Word 2003文檔...我使用Microsoft Office 12 Library來讀取文件並使用通配符搜索Find.Text = "/[A-Z]*/"
。它工作正常,Find.Execute
確實找到了佔位符。但是因爲它只返回布爾值,所以我不知道如何獲取佔位符本身。Office Word Library - 通配符搜索的結果
能否請你告訴我怎麼去由通配符搜索選項文本
我有一個包含佔位符(如/FirstName/, /LastName/
等)的MS Word 2003文檔...我使用Microsoft Office 12 Library來讀取文件並使用通配符搜索Find.Text = "/[A-Z]*/"
。它工作正常,Find.Execute
確實找到了佔位符。但是因爲它只返回布爾值,所以我不知道如何獲取佔位符本身。Office Word Library - 通配符搜索的結果
能否請你告訴我怎麼去由通配符搜索選項文本
我會建議使用字的書籤,而不是佔位符,因爲文本不被顯示給用戶,你可以使用的東西LA這無論你想自動插入文本,因爲你可以得到一個特定的書籤的範圍:
protected void insertTextAt(string bookmarkName, string text,
bool useDefaults = true, string fontName = "Arial",
int fontSize = 11, int bold = 0,bool newLine = true)
{
try
{
Object oBookMarkName = bookmarkName;
WordInterop.Range wRng =
this.wDoc.Bookmarks.get_Item(ref oBookMarkName).Range;
wRng.Text = text;
if (!useDefaults)
{
wRng.Font.Bold = bold;
wRng.Font.Name = fontName;
wRng.Font.Size = fontSize;
}
if (newLine)
{
wRng.Text += "\r\n";
}
wRng.Font.Bold = 0;
}
catch (Exception e)
{
String exceptionString = String.Format("Bookmark {0} could not"
+" be found in template {1}",bookmarkName,this.template);
throw new Exception(exceptionString,e);
}
}
我與現有的程序共享文件。如果我將佔位符更改爲書籤,可能會導致舊程序無法正常工作。順便說一句,謝謝 – 2012-02-17 05:39:29
您不必將佔位符更改爲o書籤,您只需將整個佔位符標記爲書籤,並且文本將自動被覆蓋,這樣您就可以獲得向後兼容性。如果你堅持只使用佔位符,試試這個'Application.Selection.Find.Replacement.Text =「找到」;'和這個http://msdn.microsoft.com/en-us/library/f65x8z3d(v=vs。 80)的.aspx – trebor 2012-02-17 07:24:27
下面是我的代碼,您的關注。 首先,這是letter.doc
文件
親愛的/姓/ /中間名/ /名字/:
歡迎來到我們的節目。我們承諾爲您提供最高 優質的客戶服務....
我也有存儲鍵/值對每一個佔位符
...
"/FirstName/" : "read from database"
"/MiddleName/" : "read from database"
"/LastName/" : "read from database"
...
一個Dictionary<string, string> Data
我有一種方法讀取.doc文件並替換佔位符:
oWordApp = new MSWord.ApplicationClass();
doc = oWordApp.Documents.Open(ref fileName,
ref missing, ref readOnly,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible,
ref missing, ref missing, ref missing);
doc.Activate();
doc.Select();
oWordApp.Selection.Find.ClearFormatting();
oWordApp.Selection.Find.MatchWildcards = true;
oWordApp.Selection.Find.Wrap = MSWord.WdFindWrap.wdFindContinue;
oWordApp.Selection.Find.Text = "/[A-Z]*/";
bool isFound = true;
while(isFound == true) {
isFound = oWordApp.Selection.Find.Execute(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);
if(isFound == true) {
//use the database to do the replacing
//how to get the placeholder itself, such as "/FirstName/", "/LastName/",...
}
}
請提供有關方法的信息。另請提供您使用的代碼。 – 2012-02-16 18:55:23
顯示你的代碼將幫助出Hoang – MethodMan 2012-02-16 19:00:47
謝謝你,我發佈了下面的代碼 – 2012-02-17 05:39:46