Consulting

Results 1 to 7 of 7

Thread: Solved: How To UNDO a InsertFile with VBA

  1. #1
    VBAX Regular
    Joined
    Oct 2005
    Posts
    6
    Location

    Solved: How To UNDO a InsertFile with VBA

    I am trying to create a userform in Word. Using 4 option buttons to insert a file. I am trying to handle a case of a user selecting an option, then selecting a new option by "undoing" the insert file and inserting a new file. Please see code below and make any recomendations.
    [VBA] Option Explicit
    Public Number As Integer
    Private Sub optCancel_Click()
    Const wdStory = 6
    Const wdMove = 0
    If Number <> 0 Then
    Me.Undo (1)
    End If
    Selection.EndKey wdStory, wdMove
    Selection.InsertFile ("F:\Risk_Sys\EZT\Templates\Test\Cancel.dot")
    Number = 1
    End Sub
    Private Sub optMod_Click()
    Const wdStory = 6
    Const wdMove = 0
    If Number <> 0 Then
    Me.Undo (1)
    End If
    Selection.EndKey wdStory, wdMove
    Selection.InsertFile ("F:\Risk_Sys\EZT\Templates\Test\Mod.dot")
    Number = 1
    End Sub
    Private Sub optNew_Click()
    Const wdStory = 6
    Const wdMove = 0
    If Number <> 0 Then
    Me.Undo (3)
    End If
    Selection.EndKey wdStory, wdMove
    Selection.InsertFile ("F:\Risk_Sys\EZT\Templates\Test\New.dot")
    Number = 1
    End Sub
    Private Sub optODR_Click()
    Const wdStory = 6
    Const wdMove = 0
    If Number <> 0 Then
    Me.Undo (1)
    End If
    Selection.EndKey wdStory, wdMove
    Selection.InsertFile ("F:\Risk_Sys\EZT\Templates\Test\ODR.dot")
    Number = 1
    End Sub
    [/VBA]

    I Have also tried the following and it did not work.
    [VBA]
    If Number <> 0 Then
    SendKeys ^(Z)
    End If
    [/VBA]

    Maybe there is another way going about it.
    Thanks in Advance, Dan

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    What version are you using? I can not get as Me.Undo. I get Me.UndoAction.

    Constants can, and should, be declared publicly. Why are you declaring them in each procedure?

    State again what you are trying to do? If you have an instruction that actually inserts the file...and it has done so, are you saying that Undo will take the inserted text out? I don't think so. You could certainly write code that would allow this to happen (make the inserted file a range, thereby allowing its explicit deletion). However, if I understand correctly, Undo applies to the Userform itself. An instruction that affects the document - not the userform - will not be undone by Undo.

  3. #3
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Aqua,

    Welcome to VBAX!

    In your Userform, Me refers to the userform when what you are trying to undo is the action on the document. Try using[vba]ActiveDocument.Undo[/vba]instead.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  4. #4
    VBAX Regular
    Joined
    Oct 2005
    Posts
    6
    Location
    Gerry - please see below:
    1. What version are you using? AQUA?MSO 2000

    2. I can not get as Me.Undo. I get Me.UndoAction.
    Constants can, and should, be declared publicly. Why are you declaring them in each procedure? AQUA
    ?No reason.


    3. State again what you are trying to do? AQUA?I have 4 option buttons in a doc. And the thought is that each time one of the option buttons are selected a file will be inserted into the current document. This part work fine. The problem is that if the EU selects an option button initially, then selects a different option button, I would like the inserted document to be removed and replaced with the newly selected doc.

    4. If you have an instruction that actually inserts the file...and it has done so, are you saying that Undo will take the inserted text out? I don't think so. You could certainly write code that would allow this to happen (make the inserted file a range, thereby allowing its explicit deletion). However, if I understand correctly, Undo applies to the Userform itself. An instruction that affects the document - not the userform - will not be undone by Undo. AQUA?When I stated ?userform? in my original post, I mean that I am trying to create a document that acts like a form?? I hope that makes sense?


    Tony - I tried to use ?activedocument? in lieu of ?me?, but had the same result. The odd thing is that the insertfile is an undoable action in Word2000 when initiated with VBA by using ?ctr + Z? manually.

    Thanks, Dan

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Dan,

    That is not a UserForm! Your Option Buttons are ActiveX objects within your document. Your original "Me.Undo" was OK, except ....

    I don't fully understand this, but at the time it runs, the Undo command is unavailable. It comes available again after doing something with the Selection, so if you re-arrange your lines of code it works...[vba]Private Sub optCancel_Click()
    Const wdStory = 6
    Const wdMove = 0
    Selection.EndKey wdStory, wdMove
    If Number <> 0 Then
    Me.Undo (1)
    End If
    Selection.InsertFile ("F:\Risk_Sys\EZT\Templates\Test\Cancel.dot")
    Number = 1
    End Sub[/vba]

    (and similarly for the other routines)
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    VBAX Regular
    Joined
    Oct 2005
    Posts
    6
    Location
    Thanks for your help Tony. I appreciate it as I was banging my head against the wall on this one. - Dan

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    They are not Option buttons. They appear to be command buttons. Which are decidely NOT option buttons, which is also where I was confused.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •