none
MS Word via the COM-object interface to add an image to the substrate / watermark RRS feed

  • Question

  • Hello!

    Sorry for the text – this is a translation …

    How can I use the COM interface of the MS Word class to set the "Substrate/Background" "Confidential"?

    In many forums, the examples use the example of adding to the header:

    $doc = $word.Documents.Open("doc.docx")
    $doc.Sections[1].Headers[1].Shapes.AddPicture("sec.png"

    But I want to add the background to the body of the entire document. Similar to Excel, which has a function:

    $worksheet.SetBackgroundPicture("sec.png")

    If I do in Word

    $doc.Background.Fill.UserPicture("sec.png")
    And, if I save the document, open it, and only turn on the web view mode, then watermark will be displayed. This Almost what I need ...

    • Edited by isKUL Wednesday, February 19, 2020 11:57 PM
    Wednesday, February 19, 2020 11:55 PM

Answers

  • Well, I have said it already - you can’t. I think you are expecting Word to have the same feature as Excel, but it doesn’t. So you have to choose either background or watermarks, or both, and each choice has consequences as I have mentioned. If you choose watermarks, you you should in essence apply them immediately before printing because if the document changes, some parts of it may no longer have watermarks, and so on.

    BTW you should really link to this question on Stack Overflow, and co-ordinate efforts, otherwise you waste time (not just yours!)


    Peter Jamieson

    • Marked as answer by isKUL Monday, February 24, 2020 1:07 PM
    Friday, February 21, 2020 1:05 AM

All replies

  • If "almost what I need" means that you want to see the watermark with other views in the desktop version of Word, e.g. Print Layout, then, sorry, but the Background.Fill.UserPicture approach just does not work.

    Word has always distinguished between a "Background fill", which is web-only and a "Watermark", which is for printing and can be seen in Print Layout *but not* Web Layout. 

    You can specify that you want a document's *Background fill* to *print* by setting the Application's .Options.PrintBackgrounds to True. (not .PrintBackground - that does something else). However, since that option is for the application, you can't set it for an individual document and make it "sticky" for that document.

    If you *export* to PDF, both the background fill and the watermark appear in the PDF.  That does not appear to depend on the value of .Options.PrintBackgrounds. So if you are really using Word to create some content but what you really want is something to display, that is one possible way forward. (NB, if you *print* to PDF, you don't get to see the background unless you set .Options.Backgrounds to True)

    To insert a Watermark that appears in Print Layout, you *have to* use a method that places the watermark behind the page. For a document with one section and only one type of header (or footer), you can do that by adding an image to that header. If your document may have multiple sections with different headers, or if a section has multiple types of header (e.g. odd page and even page headers) then you have to add the watermark separately to each of those headers.

    If you switch on the VBA recorder while adding a watermark, you should see that that is what Word records when you insert a watermark from an external file or a watermark that Word creates based on a piece of text you provide in the "Custom Watermark" dialog box. If you use one of the predefined watermarks, Word uses a different mechanism (it gets the watermark from the Building Blocks and uses a Building Block method to insert it. Although the result looks the same, Word only seems to add that Block to *one* section which isn't what you want.

    Further, Word does not automatically resize Watermark images, does not add them to newly inserted sections, and so on. In fact it is rather difficult to see why Word's designers chose to make it easy to add a background for display on the web and so hard to make pretty much the same thing work for viewing.

    When you use online (web-based) or device versions of Word, things are a little different. 

    In the web-based version, in the Edit mode, you do not see either background fills or watermarks. However, if you switch to reading mode, you see them both.

    In the iPad and Android version, it seems to be the other way around - in the Edit mode, you see both, and in the Reading mode, you don't see either.


    Peter Jamieson

    Thursday, February 20, 2020 3:22 PM
  • Thank you very much for your excellent explanation! But I must have confused you ... I can't figure out how to access the global background management of the entire document via COM? That is, to set the background (which will be displayed on all pages) in the document and not to spoil the content and headers and footers. At the same time, the background is displayed in all editing modes and when printing. I repeat-in Excel there is a great function for this, but for some reason I did not see it in Word.

    Due to limitations, I can't show you an example image ...

    stackoverflow.com/questions/60295653/ms-word-via-the-com-object-interface-to-add-an-image-to-the-substrate-watermar

    Friday, February 21, 2020 12:31 AM
  • Well, I have said it already - you can’t. I think you are expecting Word to have the same feature as Excel, but it doesn’t. So you have to choose either background or watermarks, or both, and each choice has consequences as I have mentioned. If you choose watermarks, you you should in essence apply them immediately before printing because if the document changes, some parts of it may no longer have watermarks, and so on.

    BTW you should really link to this question on Stack Overflow, and co-ordinate efforts, otherwise you waste time (not just yours!)


    Peter Jamieson

    • Marked as answer by isKUL Monday, February 24, 2020 1:07 PM
    Friday, February 21, 2020 1:05 AM
  • Well, I have said it already - you can’t. I think you are expecting Word to have the same feature as Excel, but it doesn’t.

    Do you think so, or is it accurate?! Excel and Word are products of the same package, but have an ideologically different approach in the COM interface architecture, strange ...
    Sunday, February 23, 2020 1:30 PM
  • Yes, they are in the same package, but Excel and Word came from very different origins. Some things are the same, or look the same, and other things aren't. 

    I would say that the Office integration mainly occurs because several Office apps support COM Automation and OLE linking and embedding, some shared features such as some of the drawing and charting facilities, and some of them also use use the same file format framework (OLE Structured Storage in the .doc, .xls, .ppt era, and Zipped XML-oriented Packages in the .docx, .xlsx, .pptx era).

    Although there are some signs that Word implemented ideas introduced by the Excel developers, e.g. the Range object, there are also many differences. Some seem fairly arbitrary, but others obviously exist partly because they do different things and use different objects to do that. For example, AFAIK you don't really get the same control over Excel "printouts" (where it basically has to split a worksheet up into tiles and print each one) and Word Printouts where you can specify sections that have different page sizes, orientations etc. 

    Despite all that, I can't say I  understand why Word seems to favour the "background" approach to watermarks for web view/output and does not also allow it to be used for regular "print view" display. While the other "watermark" approach is arguably more flexible (lets you do different watermarks on different pages and so on) it *is* harder to manage for the reasons we have seen.  The background approach isn't ideal for printing because it doesn't really take account of print margins, but I wouldn't have thought it would be too hard to have a couple of properties in the document that said "consider the background to be a regular watermark" or some such. 

    Although it doesn't help solve your immediate problem, perhaps worth having a look at word.uservoice.com for existing questions in this area and either supporting one of those or adding your own.


    Peter Jamieson


    Sunday, February 23, 2020 5:34 PM
  • Thank you for your detailed answers! And thank you for your time! :)
    Monday, February 24, 2020 1:09 PM