0

在我正在做的项目中,我必须查找我的 word 文档是否包含任何图像或表格。有没有办法使用 c# 代码来做到这一点。我找不到任何与此相关的代码。

提前致谢

4

1 回答 1

0

试试下面的代码.....

Use Microsoft.Office.Interop

此代码在 asp.net 和 winforms 中工作,但我在 Windows XP Pro 上测试它,我不知道它在 Windows Server 上工作

using System;
using System.Drawing;
using System.IO;
using System.Threading;
using Microsoft.Office.Interop.Word;
using Microsoft.VisualBasic.Devices;
using Page=System.Web.UI.Page;

public partial class DefaultPage : Page {
    private Application m_word;
    private int m_i;

    protected void Page_Load(object sender, EventArgs e) {
        object missing = Type.Missing;
        object fileName = "C:\\Test.docx";
        m_word = new Application();
        m_word.Documents.Open(ref fileName,
                              ref missing, ref missing, ref missing, ref missing, ref missing,
                              ref missing, ref missing, ref missing, ref missing, ref missing,
                              ref missing, ref missing, ref missing, ref missing, ref missing);

        try {
            for (int i = 0; i < m_word.ActiveDocument.InlineShapes.Count; i++) {
                m_i = i;
                Thread thread = new Thread(CopyFromClipboardInlineShape);
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
            }
            for (int i = 0; i < m_word.ActiveDocument.Shapes.Count; i++) {
                m_i = i;
                Thread thread = new Thread(CopyFromClipboardShape);
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
            }
        }
        finally {
            object save = false;
            m_word.Quit(ref save, ref missing, ref missing);
            m_word = null;
        }
    }

    protected void CopyFromClipboardInlineShape() {
        InlineShape inlineShape = m_word.ActiveDocument.InlineShapes[m_i];
        inlineShape.Select();
        m_word.Selection.Copy();
        Computer computer = new Computer();
        Image img = computer.Clipboard.GetImage();
        /*...*/
    }

    protected void CopyFromClipboardShape() {
        object missing = Type.Missing;
        object i = m_i + 1;
        Shape shape = m_word.ActiveDocument.Shapes.get_Item(ref i);
        shape.Select(ref missing);
        m_word.Selection.Copy();
        Computer computer = new Computer();
        Image img = computer.Clipboard.GetImage();
        /*...*/
    }
}
于 2012-06-28T12:51:10.423 回答