PDA

View Full Version : Solved: How To UNDO a InsertFile with VBA



Aqua
10-28-2005, 11:27 AM
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.
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


I Have also tried the following and it did not work.

If Number <> 0 Then
SendKeys ^(Z)
End If


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

fumei
10-29-2005, 06:57 AM
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.

TonyJollans
10-29-2005, 04:03 PM
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 usingActiveDocument.Undoinstead.

Aqua
10-31-2005, 08:04 AM
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

TonyJollans
10-31-2005, 04:39 PM
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...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

(and similarly for the other routines)

Aqua
11-01-2005, 07:39 AM
Thanks for your help Tony. I appreciate it as I was banging my head against the wall on this one. - Dan

fumei
11-01-2005, 10:27 AM
They are not Option buttons. They appear to be command buttons. Which are decidely NOT option buttons, which is also where I was confused.