• VBA style delete always deletes from active doc (2003)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » VBA style delete always deletes from active doc (2003)

    Author
    Topic
    #447017

    I have a style-cleanup routine that instead of pasting directly into my doc, it pastes in a newly-created doc, deletes some styles specified by the user, maps any text in “Normal” to a different style (which handles the styles just deleted), and then copies and pastes whats left into my doc.

    so it’s something like:

    set oDoc = Documents.Add(Visible:=False)
    oDoc.Content.Paste
    oDoc.Styles(“Bogus”).Delete

    At this point, the doc I copied from has Styles(“Bogus”) now gone!!!

    I’ve checked very carefully to ensure that it’s really the “quarantine” doc I’m operating on, but the style stays there, and deletes from the active document.

    This is frustrating, dangerous, and just, plain, wrong.
    Any clues or ideas? Do I have to have an active doc to delete styles from it?

    Viewing 3 reply threads
    Author
    Replies
    • #1088448

      I speak under correction, but should you not re-activate (or set a reference to) the original doc? If you add a document (even if it is not visible) will this not become the active doc and all remaining actions be applied to that doc.?

      After adding a doc, try re-activating the original and then delete the style.

      Just a though as I am not a guru with word code… smile

    • #1088488

      Looking at your code, I would agree that this is a problem and can understand your frustration.

      Do you get the same result if you make the doc visible? Do you get the same problem if you step through the code (is it speed related)? If you added a save step after the paste does this force the correct action (possibly by allowing enough time to elapse so that Word doesn’t get so confused)?

    • #1088499

      I did some more experimenting, and doing
      oDoc.Activate
      before deleting the style makes sure that it gets deleted from oDoc.
      And Rudi, yes, I did set a ref to the original active document, and after completion, oOrigDoc.Activate gets me back there.

      I didn’t try making the invisible doc visible, but active did work.

      Still, it’s a bug, a bad bug, but at least i found a way around it.

      • #1088608

        It’s good to know this – it really is a nasty bug!

    • #1088620

      Have you tried using the Organizer to delete the style as an alternative to activating the invisible document? Something like the following might work as expected, though I haven’t tested it:

      Application.OrganizerDelete Source:=oDoc, Name:=”Bogus”, Object:=wdOrganizerObjectStyles

    Viewing 3 reply threads
    Reply To: VBA style delete always deletes from active doc (2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: