none
Word 2007 Addin to add Watermark image saved as .doc file RRS feed

  • Question

  • I currently have an addin that was developed to work in both Word 2007 and Word 2010 with the assumption the addin would be used on .docx files.  However, it is now required to also work on .doc files but still with Word 2007 and Word 2010.  Although the following code works in both versions of Word, it will only work on .docx files and not .doc files.  On the line:

                Dim shape As Shape = doc.InlineShapes.AddPicture(strWatermarkFile, linkToFile, saveWithDocument, headerfooterRange).ConvertToShape()
    

    I get the Error HRESULT E_FAIL has been returned from a call to a COM component.

    My code that works in 2007 on .docx files and not .doc files is:

        Public Shared Function AddWaterMark2007(strWatermarkFile As String)
            Dim app As Application = Globals.ThisAddIn.Application
            Dim doc As Document = Globals.ThisAddIn.Application.ActiveDocument
            Dim headerFooter As HeaderFooter
            Dim hfIndex As WdHeaderFooterIndex = WdHeaderFooterIndex.wdHeaderFooterFirstPage ' WdHeaderFooterIndex.wdHeaderFooterPrimary
            Dim linkToFile As Object = False
            Dim saveWithDocument As Object = True
    
            If doc.Sections(1).Headers IsNot Nothing Then
                headerFooter = doc.Sections(1).Headers(hfIndex)
            ElseIf doc.Sections(1).Footers IsNot Nothing Then
                headerFooter = doc.Sections(1).Footers(hfIndex)
            Else
                headerFooter = Nothing
            End If
    
            If headerFooter IsNot Nothing Then
    
                Dim headerfooterRange As Object = headerFooter.Range
                Dim shape As Shape = doc.InlineShapes.AddPicture(strWatermarkFile, linkToFile, saveWithDocument, headerfooterRange).ConvertToShape()
                
                shape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue
                shape.WrapFormat.AllowOverlap = -1
    
                shape.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin
                shape.Left = WdShapePosition.wdShapeCenter
                shape.ScaleHeight(1, Microsoft.Office.Core.MsoTriState.msoTrue)
    
                shape.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin
                shape.Top = WdShapePosition.wdShapeCenter
                shape.ScaleWidth(1, Microsoft.Office.Core.MsoTriState.msoTrue)
    
                shape.Name = "WordPictureWatermark"
                doc.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True
    
            End If
    
    
            app.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument
            app.ActiveWindow.ActivePane.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView
        End Function
    

    Thanks in advance for any suggestions!

    Sunday, January 11, 2015 4:29 PM

All replies

  • Hi Voyagr,

    I also could reproduce this issue too.

    Since the issue is complex, I'm trying to involve some senior engineers into this issue and it will take some time. Your patience will be greatly appreciated.

    Sorry for any inconvenience and have a nice day!

    Regards & Fei



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 13, 2015 8:59 AM
    Moderator
  • Hi Fei,

    Thank you for looking into this for me.  My temporary fix has been to develop code that saves the doc as a docx temporarily, run the above code, and then convert back to a doc file.  Certainly not the most glamorous but works for now until the senior engineers have some suggestions.

    Thanks!

    Tuesday, January 13, 2015 11:16 AM
  • Hi Voyagr12,

    Thanks for your patience.

    Could you please share the sample code replicating this issue? This helps us to quickly troubleshoot without any assumption.

    Request you to put this sample addin project uploaded in onedrive and share the link here.

    Friday, January 30, 2015 10:40 PM