[WebMethod]
public bool Write(string args,string sfilename)
{
try
{
Microsoft.Office.Interop.Word.Document wDoc = null;
Microsoft.Office.Interop.Word.Application wApp = null;
Object SaveFileName = sfilename;
CreateWordDocument("c:\\sqsp.doc", ref wDoc, ref wApp);
Object missing = Type.Missing;
string[] bookmarks ={ "age", "agep", "copyno", };
string[] bookmarktexts = args.Split('|');
FillData(bookmarks, bookmarktexts, ref wDoc, ref wApp);
wDoc.SaveAs(ref SaveFileName, 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);
wDoc.Close(ref missing, ref missing, ref missing);
return true;
}
catch
{
return false;
}
}
private string CreateWordDocument(string FileName, ref Microsoft.Office.Interop.Word.Document wDoc, ref Microsoft.Office.Interop.Word.Application WApp)
{
if (FileName == "") return;
Microsoft.Office.Interop.Word.Document thisDocument = null;
Microsoft.Office.Interop.Word.FormFields formFields = null;
Microsoft.Office.Interop.Word.Application thisApplication = new Microsoft.Office.Interop.Word.ApplicationClass();//error
thisApplication.Visible = false;
thisApplication.Caption = "";
thisApplication.Options.CheckSpellingAsYouType = false;
thisApplication.Options.CheckGrammarAsYouType = false;
Object filename = FileName;
Object ConfirmConversions = false;
Object ReadOnly = false ;
Object AddToRecentFiles = false;
Object PasswordDocument = System.Type.Missing;
Object PasswordTemplate = System.Type.Missing;
Object Revert = System.Type.Missing;
Object WritePasswordDocument = System.Type.Missing;
Object WritePasswordTemplate = System.Type.Missing;
Object Format = System.Type.Missing;
Object Encoding = System.Type.Missing;
Object Visible = System.Type.Missing;
Object OpenAndRepair = System.Type.Missing;
Object DocumentDirection = System.Type.Missing;
Object NoEncodingDialog = System.Type.Missing;
Object XMLTransform = System.Type.Missing;
try
{
Microsoft.Office.Interop.Word.Document wordDoc =
thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
ref NoEncodingDialog, ref XMLTransform);
thisDocument = wordDoc;
wDoc = wordDoc;
WApp = thisApplication;
formFields = wordDoc.FormFields;
return "true";
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
return ex.ToString();
}
}
private void FillData(string[] BookMark, string[] Text, ref Microsoft.Office.Interop.Word.Document wDoct, ref Microsoft.Office.Interop.Word.Application wAppt)
{
for (int i = 0; i < BookMark.Length; i++)
{
if (wAppt.ActiveDocument.Bookmarks.Exists(BookMark[i]))
{
Object titleNo = BookMark[i];
wAppt.ActiveDocument.Bookmarks.get_Item(ref titleNo).Select();
wAppt.Selection.TypeText(Text[i]);
}
}
}
以上webMethod如果测试一下是没有问题的,能够正常运行.可是如果我在另网站项目中本地引用它.就会在"红色字休"处报错!!why???为什么不能webservices自身运行ok,而调用却会报错误呢??
[此贴子已经被作者于2006-7-10 17:18:23编辑过]