如何在从HTML转换为RTF时强制Word嵌入图像?

时间:2014-08-28 07:42:55

标签: html ms-word rtf office-automation

我正在使用Word的自动化功能将带有图像的HTML文件转换为RTF文件。

问题是Word没有将图像嵌入到RTF文件中,而是使用INCLUDEPICTURE从外部引用图像。这种连锁效应是Windows Rich Edit控件忽略此关键字并且不呈现任何图像。

LPDISPATCH lpd = docs.Open(COleVariant(sHtmlFilename),  // FileName
                    varFalse,                           // ConfirmConversions
                    varFalse,                           // ReadOnly
                    varFalse,                           // AddToRecentFiles
                    varNull,                            // PasswordDocument
                    varNull,                            // PasswordTemplate
                    varTrue,                            // Revert
                    varNull,                            // WritePasswordDocument
                    varNull,                            // WritePasswordTemplate
                    COleVariant(wdOpenFormatWebPages),  // Format
                    COleVariant(msoEncodingAutoDetect), // Encoding
                    varFalse,                           // Visible
                    varFalse,                           // OpenAndRepair
                    varZero,                            // DocumentDirection
                    varTrue,                            // NoEncodingDialog
                    varNull);                           // XMLTransform


if (lpd == NULL)
    return FALSE;

_Document doc(lpd);

doc.SaveAs(COleVariant(sRtfFilename),               // FileName
                COleVariant(wdSaveFormatRTF),       // FileFormat
                varFalse,                           // LockComments
                varNull,                            // Password
                varFalse,                           // AddToRecentFiles
                varNull,                            // WritePassword
                varFalse,                           // ReadOnlyRecommended
                varFalse,                           // EmbedTrueTypeFonts
                varFalse,                           // SaveNativePictureFormat
                varFalse,                           // SaveFormsData
                varFalse,                           // SaveAsAOCELetter
                COleVariant(msoEncodingAutoDetect), // Encoding
                varFalse,                           // InsertLineBreaks
                varFalse,                           // AllowSubstitutions
                varOne,                             // LineEnding
                varFalse);                          // AddBiDiMarks

我的问题是:自动化模型中是否还有其他功能会强制Word将图像嵌入到RTF中?

注意:我目前正在尝试通过后处理RTF来手动嵌入图像,但这是一个不优雅且可能很脆弱的解决方案。

2 个答案:

答案 0 :(得分:3)

我最近遇到了同样的问题并设法解决了这个问题:

procedure TMWnd.UnlinkImages(ADoc: Variant);
var
    fCnt, sCnt, iCnt, i  : Cardinal;
    field, shape, ishape  : Variant;
begin
    fCnt := ADoc.Fields.Count;
    for i:=1 to fCnt do begin
        field := ADoc.Fields.Item(i);
        if VarIsClear(field)or VarIsClear(field.LinkFormat) then
            Continue;
        if field.Type <> wdFieldIncludePicture then
            Continue;
        field.LinkFormat.SavePictureWithDocument := True;
        field.LinkFormat.BreakLink;
    end;


    sCnt := ADoc.Shapes.Count;
    for i:=1 to sCnt do begin
        shape := ADoc.Shapes.Item(i);
        if VarIsNull(shape)or VarIsClear(shape.LinkFormat) then
            continue;
        shape.LinkFormat.SavePictureWithDocument := True;
        shape.LinkFormat.BreakLink;
    end;

    iCnt := ADoc.InlineShapes.Count;
    for i:=1 to iCnt do begin
        ishape := ADoc.InlineShapes.Item(i);
        if VarIsClear(ishape)or VarIsClear(ishape.LinkFormat) then
            Continue;
        ishape.LinkFormat.SavePictureWithDocument := True;
        ishape.LinkFormat.BreakLink;
    end;
end;

这是Delphi代码,但它可以很容易地适应任何语言,因为它是简单的OLE自动化方法调用。

答案 1 :(得分:2)

我需要将其翻译为VBscript。 也许这对某人有用。

const wdFormatRTF=6
Set Word = CreateObject("Word.Application")
Word.Documents.Open("fullpathtomyfile.htm")
set ADoc=Word.ActiveDocument

fCnt = ADoc.Fields.Count
for Each field In ADoc.Fields
    field.LinkFormat.SavePictureWithDocument = True
    field.LinkFormat.BreakLink
next


 For Each shape In ADoc.Shapes 
    shape.LinkFormat.SavePictureWithDocument = True
    shape.LinkFormat.BreakLink
next

For Each iShape In ADoc.InlineShapes 
    ishape.LinkFormat.SavePictureWithDocument = True
    ishape.LinkFormat.BreakLink
Next

Word.ActiveDocument.SaveAs2 "fullpathtomyfile.rtf", wdFormatRTF
Word.Documents.Close
Word.Quit

祝你好运, 米甲

相关问题