Linked Fields

Helllo,

I had a query about linked field handling in Aspose Words. I’m trying to iterate through the linked fields in a document and change their source file. However, I can’t see where the source file name is accessible. For shapes I noticed that you can read/write the SourceFullName property inside the OLEFormat class, but there seems to be no equivalent for fields.

I am able to do this through the VSTO API but the performance is less than ideal. With VSTO, I can access the SourceFullName property through the LinkFormat structure, as shown below…

field.LinkFormat.SourceFullName = “newfile.xlsx”;

Can I do the same with Aspose?

Thanks in advance,
Gary

Hi

Thanks for your request. Aspose.Words recognizes LINK fields as Shapes with linked OLE object. I think you can try using the following code in your case.

// Open document
Document doc = new Document(@"Test008\in.doc");
// Get collection of shapes from the document
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
// Iterate through the collection
foreach (Shape shape in shapes)
{
    // Check whether current shape is linked OLE object
    if (shape.OleFormat != null && shape.OleFormat.IsLink)
    {
        // Print path to old file
        Console.WriteLine(shape.OleFormat.SourceFullName);
        // Set new path
        shape.OleFormat.SourceFullName = @"C:\Temp\out.doc";
    }
}
// Save the output document
doc.Save(@"Test008\out.doc");

If this does not help you, please attach your document for testing. I will investigate the issue and provide you more information.
Best regards.

Hello,

Thanks for the sample code. Unfortunately it does not work for the linked fields in my document. It does, however, effect linked charts.

I’ve attached my test document so you can try it for yourself.

Also, the following excerpt from the documentation suggests that non-“image” linked objects are treated as fields, not shapes…

“The LINK field in the file can be with an image or without.
Aspose.Words represents a LINK field with an image and without
differently in the model. A LINK field with an image will be
represented by a Shape object and no field. A LINK field without an image will be represeted as a normal field.”

This text is from the documentation for the FieldType enumeration here …

https://reference.aspose.com/words/net/aspose.words.fields/fieldtype/

Cheers,
Gary

Hi

Thank you for additional information. You can try using the following code to update file name in the Link fields:

// Open document
Document doc = new Document(@"Test010\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Get collection of FieldStart nodes
NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
// Loop through FieldStart nodes
foreach (FieldStart start in starts)
{
    // Check whether current field start is start of Link field
    if (start.FieldType == FieldType.FieldLink)
    {
        // We should get field code
        string fieldCode = string.Empty;
        Node currentNode = start.NextSibling;
        // Get Field code
        while (currentNode.NodeType != NodeType.FieldSeparator)
        {
            Node nextNode = currentNode.NextSibling;
            if (currentNode.NodeType == NodeType.Run)
                fieldCode += (currentNode as Run).Text;
            // Remove old field code
            currentNode.Remove();
            currentNode = nextNode;
        }
        // We should get file name from field code
        // Ctructure of the Link field is the following:
        // LINK Excel.Sheet.8 C:\\Users\\Gary\\Documents\\Chambers\\Test\\TestSheet.xlsx Sheet1!R5C5 \a \f 4 \r
        // 
        // - This is the name of fields, for link field it is always equels LINK
        // - This is type of linked object, it might be Excel.Sheet.8, Word.Docuemnt.8 etc
        // - This is file name of the linked object, we will replace it 
        // - contains flags of the field
        Regex regex = new Regex(@"\s*(?\S+)\s+(?\S+)\s+(?\S+)\s+(?.+)");
        Match match = regex.Match(fieldCode);
        string fileName = match.Groups["name"].Value;
        // Replace old file name with new file name
        fieldCode = fieldCode.Replace(fileName, @"C:\Temp\test.xslx");
        // Insert new field code
        builder.MoveTo(currentNode);
        builder.Write(fieldCode);
    }
}
// Save output document
doc.Save(@"Test010\out.docx");

Hope this helps.
In addition, I think the following article could be useful for you:
https://docs.aspose.com/words/net/working-with-fields/
Best regards,