Hi,
Do you have any code to get EmbedAttachment from a document and replace values using mail merge then reattach to the mail doc to the same position?
Thanks
Jithin V P
Hi,
Thanks. It is fixed. Thank you very much for your help. One more question, Do you have any code to get EmbedAttachment from a document and replace values in that using mail merge then reattach to the main document in to the same position?
@jithin.p Could you please elaborate your requirements and attach your input and expected output documents? We will check the documents and provide you more information.
Sorry. Due to security reasons i am not able to provide the document. I will share the screenshot of the embed attachment. In the screenshot you can see the embed attachments. I need to read data in those attachments and need edit the values and need to reattach to the same position of the main document. That is my requirement. I have the following code but it is not reattaching the embed attachment to main doc.
foreach (Shape shape in OutPut.GetChildNodes(NodeType.Shape, true))
{
// Check if the shape contains an embedded object
if (shape.OleFormat != null)
{
// Check if the embedded object is of the type you want to extract (e.g., Word document)
if (shape.OleFormat != null && shape.OleFormat.IsLink == false)
{
//Only process embedded OLE objects(not linked)
string oleFileName = shape.OleFormat.IconCaption;
if (oleFileName.EndsWith(".dotx") || oleFileName.EndsWith(".doc"))
{
// Extract the embedded document
using (MemoryStream stream = new MemoryStream())
{
shape.OleFormat.Save(stream);
stream.Position = 0;
// Load the embedded document
Document embeddedDoc = new Document(stream);
// Perform your editing on the embedded document
embeddedDoc.Range.Replace("Confidential", "Test", new FindReplaceOptions());
// Save the edited embedded document back to a stream
shape.OleFormat.Save(stream);
}
}
}
}
}
Thanks
Jithin V P
@jithin.p Unfortunately, screenshots will not allow us to analyze the problem and test the scenario. So it is better to provide sample documents. Could you please at least let us know what is your input document format?
It is .dotx and pdf. I have the following code but it is not reattaching the embed attachment to main doc.
foreach (Shape shape in OutPut.GetChildNodes(NodeType.Shape, true))
{
// Check if the shape contains an embedded object
if (shape.OleFormat != null)
{
// Check if the embedded object is of the type you want to extract (e.g., Word document)
if (shape.OleFormat != null && shape.OleFormat.IsLink == false)
{
//Only process embedded OLE objects(not linked)
string oleFileName = shape.OleFormat.IconCaption;
if (oleFileName.EndsWith(".dotx") || oleFileName.EndsWith(".doc"))
{
// Extract the embedded document
using (MemoryStream stream = new MemoryStream())
{
shape.OleFormat.Save(stream);
stream.Position = 0;
// Load the embedded document
Document embeddedDoc = new Document(stream);
// Perform your editing on the embedded document
embeddedDoc.Range.Replace("Confidential", "Test", new FindReplaceOptions());
// Save the edited embedded document back to a stream
shape.OleFormat.Save(stream);
}
}
}
}
}
@jithin.p There is no way re replace data in the OLE object. You can insert new OLE object with changed data and remove the original OLE object:
Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
List<Shape> oleShapes = doc.GetChildNodes(NodeType.Shape, true).Cast<Shape>()
.Where(s => s.OleFormat != null && s.OleFormat.IsLink == false).ToList();
foreach (Shape shape in oleShapes)
{
if (shape.OleFormat.ProgId == "Word.Document.12")
{
// Extract the embedded document
using (MemoryStream stream = new MemoryStream())
{
shape.OleFormat.Save(stream);
stream.Position = 0;
// Load the embedded document
Document embeddedDoc = new Document(stream);
// Perform your editing on the embedded document
embeddedDoc.Range.Replace("test", "Replaced", new FindReplaceOptions());
using (MemoryStream chnagedStream = new MemoryStream())
{
embeddedDoc.Save(chnagedStream, SaveFormat.Docx);
chnagedStream.Position = 0;
// Insert new OLE object with chnaged data and remove original one.
builder.MoveTo(shape);
builder.InsertOleObject(chnagedStream, shape.OleFormat.ProgId, true, null);
shape.Remove();
}
}
}
}
doc.Save(@"C:\Temp\out.docx");
Thank you very much. It worked. Can i set the same file name while reinserting? Now it is reattaching with name “Mincrosoft Word Template”.
Thanks
Jithin V P
@jithin.p You can use Shape.ImageData.ImageBytes
as presentation image. Please modify the code like this:
Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
List<Shape> oleShapes = doc.GetChildNodes(NodeType.Shape, true).Cast<Shape>()
.Where(s => s.OleFormat != null && s.OleFormat.IsLink == false).ToList();
foreach (Shape shape in oleShapes)
{
if (shape.OleFormat.ProgId == "Word.Document.12")
{
// Extract the embedded document
using (MemoryStream stream = new MemoryStream())
{
shape.OleFormat.Save(stream);
stream.Position = 0;
// Load the embedded document
Document embeddedDoc = new Document(stream);
// Perform your editing on the embedded document
embeddedDoc.Range.Replace("test", "Replaced", new FindReplaceOptions());
using (MemoryStream chnagedStream = new MemoryStream())
{
embeddedDoc.Save(chnagedStream, SaveFormat.Docx);
chnagedStream.Position = 0;
// Insert new OLE object with chnaged data and remove original one.
builder.MoveTo(shape);
using (MemoryStream presentation = new MemoryStream(shape.ImageData.ImageBytes))
builder.InsertOleObject(chnagedStream, shape.OleFormat.ProgId, true, presentation);
shape.Remove();
}
}
}
}
doc.Save(@"C:\Temp\out.docx");
Thank you very much. It worked. I can see the filename as it is.
Thanks
JithinV P