none
If I want to delete everything in a document... RRS feed

  • Question

  • Hi All;

    I want to delete everything in a document I have open so it's back to the state when you create a new document. Is calling Document.Content.Delete() sufficient? Or do I need to delete other items too?

    thanks - dave


    Very funny video - What's your Metaphor?
    Sunday, March 20, 2011 6:18 PM

Answers

  • Hi all;

    First off, thank you for all the suggestions. You all went way beyond the call of duty.

    Second, I'm giving up on this. The approach I have to clear the document and then addFile() works. Yes there are issues with the document settings but those we can work around. And I don't hink there is a way to add a document to an existing window. TO an exxisting Application yes, but not to an existing window.

    thanks - dave


    Very funny video - What's your Metaphor?
    • Marked as answer by DavidThi808 Sunday, April 10, 2011 4:11 PM
    Sunday, April 10, 2011 4:11 PM

All replies

  • Hi David,

    ActiveDocument.Content.Delete does not delete any macros, the header/footer content or change any page layout settings that might not have existed in the document's template. If you're wanting a new blank document, why not simply create one? You can always save it over the old file if need be.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Sunday, March 20, 2011 9:19 PM
  • Hi;

    Basically I want to load a new document into an existing Word window. Keeping the existing Word window makes the user experience much better. So my approach is to delete everything, then call Range.InsertFile to insert the new document. On an empty document will an InsertFile override the document page layout and/or header/footers?

    Or is there an easier way to do this? Reload won't work because it prompts.

    thanks - dave


    Very funny video - What's your Metaphor?
    Monday, March 21, 2011 3:00 PM
  • Basically I want to load a new document into an existing Word window. Keeping the existing Word window makes the user experience much better.

    Or is there an easier way to do this? Reload won't work because it prompts.

    Hi Dave,

    You don't need to close the exisiting window - you can close the document and load a new one. If you load the new one first, that will minimize the screen flicker, since the old one doesn't need to be in the foreground when it gets closed. I do accept, though, that playing around with an existing document with screenupdating set to false can eliminate the flicker altogether. As for InsertFile, though, I think you'll find that approach doesn't import the source document's page layout.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Tuesday, March 22, 2011 8:41 AM
  • Hi;

    How can I load the new document into the same Word window? I'm trying this but the app creates a new window every way I try.

    thanks - dave


    Very funny video - What's your Metaphor?
    Thursday, March 24, 2011 5:34 PM
  • Hi David,

    You could do something like:

    Sub Test()
    Application.ScreenUpdating = False
    Dim oDoc As Document, StrFile As String
    Set oDoc = ActiveDocument
    StrFile = oDoc.FullName
    Documents.Add
    oDoc.Close savechanges:=False
    On Error Resume Next
    Kill StrFile
    Application.ScreenUpdating = True
    End Sub

    The 'oDoc.Close savechanges:=False' line closes the old document without saving it and the 'Kill StrFile' line deletes the old document if it has previously been saved.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Friday, March 25, 2011 6:19 AM
  • Hi Paul;

    I tried that but it comes up in a new window which I am trying to avoid as I want it to look like the existing document just changed.

    The new document has the same page properties 99.99% of the time so the delete all, insertFile seems to work ok (so far). But I wish there was a way to replace the document in an existing window.

    thanks - dave


    Very funny video - What's your Metaphor?
    Friday, March 25, 2011 3:19 PM
  • Hi David,

    With the demo code I posted, I am unable to replicate the new document being created in a new window. For me, the new document appears in the same window as the old one. What can happen, though, is that, if the old document's window was the top one, the new document will occupy the whole window. You can obviate that with code like:

    Application.Windows.Arrange

    (you may need to do more than just that if your window arrangement is non-standard).


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Friday, March 25, 2011 9:29 PM
  • Hi David,

    Have you got an idea to resolve your problem yet? Does the suggestion work for you or not? If you still show any concern on this post, just feel free to follow up.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 30, 2011 11:03 AM
  • Hi David,

    If Word is already open, it "shouldn't" create a new instance of Word unless your code is issuing a "new" Word applicaiton command.

     


    Regards
    Thursday, March 31, 2011 4:00 AM
  • Hi;

    I tried again. Here's my code:

    Document nativeDoc = ((WordDocument) doc).NativeWordDocument;
    Microsoft.Office.Interop.Word.Application app = nativeDoc.Application;
    string oldFilename = nativeDoc.FullName;
    app.ScreenUpdating = false;
    
    app.Documents.Add(filename);
    nativeDoc.Close(false);
    if (File.Exists(oldFilename))
    	File.Delete(oldFilename);
    app.ScreenUpdating = true;
    
    

    It creates a new window in this case - positioned slightly down/left (as it is supposed to) from where the previous window was.

    Am I not doing this right?

    thanks - dave


    Very funny video - What's your Metaphor?
    Monday, April 4, 2011 4:41 PM
  • Hi David,

    Try closing the existing file first and then adding the new file.

     


    Regards
    Monday, April 4, 2011 5:58 PM
  • That's even worse - then I have the doc disappear, new one appear, then the new one shift.

    thanks - dave


    Very funny video - What's your Metaphor?
    Monday, April 4, 2011 5:59 PM
  • Hi Dave,

    The behavior sounds potentially like the declaring of "app" creates a new version of Word. In VSTO and VB we declare the current application this way.

     

    Dim wdoc As Word = Globals.ThisAddIn.Application

     

    Is there a Globals.ThisApplication statement available?

     

    Or another possibility is to try a With command like …

            With app.ActiveWindow.Document.

     


    Regards
    Monday, April 4, 2011 6:19 PM
  • Hi Dave,

    I wrote this in VBA and hopefully you can translate it into C#. The new document opens in the same window.

    Sub StayInWindow()
        Dim cDoc As Document
        Set cDoc = ActiveDocument
        With ActiveWindow.Application
            cDoc.Close
            Documents.Add
        End With
    End Sub


    Regards
    Monday, April 4, 2011 7:08 PM
  • IN my experiments, the Window also shifts if it is in the restored state - not always, apparently, but if the Window is clipped by the screen, Word certainly seems to try to reposition it.

    So maybe you could modify Paul's code by saving/restoring the left, top, width and height of the ActiveWindow (I think you may need more than I have suggested here to ensure that the Correct Window is the ActiveWindow, etc. etc.), e.g. along the lines of...

    Sub Test()
    Application.ScreenUpdating = False
    Dim oDoc As Document, StrFile As String, l As Long, w As Long, t As Long, h As Long
    Set oDoc = ActiveDocument

    l = ActiveWindow.Left

    w = ActiveWindow.Width

    t = ActiveWindow.Top

    h = ActiveWindow.Height

    StrFile = oDoc.FullName
    Documents.Add
    oDoc.Close savechanges:=False

    On Error Resume Next
    Kill StrFile

    ActiveWindow.Left = l

    ActiveWindow.Width = w

    ActiveWindow.Top = t

    ActiveWindow.Height = h

    Application.ScreenUpdating = True
    End Sub



    Peter Jamieson
    Monday, April 4, 2011 7:16 PM
  • David,

    I have noticed for myself that if I have my taskbar set to seperate every application rather than group them it appears that word starts a new application every document when it actually isn't, just Windows interpretting each Doc as seperate

    Try grouping your Word Docs in the taskbar and see if that gives you something like what you want, so it doesn't appear to be giving a new doc. Also since word 2007 a new doc out of a maximised window cascade opens the new doc in the slightly lowered location. Changing the windows positioning will correct this but not remove the flicker. Having it maximised with obviously stop that cascade effect. Have you tried hiding the app, make your changes and then showing it. Causes a flicker but not a lot.

    Also try. Open up several documents, right-click on the Taskbar and select Cascade: that should toggle off Cascade mode

    As stated above, the best way to reset a documents settings and structures is close the old one and add a new one and use that.

    It may not be a word concern for the aesthetics you're after :)

     

    Tuesday, April 5, 2011 1:05 AM
  • Hi all;

    First off, thank you for all the suggestions. You all went way beyond the call of duty.

    Second, I'm giving up on this. The approach I have to clear the document and then addFile() works. Yes there are issues with the document settings but those we can work around. And I don't hink there is a way to add a document to an existing window. TO an exxisting Application yes, but not to an existing window.

    thanks - dave


    Very funny video - What's your Metaphor?
    • Marked as answer by DavidThi808 Sunday, April 10, 2011 4:11 PM
    Sunday, April 10, 2011 4:11 PM