【发布时间】:2019-01-04 10:37:40
【问题描述】:
private void CreateDocument(string date, string name, string phone, string father_name, string address, string village, string post, string taluka, string city, string district, string pincode, string product, int price)
{
string currentPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
// Get the Word application object.
Word._Application word_app = new Word.Application();
// Make Word visible (optional).
word_app.Visible = true;
// Create the Word document.
object missing = Type.Missing;
Word._Document word_doc = word_app.Documents.Add(
ref missing, ref missing, ref missing, ref missing);
// Create a header paragraph.
Word.Paragraph para = word_doc.Paragraphs.Add(ref missing);
//para.Range.Text = "Chrysanthemum Curve";
object style_name = "Heading 1";
para.Range.set_Style(ref style_name);
para.Range.InsertParagraphAfter();
// Add more text.
para.Range.Text = "";
para.Range.InsertParagraphAfter();
// Save the current font and start using Courier New.
string old_font = para.Range.Font.Name;
para.Range.Font.Name = "Courier New";
// Add the equations.
para.Range.Font.Bold = 1;
para.Range.Font.Size = 16;
para.Range.Text = "EXPRESS PARCEL WITH COD" +"\v" + "ADVANCE PAYMENT" + "\v" + "CODE NO 560023100235" + "\v" + "BOOKING AT PBC BG 560046" + "\v" + "COD FOR RS " + price + "/ -" + "\v" + "PLEASE COLLECT CASH " + price + "/-" + "\v" + "(" + NumberToWords(price) + ")" + "\v" + "BILLER 5792" + Environment.NewLine + Environment.NewLine + Environment.NewLine;
// Start a new paragraph and then
// switch back to the original font.
para.Range.InsertParagraphAfter();
para.Range.Font.Name = "Courier New";
para.Range.Font.Bold = 1;
para.Range.Font.Size = 16;
para.Range.Text = "TO\v" + name + father_name + "\v" + address + "\v" + village + "\v" + post + "\v" + taluka + "\v" + city + "\v" + district + "\v" + "PINCODE-" + pincode + "\v" + "MOBILE-" + phone + Environment.NewLine+Environment.NewLine;
// Start a new paragraph and then
// switch back to the original font.
para.Range.InsertParagraphAfter();
para.Range.Font.Name = "Times New Roman";
para.Range.Font.Size = 12;
para.Range.Text = "FROM" + "\v" + "AVK SHOPPING PVT LTD" + "\v" + "NO U16, BHUVANESHWARINAGAR" + "\v" + "MAGADI ROAD BANGALORE - 23" + Environment.NewLine+Environment.NewLine+ Environment.NewLine+ Environment.NewLine;
// Start a new paragraph and then
// switch back to the original font. para.Range.InsertParagraphAfter();
para.Range.Font.Name = "Times New Roman";
para.Range.Font.Size = 12;
para.Range.Text = "PARCEL CONTAINS" + " (" + product + ") " + "AND THE PARCEL DO" + "\v" + "NOT CONTAIN ANY DANGEROUS ARTICLE PROHIBITED BY" + "\v" + "POSTAL REGULATIONS";
Thread.Sleep(10000);
//if(!(File.Exists(@currentPath + name + date + ".docx")))
//{
//}
//else
//{
// //File.Create(@currentPath + name + date + ".docx");
//}
string filename = name + "-" + phone + "-" + ".docx";
MessageBox.Show(currentPath);
object ob = @currentPath + filename;
word_doc.SaveAs2(ref ob);
MessageBox.Show("Document created successfully !");
// Close.
Marshal.ReleaseComObject(word_doc);
Marshal.ReleaseComObject(word_app);
}
上面的代码运行良好,但有时word_doc.SaveAs2(ref ob);its 给出错误 RPC 服务器不可用。 (HRESULT 例外:0x800706BA)' 我已经检查了一些建议,但没有一个解决了我的 rpc 问题。
一旦创建 .docx 文件,它就会自动打开。我不想在创建文件后打开 .docx,因为我的工具一次创建 100 个文件。如果每个文档都打开,我的电脑将使用这么多资源。 (只是我想静默保存文件)。
这是我正在使用的 windowsforms - vs2017。
【问题讨论】:
-
请花点时间查看有关在help center 中提问的网站规则。只允许一个问题/问题。在 新 问题中提出您的第二个问题。请检查您是否正确标记它 - 阅读有关标记用途的标记信息。
-
这段代码运行的上下文是什么? WPF?窗体?网络项目?
-
是windows窗体。