Free Support Forum - aspose.com

Copy/Paste as enhanced Metafile?

I'm currently evaluating Aspose.Words to replace Word automation. With Word Automation, I do alot of copying (from word or excel) and pasting (via PasteSpecial) into Word as an enhanced Metafile.

Ex. oPar.Range.PasteSpecial(Link:=False, DataType:=Word.WdPasteDataType.wdPasteEnhancedMetafile, Placement:=Word.WdOLEPlacement.wdInLine, DisplayAsIcon:=False)

.Is this possible to do with Aspose.Words? If so, how? If not, can you tell me a way around it?

Thanks, Barb

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. If you would like to copy/paste embedded OLE objects then you can import shape that contains this object. For example see the following code:

//Open source document

Document srcDoc = new Document(@"Test269\in.doc");

//Create destination document (empty document)

Document dstDoc = new Document();

//Get collection of shapes

NodeCollection shapes = srcDoc.GetChildNodes(NodeType.Shape, true);

//Import first shape anf insert it into dst document

Node dstNode = dstDoc.ImportNode(shapes[0], true, ImportFormatMode.KeepSourceFormatting);

dstDoc.FirstSection.Body.FirstParagraph.AppendChild(dstNode);

//Save output document

dstDoc.Save(@"Test269\out.doc");

Hope this helps.

Best regards.

Because I copy from many places (ie, word, excel, etc.), Is there a way to insert the contents of FROM THE CLIPBOARD (as an enhanced metafile) into the Aspose.Words document? It MUST be enhanced metafile to display properly. I tried a bitmap… but it just doesn’t display correctly. (ie

'If objData.GetDataPresent(GetType(System.Drawing.Bitmap)) Then

' Dim bmp As System.Drawing.Bitmap = CType(objData.GetData(GetType(System.Drawing.Bitmap)), System.Drawing.Bitmap)

' xBuilder.InsertImage(bmp, xwidth, xheight)

'End If

So, there is no pastespecial with aspose?

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for more information. No there is no way to insert content from clipboard. You can only import content. For example you can import content from Excel worksheets using Aspose.Cells. Please see the following demo to learn more

http://www.aspose.com/community/files/51/file-format-components/aspose.words/entry112559.aspx

Best regards.

Thanks, figured it out using a windows clipboard api. Thought I'd post for others:

Public Class ClipboardAPI

<DllImport("user32.dll", EntryPoint:="OpenClipboard", _

SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function OpenClipboard(ByVal hWnd As IntPtr) As Boolean

End Function

<DllImport("user32.dll", EntryPoint:="EmptyClipboard", _

SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function EmptyClipboard() As Boolean

End Function

<DllImport("user32.dll", EntryPoint:="SetClipboardData", _

SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function SetClipboardData(ByVal uFormat As Integer, ByVal hWnd As IntPtr) As IntPtr

End Function

<DllImport("user32.dll", EntryPoint:="CloseClipboard", _

SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function CloseClipboard() As Boolean

End Function

<DllImport("user32.dll", EntryPoint:="GetClipboardData", _

SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function GetClipboardData(ByVal uFormat As Integer) As IntPtr

End Function

<DllImport("user32.dll", EntryPoint:="IsClipboardFormatAvailable", _

SetLastError:=True, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _

Public Shared Function IsClipboardFormatAvailable(ByVal uFormat As Integer) As Short

End Function

End Class

dim xBuilder As DocumentBuilder

Const CF_ENHMETAFILE As Integer = 14

Dim henmetafile As IntPtr

Dim metaFile As System.Drawing.Imaging.Metafile

If ClipboardAPI.OpenClipboard(Me.Handle) Then

If ClipboardAPI.IsClipboardFormatAvailable(CF_ENHMETAFILE) <> 0 Then

henmetafile = ClipboardAPI.GetClipboardData(CF_ENHMETAFILE)

metaFile = New System.Drawing.Imaging.Metafile(henmetafile, True)

ClipboardAPI.CloseClipboard()

End If

End If

Try

xBuilder.InsertImage(metaFile, 574.55, 487.45)

Catch ex As Exception

End Try

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your code. I also translated your code in C#.

using System.Runtime.InteropServices;

public class ClipboardAPI

{

[DllImport("user32.dll", EntryPoint = "OpenClipboard", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]

public static extern bool OpenClipboard(IntPtr hWnd);

[DllImport("user32.dll", EntryPoint = "EmptyClipboard", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]

public static extern bool EmptyClipboard();

[DllImport("user32.dll", EntryPoint = "SetClipboardData", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]

public static extern IntPtr SetClipboardData(int uFormat, IntPtr hWnd);

[DllImport("user32.dll", EntryPoint = "CloseClipboard", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]

public static extern bool CloseClipboard();

[DllImport("user32.dll", EntryPoint = "GetClipboardData", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]

public static extern IntPtr GetClipboardData(int uFormat);

[DllImport("user32.dll", EntryPoint = "IsClipboardFormatAvailable", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]

public static extern short IsClipboardFormatAvailable(int uFormat);

}

//===================================================================

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

const int CF_ENHMETAFILE = 14;

IntPtr henmetafile;

System.Drawing.Imaging.Metafile metaFile = null;

if (ClipboardAPI.OpenClipboard(this.Handle))

{

if (ClipboardAPI.IsClipboardFormatAvailable(CF_ENHMETAFILE) != 0)

{

henmetafile = ClipboardAPI.GetClipboardData(CF_ENHMETAFILE);

metaFile = new System.Drawing.Imaging.Metafile(henmetafile, true);

ClipboardAPI.CloseClipboard();

}

}

try

{

builder.InsertImage(metaFile, 574.55, 487.45);

}

catch (Exception ex)

{ }

doc.Save(@"C:\Temp\out.doc");

Best regards.