Hey Apose Team,
I’m using C# and aspose words to update links in a word document. After I’ve updated the links, I wish to break the link.
Is there a way to break OLE links after updating the links?
@JThomas98 OLE object in MS Word document is considered as linked if it’s source name is specified. Please see OleFormat.IsLink property and OleFormat.SourceFullName property. Linked OLE objects are not actually stored in the document, so to change linked OLE object to not linked, you should reinsert OLE object as not linked. For example see the following code:
Document doc = new Document(@"C:\Temp\out.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Get OLE object shape.
Shape oleShape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
if (oleShape.OleFormat != null && oleShape.OleFormat.IsLink)
{
// Move DocumentBuilder to the shape.
builder.MoveTo(oleShape);
// reinsert OLE object as not linked.
// NOTE the linked file must be acessible.
using (FileStream oleData = File.OpenRead(oleShape.OleFormat.SourceFullName))
using (MemoryStream olePresentation = new MemoryStream(oleShape.ImageData.ImageBytes))
{
Shape newOleShape = builder.InsertOleObject(oleData, oleShape.OleFormat.ProgId, oleShape.OleFormat.OleIcon, olePresentation);
// It might be required to set other shape properties like zise and position.
newOleShape.Width = oleShape.Width;
newOleShape.Height = oleShape.Height;
// ....
// Remove the original OLE shape.
oleShape.Remove();
}
}
doc.Save(@"C:\Temp\out.docx");
Okay, this is helpful! Thanks, I’ll let you know how this goes.
@alexey.noskov,
I Implemented the code you suggested, but my document still shows the dummy data rather than what is in the excel file that the dummy object is pointing to.
and there is no longer a link in the dummy template. So, breaking the link part worked. But it did not replace my dummy part with the actual excel table that it was linked to.
@alexey.noskov
Here are the files I’m using:
TestFiles.zip (75.8 KB)
You’d notice that I have the 2 tables in the word document linked to the workbook called TB_Templates_Generic. My program changes these links to point to Template_Generic using the code
foreach (Shape shp in AllShapes)
{
if (shp.OleFormat != null)
{
//Replace links whose path contains the text "_Template"
if (shp.OleFormat.SourceFullName.IndexOf("_Template", StringComparison.CurrentCultureIgnoreCase) == -1)
{
continue;
}
shp.OleFormat.SourceFullName = @"H:\QHP_2019\RptEngineDev\Reports\FinalReports\Reports\TextDoc\Type1\GEN1\TestFiles\Template_Generic.xlsm";
if (!RetVal)
RetVal = true;
}
}
After this, I use the code you provided to break these links in the hope that it is already pointed to the updated excel workbook and that the data matches to what is in Template_Generic excel workbook. Sorry for the multiple replies. Just wanted to explain myself thoroughly so you wouldn’t have any confusions.
@JThomas98 The code provided above replaces the linked OLE object with embedded OLE object, but the file is still inserted as an OLE object. If it is required to replace linked OLE object with it’s actual content, you should first convert the object to format supported by Aspose.Words (to DOCX or HTML) and then use DocumentBuilder.InsertDocument
or DocumentBuilder.InsertHtml
method to insert content of the file into the Word document. You can use Aspose.Cells to convert Excel sheets to HTML, DOCX or TXT, which can be then loaded by Aspose.Words:
https://docs.aspose.com/cells/net/different-ways-to-save-files/#how-to-save-file-to-different-formats
Hey @alexey.noskov,
I came across Field.Unlink method in aspose words documentation. Is this similar to the Field.Unlink in office? I tried your approach of converting an excel sheet to docx format to insert into my word document. But the problem is that I just want a specific range of cells from an excel worksheet that I wish to insert in word.
@JThomas98 Yes, Field.Unlink
and Document.UnlinkFields
does the same as unlink field feature in MS Word, i.e. replaces the fields in the document with their actual values. But you should not that Aspose.Words does not deal with linked Excel files.
In this case you should use Aspose.Cells to get the required range and then insert this range into MS Word document using Aspose.Words.
I’ve tried capturing the range as an image and inserting it into word document. This worked but had its problems which I reported to Aspose. It’s still in the works. Other than that, I tried saving a worksheet as DOCX and then inserting it into the word document. But in this case, I was only able save an entire worksheet as docx rather than a range which is not what I wanted. Is there something else that I could try?
@JThomas98 I am afraid I cannot suggest you other option. I think the question should be better asked to Aspose.Cells team, because they have much more experience with Excel files.
Alright, thanks!