翻譯|使用教程|編輯:胡濤|2023-05-06 10:15:39.000|閱讀 174 次
概述:本文將演示如何在 C# 中的 Word 文檔中添加或修改 VBA 宏。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.Words 是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺應(yīng)用程序中直接使用Microsoft Word。此外,
Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
Microsoft 支持在 MS Word 文檔中添加 VBA 宏來自動或觸發(fā)各種操作。在以編程方式處理 Word 文檔時,您可能必須操作 VBA 宏。對于此類情況,本文將演示如何在 C# 中的 Word 文檔中添加或修改 VBA 宏。
此版本中添加了一個新屬性 VbaModule.SourceCode 以獲取或設(shè)置 VBA 項目模塊源代碼。以下代碼示例演示如何使用 VbaModule.SourceCode 屬性修改 VBA 宏。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET Document doc = new Document(dataDir + "test.docm"); VbaProject project = doc.VbaProject; const string newSourceCode = "Test change source code"; // Choose a module, and set a new source code. project.Modules[0].SourceCode = newSourceCode;
在 Word 文檔中創(chuàng)建重復(fù)節(jié)內(nèi)容控件
MS Word 允許您在 Word 文檔中插入帶有 XML 映射的重復(fù)內(nèi)容控件。您可以在整個段落或表格行周圍插入重復(fù)內(nèi)容控件。我們在最新版本的 Aspose.Words 中添加了此功能。此版本中添加了新的枚舉類型RepeatingSectionItem 。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
CustomXmlPart xmlPart = doc.CustomXmlParts.Add("Books",
"<books><book><title>Everyday Italian</title><author>Giada De Laurentiis</author></book>" +
"<book><title>Harry Potter</title><author>J K. Rowling</author></book>" +
"<book><title>Learning XML</title><author>Erik T. Ray</author></book></books>");
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Title");
builder.InsertCell();
builder.Write("Author");
builder.EndRow();
builder.EndTable();
StructuredDocumentTag repeatingSectionSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSection, MarkupLevel.Row);
repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", "");
table.AppendChild(repeatingSectionSdt);
StructuredDocumentTag repeatingSectionItemSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row);
repeatingSectionSdt.AppendChild(repeatingSectionItemSdt);
Row row = new Row(doc);
repeatingSectionItemSdt.AppendChild(row);
StructuredDocumentTag titleSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", "");
row.AppendChild(titleSdt);
StructuredDocumentTag authorSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", "");
row.AppendChild(authorSdt);
doc.Save(dataDir + "Document.docx");
如您所知,LINQ 報告引擎使您能夠使用一組擴展的報告功能來構(gòu)建報告。該引擎使您能夠在報告模板中引用應(yīng)用程序的業(yè)務(wù)對象。我們在此版本中添加了 XmlDataSource、JsonDataSource 和 CsvDataSource 類,以使用 XML、JSON 和 CSV 作為數(shù)據(jù)源來使用 LINQ 報告生成報告。有關(guān)此功能的完整詳細信息,請閱讀以下文章。
MS Word 允許您將自定義文檔屬性鏈接到書簽。在 MS Word 中,您可以在文檔屬性對話框下選中此選項。在此版本中,我們在 DocumentProperty 類中添加了 LinkSource 和 IsLinkToContent 屬性,并在 CustomDocumentProperties 類中添加了方法 AddLinkToContent。以下代碼示例顯示如何將自定義文檔屬性鏈接到書簽。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "test.docx");
// Retrieve a list of all custom document properties from the file.
CustomDocumentProperties customProperties = doc.CustomDocumentProperties;
// Add linked to content property.
DocumentProperty customProperty = customProperties.AddLinkToContent("PropertyName", "BookmarkName");
// Also, accessing the custom document property can be performed by using the property name.
customProperty = customProperties["PropertyName"];
// Check whether the property is linked to content.
bool isLinkedToContent = customProperty.IsLinkToContent;
// Get the source of the property.
string source = customProperty.LinkSource;
// Get the value of the property.
string value = customProperty.Value.ToString();
以上便是如何在 C# 中的 Word 文檔中添加或修改 VBA 宏 ,要是您還有其他關(guān)于產(chǎn)品方面的問題,歡迎咨詢我們,或者加入我們官方技術(shù)交流群。
歡迎下載|體驗更多Aspose產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn