我有一个string列作为一个date,我想选择它作为一个date.
可能吗?
我的样本数据格式是; month/day/year- >12/31/2011
我遇到了经典场景,当在.NET中创建Word COM对象时(通过Microsoft.Office.Interop.Word程序集),即使我正确关闭并释放对象,WinWord进程也不会退出.
我已经将其缩小到使用Word.Documents.Add()方法.我可以在没有问题的情况下以其他方式使用Word(打开文档,修改内容等),并在我告诉它时退出WinWord.exe.一旦我使用Add()方法(并且仅在添加模板时),该过程就会继续运行.
这是一个简单的例子,它可以重现这个问题:
Dim word As New Word.Application()
word.Visible = False
Dim documents As Word.Documents = word.Documents
Dim doc As Word.Document = documents.Add(Template:=CObj(templatePath), NewTemplate:=False, DocumentType:=Word.WdNewDocumentType.wdNewBlankDocument, Visible:=False)
'' dispose objects
doc.Close()
While (Marshal.ReleaseComObject(doc) <> 0)
End While
doc = Nothing
While (Marshal.ReleaseComObject(documents) <> 0)
End While
documents = Nothing
word.Quit()
While (Marshal.ReleaseComObject(word) <> 0)
End While
word = Nothing
GC.Collect()
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我正在创建和正确处理对象,甚至采取额外的步骤来循环Marsha.ReleaseComObject,直到它返回正确的代码.在其他方面使用Word对象很好,只是那些讨厌的Documents.Add让我感到悲伤.是否有另一个在此过程中创建的对象需要引用和处理?我需要遵循另一个处理步骤吗?别的什么?非常感谢您的帮助 :)
Update: 我在处理步骤结束时尝试了GC.Collect,但仍然没有运气.
Update 2: 我已经将问题缩小到使用自定义模板.当我调用Documents.Add(...)时,我为新文档指定了一个自定义模板.如果我不这样做而是调用没有参数的Add(),那么问题就不会发生.
我正在尝试使用Interop.Word.Application打开.docx文件并转换为PDF.它作为控制台应用程序,但如果我在我的Web应用程序中使用它不起作用.我试图看到该文件夹的权限.我给了'网络服务'完全控制,但我仍然没有设置对象引用word.Documents.Open.你能告诉我可能出现什么问题吗?我遇到了这个错误.请告诉我.我很感激任何建议.谢谢.
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
object oMissing = System.Reflection.Missing.Value;
word.Visible = false;
word.ScreenUpdating = false;
string fileName = @"c:\OUTPUT\test.docx");
Document doc = word.Documents.Open(filename, 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);
doc.Activate();
Run Code Online (Sandbox Code Playgroud) 我正在尝试在空文档中添加页眉和页脚.
当将docx更改为zip时,我使用此代码在word/document.xml中添加Header部分.
ApplyHeader(doc);
public static void ApplyHeader(WordprocessingDocument doc)
{
// Get the main document part.
MainDocumentPart mainDocPart = doc.MainDocumentPart;
// Delete the existing header parts.
mainDocPart.DeleteParts(mainDocPart.HeaderParts);
// Create a new header part and get its relationship id.
HeaderPart newHeaderPart = mainDocPart.AddNewPart<HeaderPart>();
string rId = mainDocPart.GetIdOfPart(newHeaderPart);
// Call the GeneratePageHeaderPart helper method, passing in
// the header text, to create the header markup and then save
// that markup to the header part.
GeneratePageHeaderPart("Test1").Save(newHeaderPart);
// Loop through all section properties in …Run Code Online (Sandbox Code Playgroud) 我使用Word和OpenXml在C#ASP.NET Web应用程序中提供邮件合并功能:
1)上传包含许多预定义字符串的文档以进行替换.
2)使用OpenXML SDK 2.0我打开Word文档,将mainDocumentPart作为字符串并使用Regex执行替换.
3)然后我使用OpenXML创建一个新文档,添加一个新的mainDocumentPart并将替换产生的字符串插入到这个mainDocumentPart中.
但是,新文档中的所有格式/样式等都将丢失.
我猜我可以单独复制和添加样式,定义,注释部分等来模仿原始文档.
但是有没有一种方法使用Open XML复制文档,允许我在新副本上执行替换?
谢谢.
我正在尝试从我的C#代码中打印一个word文档.我使用了12.0.0.0 Word Interop,我正在尝试做的是在文档打印之前弹出打印对话框.我不是100%肯定所有这些的语法,因为我无法让我的代码工作:(任何想法?
提前致谢!
我承认,我对使用Interop库非常陌生,但人们似乎总是给出的建议是,记录一个宏并查看vba代码.问题是,宏没有准确记录我正在做的事情:单击快速样式将其应用于当前选择.
我的任务非常简单:我需要将快速样式应用于段落(Microsoft.Office.Interop.Word.Paragraph).但是,使用set_style命令仅应用基本格式,并且段落保持原始快速样式选择(正常).
使用Remou的方法虽然对我有用,但它看起来与我自己的代码非常相似,我无法使它工作,我认为这可能是我对对象模型的理解有点过时了.
public void AddParagraph(string text, string styleName = null)
{
Paragraph paragraph = _document.Content.Paragraphs.Add();
if (styleName != null)
{
paragraph.Range.set_Style(_document.Styles[styleName]);
}
paragraph.Range.Text = text;
paragraph.Range.InsertParagraphAfter();
}
Run Code Online (Sandbox Code Playgroud)
然后我用eg调用它AddParagraph("A title", "Heading 1");,但是使用上面的包装器构建我的文档的结果是,没有应用完整的样式,只有字体,颜色,大小和粗体/斜体.
我使用自己的.dotx文件,使用我自己定义和命名的样式,但只是从Remou复制代码使用我自己的模板,所以我不认为这是问题,并且使用该代码我无法弄清楚如何用自己的样式附加多个段落.
任何人都可以指出我的方法有什么问题,或者至少我如何能够让Remou为我的要求提供答案?:)
使用VSTO,我在功能区设计器中创建了一个自定义选项卡,并在那里添加了一些组和按钮控件.当用户单击其中一个按钮时,我想连接到SharePoint站点并在Word中打开Word文档(实例已打开).我已经能够连接到SharePoint站点并拥有我想要打开的文档的URL.
但是我怎样才能将这些文档加载到Word中?我已经在Word中的代码隐藏中,所以如何定位我所在的Word实例并在那里打开文件?
提前致谢.
我想通过互操作打开一个word文档,并且word必须在进程中可见.它看起来相当直接,因为在word文档的open函数中有一个名为"visible"的参数.但是word在后台.我错过了吗?
static void Main(string[] args)
{
Microsoft.Office.Interop.Word.Application word = null;
word = new Microsoft.Office.Interop.Word.Application();
object inputFile = "c:\\test.docx";
object confirmConversions = false;
object readOnly = true;
object visible = true;
object missing = Type.Missing;
// Open the document...
Microsoft.Office.Interop.Word.Document doc = null;
doc = word.Documents.Open(
ref inputFile, ref confirmConversions, ref readOnly, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref visible,
ref missing, ref missing, ref missing, ref missing);
doc.Activate();
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)