none
ASP.Net Word Header not expanding for Image RRS feed

  • Question

  • I am attempting to insert(add) an .gif image to a Word template header. I am successfully getting the image in the header but the header doesn't expand so the first line of the main document is at the top of the page and overlays the image in the header.  Any assistance is appreciated!

    Here's my code;

    // Go to header/footer and insert image
    public void GotoHeaderFooter(string path)
    {
    
        object oTrue = true;
        object oFalse = false;
        object oMissing = System.Reflection.Missing.Value;
        object novalue = System.Reflection.Missing.Value;
        object missing = System.Reflection.Missing.Value;
                
    
        //adding header
        string logoPath = path + "image.gif";
        Microsoft.Office.Interop.Word.Shape logoCustom = null;
        oWordApplic.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
                
        logoCustom = oWordApplic.Selection.HeaderFooter.Shapes.AddPicture(logoPath, ref oFalse, ref oTrue);
        logoCustom.Select(ref oMissing);
        logoCustom.Left = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;
        logoCustom.Top = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;
        logoCustom.WrapFormat.AllowOverlap = -1;
        logoCustom.WrapFormat.Side = Microsoft.Office.Interop.Word.WdWrapSideType.wdWrapBoth;
        logoCustom.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapNone;
     
        oWordApplic.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
    }
    
    

    Friday, February 20, 2015 5:20 PM

Answers

  • Hello Johson,

    The Considerations for server-side Automation of Office page states the following:

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

    Consider using Open XML SDK or any other components designed for server-side execution.

    • Marked as answer by L.HlModerator Saturday, February 28, 2015 2:06 PM
    Friday, February 20, 2015 5:50 PM
  • Hi Johnson

    I'm going to move your question to the Word for Developers forum as the ASP.NET technology doesn't work with VSTO.

    You should seriously consider Eugene's remarks, however...

    1. You should NOT be using Selection and SeekView in order to manipulate a Word document's Header / Footer. It's extremely unreliable in any document that contains more than one header or footer. Assign the desired Header/Footer to a RANGE object and use that, instead. Also, you should use an explicit document object and not ActiveDocument or Activewindow, they simply are NOT reliable. Example (VB pseudocode):
      Dim wdDoc as Word.Document = oWordApplic.ActiveDocument
      Dim rngHd as Word.Range = wdDoc.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range
      logoCustom = rngHd.Shapes.AddPicture( 'and so on and so forth)

    2. I do not see anywhere in the code you show us where you explicit set the WrapType. If you do not specify, Word will take the default for the user profile, which could be anything. In this case, apparently "behind the text" or possibly "through". You should use wdWrapSquare or wdWrapTopBottom or wdWrapInline.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by L.HlModerator Saturday, February 28, 2015 2:06 PM
    Saturday, February 21, 2015 8:37 PM
    Moderator

All replies

  • Hello Johson,

    The Considerations for server-side Automation of Office page states the following:

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

    Consider using Open XML SDK or any other components designed for server-side execution.

    • Marked as answer by L.HlModerator Saturday, February 28, 2015 2:06 PM
    Friday, February 20, 2015 5:50 PM
  • Hi Johnson

    I'm going to move your question to the Word for Developers forum as the ASP.NET technology doesn't work with VSTO.

    You should seriously consider Eugene's remarks, however...

    1. You should NOT be using Selection and SeekView in order to manipulate a Word document's Header / Footer. It's extremely unreliable in any document that contains more than one header or footer. Assign the desired Header/Footer to a RANGE object and use that, instead. Also, you should use an explicit document object and not ActiveDocument or Activewindow, they simply are NOT reliable. Example (VB pseudocode):
      Dim wdDoc as Word.Document = oWordApplic.ActiveDocument
      Dim rngHd as Word.Range = wdDoc.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range
      logoCustom = rngHd.Shapes.AddPicture( 'and so on and so forth)

    2. I do not see anywhere in the code you show us where you explicit set the WrapType. If you do not specify, Word will take the default for the user profile, which could be anything. In this case, apparently "behind the text" or possibly "through". You should use wdWrapSquare or wdWrapTopBottom or wdWrapInline.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by L.HlModerator Saturday, February 28, 2015 2:06 PM
    Saturday, February 21, 2015 8:37 PM
    Moderator
  • Thank you for the insight Eugene.  I do realize the shortcomings of unattended execution particularly on a server with other apps running. This is good information and thanks again for taking the time to respond.
    Monday, March 2, 2015 1:03 PM
  • Thanks Cindy.  Your response along with Eugene's above is very helpful. 
    Monday, March 2, 2015 1:05 PM