image (noninlineshape) from Word to clipboard to file

linurb

I have a Word document (referred to as "doc" in the code below) with a bunch of .jpg images. Some of them have text wrapped around them (= Shapes), some of them don't (= InlineShapes). I am able to save the InlineShapes like so:

InlineShape ils = doc.InlineShapes[1];
ils.Select();
application.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Bitmap)) {
    Image image = (Image)data.GetData(DataFormats.Bitmap, true);
        image.Save("c:\\image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}

But if I try to get the other ones by replacing the first two lines with these –

Shape s = doc.Shapes[1];
s.Select();

– it won't work. And if I check the formats with "data.GetFormats()" I notice that Bitmap isn't listed, which explains why it doesn't work. Instead it lists the "Office Drawing Shape Format". I suppose that I should try to convert the Shape to a InlineShape somehow, but I haven't been able to make it work. When I try to do it like this –

s.ConvertToInlineShape();

– I get an "invalid parameter" exception.

linurb

OK, the problem seems to have been that I tried to convert it at the wrong time. If I loop through all Shapes and convert them before trying to do anything else it works fine.

int number = doc.InlineShapes.Count;
MessageBox.Show(number.ToString()); // 0 to begin with

foreach (Microsoft.Office.Interop.Word.Shape s in doc.Shapes) {
    MessageBox.Show(s.Type.ToString());
    if (s.Type.ToString() == "msoTextBox") {
        MessageBox.Show(s.TextFrame.TextRange.Text);
    } else if (s.Type.ToString() == "msoPicture") {
        s.ConvertToInlineShape();
    }
}

number = doc.InlineShapes.Count;
MessageBox.Show(number.ToString());  // Now it's 1 as it should be

InlineShape ils = doc.InlineShapes[1];
ils.Select();
application.Selection.Copy();

IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Bitmap)) {
    Image image = (Image)data.GetData(DataFormats.Bitmap, true);
    image.Save("c:\\image.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

image (noninlineshape) from Word to clipboard to file

From Dev

Copy image from clipboard to file

From Dev

How to save image from clipboard to file in UWP

From Dev

Pasting image from clipboard to MS Word has wrong aspect ratio

From Dev

How to copy an image to the clipboard from a file using command line?

From Dev

How to copy an image to the clipboard from a file using command line?

From Dev

How can I paste an image from clipboard directly to a file?

From Dev

Paste an image from the Clipboard into Excel

From Dev

Save text from the clipboard to a file

From Dev

Vim: copy from file to clipboard

From Dev

Paste Copied File From Clipboard To File System

From Dev

Pasting an image directly from a client clipboard

From Dev

How to copy a image from clipboard in Python?

From Dev

AutoHotKey -Windows Copy an image from a website to clipboard

From Dev

Sending accented characters to Clipboard from a batch file

From Dev

Save last "copy" from clipboard to file

From Dev

How to copy picture from a file to clipboard

From Dev

Word Count from a file

From Dev

Read a word from a file

From Dev

Copying to clipboard from .txt file via .bat file

From Dev

Command-line utility to copy file to clipboard from file path

From Dev

Redmine paste-image-from-clipboard not working after upgrading

From Dev

GIMP: Setting default DPI when creating an image from clipboard

From Dev

How to paste image from clipboard to Outlook email Body?

From Dev

Word: How to paste formatting from image to image?

From Dev

read word by word from file in C++

From Dev

Reading word by word from text file in C

From Dev

Pasting from a file browser and HTML5 Clipboard API

From Dev

How would I save an RTF file from clipboard in C#?

Related Related

  1. 1

    image (noninlineshape) from Word to clipboard to file

  2. 2

    Copy image from clipboard to file

  3. 3

    How to save image from clipboard to file in UWP

  4. 4

    Pasting image from clipboard to MS Word has wrong aspect ratio

  5. 5

    How to copy an image to the clipboard from a file using command line?

  6. 6

    How to copy an image to the clipboard from a file using command line?

  7. 7

    How can I paste an image from clipboard directly to a file?

  8. 8

    Paste an image from the Clipboard into Excel

  9. 9

    Save text from the clipboard to a file

  10. 10

    Vim: copy from file to clipboard

  11. 11

    Paste Copied File From Clipboard To File System

  12. 12

    Pasting an image directly from a client clipboard

  13. 13

    How to copy a image from clipboard in Python?

  14. 14

    AutoHotKey -Windows Copy an image from a website to clipboard

  15. 15

    Sending accented characters to Clipboard from a batch file

  16. 16

    Save last "copy" from clipboard to file

  17. 17

    How to copy picture from a file to clipboard

  18. 18

    Word Count from a file

  19. 19

    Read a word from a file

  20. 20

    Copying to clipboard from .txt file via .bat file

  21. 21

    Command-line utility to copy file to clipboard from file path

  22. 22

    Redmine paste-image-from-clipboard not working after upgrading

  23. 23

    GIMP: Setting default DPI when creating an image from clipboard

  24. 24

    How to paste image from clipboard to Outlook email Body?

  25. 25

    Word: How to paste formatting from image to image?

  26. 26

    read word by word from file in C++

  27. 27

    Reading word by word from text file in C

  28. 28

    Pasting from a file browser and HTML5 Clipboard API

  29. 29

    How would I save an RTF file from clipboard in C#?

HotTag

Archive