none
manipulating range outside of word document RRS feed

  • Question

  • Hi everyone,

    I have been working on a decentralized system that passes on metadata of content in word documents without relying on a central software. I am simply attaching the metadata coded in XML onto content that gets copied and extract it when pasted, by using re purposed copy/paste commands and fetching the selected range and adapting it. This works quite well (with the exception of Drag&Drop).

    The problem I have is the following: this method fails when working with read only documents. Is there a way to duplicate a range object so it can be manipulated (and finally copied into the clipboard) without applying the changes to the source document?

    As always, thanks for any help given, it is greatly appreciated.


    • Edited by Rinmic Wednesday, April 24, 2013 9:24 AM
    • Moved by Cindy Meister MVPModerator Wednesday, April 24, 2013 11:12 AM Word-specific, not using the Open XML SDK
    Wednesday, April 24, 2013 12:39 AM

Answers

All replies

  • Hi Rinmic

    You've posted this in the Open XML SDK forum, which targets using a special library to work with closed Office documents. But I get the impression that you work with the Word APIs? So should we move this to the Word for Developers forum?

    Note that more information would be required:

    1. Version of Word

    2. Explain your method in more detail

    3. Explain in detail how it "doesn't work" in read-only documents and exactly what you mean by read-only documents.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, April 24, 2013 10:43 AM
    Moderator
  • Hi Cindy,

    yes sorry, totally wrong forum.

    1. Office 2010

    2. I use the following code:

                Word.Range rng = Globals.ThisAddIn.Application.Selection.Range;
                rng.Calculate();
                int end = rng.End;
                int start = rng.Start;
                Word.Range rngCopy = rng.Duplicate;
                object oCollapseStart = Word.WdCollapseDirection.wdCollapseStart;
                rng.Collapse(ref oCollapseStart);
                string copyddna = ddna.AdjustRangeForCopy(start, end);
                try
                {
                    if (!copyddna.Equals("")) { rng.Text = rng.Text + "<DDNAS>" + copyddna + "</DDNAS>"; }
                    rngCopy.Start = rng.Start;
                    rngCopy.Copy();
                    rng.Delete();
                }
                catch { rngCopy.Copy(); }

    The ddna string is the xml that I attach to copied content. The problem with this code is that it includes the text into the range in the document before copying it, which is not possible if the document is read only. So my question would be if I can take the range "outside" of the document and do the processing there and then copy it.

    3. Read only means: editing the document is not allowed, e.g. it was just downloaded from an email attachment or was marked as finished and only readable from inside word.

    Thanks alot!



    • Edited by Rinmic Wednesday, April 24, 2013 10:59 AM
    Wednesday, April 24, 2013 10:49 AM
  • Hi Rinmic

    If you need to work only with the text and not any formatting or objects then why not:

    try
               
    {
                    string copyText = rng.Text

                   
    if (!copyddna.Equals("")) {copyText += "<DDNAS>" + copyddna + "</DDNAS>"; }
                    //rngCopy
    .Start = rng.Start;
                    //rngCopy
    .Copy();
                    //rng
    .Delete();
               
    }


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, April 24, 2013 11:17 AM
    Moderator
  • Hi Cindy,

    the metadata is text, but the ranges I work with can be anything included in a word document. So if I manipulate the text of a range outside the range using a string, I would still need to refill that text into the range before copying so I could copy the correct range. This would again not work if the document is read only, or am I overlooking something?

    thanks for your help!


    • Edited by Rinmic Wednesday, April 24, 2013 7:58 PM
    Wednesday, April 24, 2013 7:55 PM
  • Hi Rinmic

    Ah, I wasn't following that you're manipulating only PART of the text in the Range.

    In that case, I'd probably create a new document (doesn't have to be Visible, there's a parameter in the Open method that you can set to false). The use Content.FormattedTExt = CopyRange.FormattedText to bring the original range into that document. Do the manipulation there, then copy that to the target location.

    OR, if you feel up to more work, you should be able to extract the WordOpenXML for the entire range, manipulate the XML string in-memory to add the meta-data, then pass that directly to the target using the InsertXML method. This would really be the more "correct" way than making changes directly in the source document. Note that I'm assuming Word 2007+ file format, not Word 97-2003.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, April 25, 2013 8:33 AM
    Moderator
  • Hi Cindy,

    yes I was contemplating using that option. However, I was under the impression that FormattedText would only grab all the text from the range, ignoring other content like pictures. I think I would need to use the ExportFragment method to get all the content. I could then manipulate that range in the new invisible document and copy it from there. I guess I was hoping there is a more correct//elegant solution.

    For your second solution: If I have successfully manipulated the XML string in memory, could I still put that into the clipboard? I do not know the target document and since I am aiming for a decentralized approach. This means I do not intend to keep track of copied content snippets until they are saved (therefore the whole attaching metadata directly to the content method :)).

    Thanks for all the help!

    Rinmic

    Thursday, April 25, 2013 8:51 AM
  • Option 1: IF the objects are anchored in that range then they should copy with the text. With anything formatted "inline with text" that shouldn't be an issue, it would only be "floating" objects. But those won't be picked up using the Copy method, either...

    Option 2: Quite honestly, I don't know what Word puts on the Clipboard, these days, when you copy content in the WordOpenXML file format. You'd have to inspect that and see if it's the same (or similar) to the XML you have in memory.

    But if you're not pasting back into Word, then you may want another format, such as HTML, anyway? Which would bring up another possibility: copy first to the Clipboard, then manipulate the content while it's on the Clipboard. If you're just adding these tags (plain text) that should be possible?

    Another possibility would be to combine Option 1 and 2: Pick up the WordOpenXML, manipulate it, then write it to a second, invisible document and copy that to the Clipboard.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, April 25, 2013 8:58 AM
    Moderator
  • Hi Cindy,

    adding to the content INSIDE the clipboard was actually my first approach when I started developing my AddIn. Sadly, I was never able to find any description of a clipboard API that I could use to do so. Would you able to point me to a documentation for that, it would be greatly appreciated.

    I expect my content to be pasted back into word, but I want to allow for other instances in between, like email or instant messenger. The added text signature therefore ensures that the target document can include this metadata as long as the addin is installed, without relying on any central source or online server. I cannot go into to much detail since this is research in progress (I would however be happy to discuss more details via email).

    Thanks for your pointers, I think if the clipboard options fails (again), I will use the invisible document option.

    rinmic

    Thursday, April 25, 2013 9:07 AM
  • Hi Rinmic

    I don't really know all that much about the Clipboard, but I do read messages where people seem to be able to do that kind of thing, using the Windows API. You might find this message useful, as a starting point:

    http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/ee9e0d28-0f1e-467f-8d1d-1a86b2db2878


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Rinmic Thursday, April 25, 2013 9:31 AM
    Thursday, April 25, 2013 9:26 AM
    Moderator
  • Hi Cindy,

    thank you very much, I will follow up on that.

    Thanks,

    rinmic

    Thursday, April 25, 2013 9:31 AM