Saving an OpenXML Document (Word) generated from a template
By : Azhari Subroto
Date : March 29 2020, 07:55 AM
To fix this issue Are you referring to the OpenXml SDK? Unfortunately, as of OpenXml SDK 2.0, there's no SaveAs method. You'll need to: Make a temporary copy of your template file, naming it whatever you want. Perform your OpenXml changes on the above file. Save the appropriate sections (ie. using the .myWordDocument.MainDocumentPart.Document.Save() method for the main content or someHeaderPart.Header.Save() method for a particular header).
|
Copy data from existing WORD document to new WORD document created from template (in code)
By : coch9894
Date : March 29 2020, 07:55 AM
help you fix your problem Every Word document has a ThisDocument object. If you write VBA code which uses ThisDocument, that object always points to the ThisDocument object in the same file as your code. ActiveDocument, on the other hand, always points to the document that is active in Word, that is, the one that is visible in the Word window at the moment. If possible, try to avoid using ActiveDocument since it can point to a different document than what you think. In order to access a Document object other than the one your code is in, you cannot use ThisDocument, so instead you create a reference to the other document: code :
Option Explicit
Sub FindOtherDocument()
Dim oOtherDocument As Document
Set oOtherDocument = Application.Documents("Filled.docx")
Call MsgBox("This is the name of the file which oOtherDocument points to: " & oOtherDocument.FullName)
End Sub
|
OpenXml Convert from Word document to HTML with Header
By : ahidal01
Date : March 29 2020, 07:55 AM
|
create a new document from word template with multiple pages using documentformat.openxml
By : user3413977
Date : March 29 2020, 07:55 AM
hope this fix your issue The sample code below (which is unit-tested and works) does what you are trying to achieve. It is based on the following interpretation of the question and assumptions: "Control place holders" means "Rich Text content controls", which are called block-level structured document tags (SDTs) in Open XML lingo and are thus represented by the SdtBlock class in the Open XML SDK. The content controls have tags, meaning the relevant w:sdt elements have grandchild elements like . Those tags are used to link the data received from the REST service to the content controls. The data is provided as a Dictionary , mapping tag values to the content control text (data). code :
public class ContentControlWriter
{
private readonly IDictionary<string, string> _contentMap;
/// <summary>
/// Initializes a new ContentControlWriter instance.
/// </summary>
/// <param name="contentMap">The mapping of content control tags to content control texts.
/// </param>
public ContentControlWriter(IDictionary<string, string> contentMap)
{
_contentMap = contentMap;
}
/// <summary>
/// Transforms the given WordprocessingDocument by setting the content
/// of relevant block-level content controls.
/// </summary>
/// <param name="wordDocument">The WordprocessingDocument to be transformed.</param>
public void WriteContentControls(WordprocessingDocument wordDocument)
{
MainDocumentPart part = wordDocument.MainDocumentPart;
part.Document = (Document) TransformDocument(part.Document);
}
private object TransformDocument(OpenXmlElement element)
{
if (element is SdtBlock sdt)
{
string tagValue = GetTagValue(sdt);
if (_contentMap.TryGetValue(tagValue, out string text))
{
return TransformSdtBlock(sdt, text);
}
}
return Transform(element, TransformDocument);
}
private static object TransformSdtBlock(OpenXmlElement element, string text)
{
return element is SdtContentBlock
? new SdtContentBlock(new Paragraph(new Run(new Text(text))))
: Transform(element, e => TransformSdtBlock(e, text));
}
private static string GetTagValue(SdtElement sdt) => sdt
.Descendants<Tag>()
.Select(tag => tag.Val.Value)
.FirstOrDefault();
private static T Transform<T>(T element, Func<OpenXmlElement, object> transformation)
where T : OpenXmlElement
{
var transformedElement = (T) element.CloneNode(false);
transformedElement.Append(element.Elements().Select(e => (OpenXmlElement) transformation(e)));
return transformedElement;
}
}
|
Image Size Not Applied when opening OpenXML Word Document Created in C#
By : Aissam Musnaoui
Date : March 29 2020, 07:55 AM
should help you out this is a pretty old question but I think it's worth answering anyway since this seems to come up a lot. I also used the above sample and it works well, except that cx and cy are hard-coded and distort the image.
|