Update field links in Word doc

How do I update links in a Word doc? Using the Office Excel Interop library I was able to do it this way:
word_application.ActiveDocument.Fields[1].LinkFormat.SourceFullName = sourceFile
word_application.ActiveDocument.Fields[2].LinkFormat.SourceFullName = sourceFile

The code is in Python, but that’s OK, ignore the syntax, I’m looking for the correct method/params to do this and I cannot find the equivalences in Aspose.Words docs.

Thanks!
-rob

@rkoch1,

Thanks for your inquiry. Please use OleFormat.SourceFullName property to get or set the path and name of the source file for the linked OLE object. Please check the following code example. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);

if (shape.OleFormat != null)
{
    shape.OleFormat.SourceFullName = MyDir + "new document.xlsx";
}
doc.Save(MyDir + "17.8.docx");

I see the SourceFullName is updated when I open the document and when I checked the Link Properties in Word it has the updated link so that piece works. However, the object is still referencing the templated/older chart.What would be the way to refresh the OLE object or when you’re in Word you can go to File > Info > Edit Links to Files > Update Now?

document.UpdateFields() didn’t do anything.

Then… after the objects are refreshed, I’d like to break the links – using the Office library, I could do this:
word_application.ActiveDocument.Fields[1].LinkFormat.BreakLink()

Thanks.

@rkoch1,

Thanks for your inquiry. Please use OleFormat.AutoUpdate property to specify whether the link to the OLE object is automatically updated or not in Microsoft Word. We suggest you please set the value of this property as true.

If you still face problem, please share your input, output and expected output Word documents along with code example to reproduce your issue at our end. We will investigate the issue on our side and provide you more information.

Here’s the code we used:

            var license = new Aspose.Words.License();
            license.SetLicense(@"Aspose.Total.lic");
            var wordDocPath = @"Template.docx";
            var excelWbPath = @"Format.xlsx";
            Console.WriteLine("Starting test.");
            Console.WriteLine("Now updating...");
            var document = new Document(wordDocPath);
            var shape = (Shape)document.GetChild(NodeType.Shape, 0, true);
            shape.OleFormat.SourceFullName = excelWbPath;

            var shape2 = (Shape)document.GetChild(NodeType.Shape, 1, true);
            shape2.OleFormat.SourceFullName = excelWbPath;

            document.Save("Final.docx");

            Console.WriteLine("Completed. File is at Final.docx");
            Console.ReadLine();

I redid this in C# and was able to replicate my problem. Clearly Aspose.Words piece that updates OLE Objects are broken. Word doc comes up asking me to update link. I said no, while the chart still isn’t updated. Plus, I am not sure if there’s a “Break Link” functionality or not.

Also how shall I send you my sample project?

Thanks!
-rob

@rkoch1,

Please ZIP and attach your input Word and Excel documents here for testing. We will investigate the issue on our side and provide you more information.

Hi @tahir.manzoor ,
When I am updating the OLE links of an excel file in word using Aspose.word, it updates the link, but it adds “\f 0” extra at the end of the link I don’t know what is the issue can you tell me why it adds these character at the end ?
the original link is : \\Hdrive\\Test\\Templates\\Charts\\charts_Template_Main.xlsm Testsheet!R1C7:R37C19 \a \p
The updated link is : \\Hdrive\\Test\\Templates\\Charts\\charts_New_Main.xlsm Testsheet!R1C7:R37C19 \a \p \f 0
as I only updated the Template to New but why did it add \f 0 ?
my code is :

Document doc = new Document(TxtWordFile.Text);
NodeCollection ShapeCollection = doc.GetChildNodes(NodeType.Shape, true);
for (int i = 0; i < ShapeCollection.Count; i++)
{
    Aspose.Words.Drawing.Shape shape = (Aspose.Words.Drawing.Shape)doc.GetChild(NodeType.Shape, i, true);
    if (shape.OleFormat != null)
    {
        string oleFormatString = shape.OleFormat.SourceFullName;
        string newstring = oleFormatString.Replace("_template_", "_new_");
        shape.OleFormat.SourceFullName = newstring;
        lblcount++;
    }
}
doc.Save(outputfile);

@omar.sidiqy Could you please attach your source and output documents here? We will check the issue and provide you more information.

Thanks, @alexey.noskov
these are my files, Testing.docx is the original one and testing_updateLinks.docx is the updated one.
I just changed the excel file name from TestCase to TestCaseCopy.
Files.zip (47.2 KB)

Document doc = new Document(TxtWordFile.Text);
NodeCollection ShapeCollection = doc.GetChildNodes(NodeType.Shape, true);
for (int i = 0; i < ShapeCollection.Count; i++)
{
    Aspose.Words.Drawing.Shape shape = (Aspose.Words.Drawing.Shape)doc.GetChild(NodeType.Shape, i, true);
    if (shape.OleFormat != null)
    {
        string oleFormatString = shape.OleFormat.SourceFullName;
        string newstring = oleFormatString.Replace("Case", "CaseCopy");

        shape.OleFormat.SourceFullName = newstring;
        RtxtLinks.AppendText($"{oleFormatString}{Environment.NewLine}");
        RtxtLinks.AppendText($"UPDATED LINK : {Environment.NewLine}");
        RtxtLinks.AppendText($"{newstring} {Environment.NewLine}{Environment.NewLine}");
        lblcount++;

    }
}
LblLinkCount.Text = lblcount.ToString();
doc.Save(outputfile);

@omar.sidiqy Thank you for additional information. You can reproduce this by simply open/save your document:

Document doc = new Document(@"C:\Temp\Testing.docx");
doc.Save(@"C:\Temp\out.docx");

This occurs because Aspose.Words reads the linked object as Shape and then reconstructs the LINK field upon saving the document. On this stage the default value of \f switch is added to the field. It looks like this does not change the behavior of the field.