How to Pause a Word Macro

Received the tip to type "stop" in my Word macro code if I wanted to pause and perform some final editing before continuing to insert pictures, etc., and then saving to a pdf document.  Only problem is that it brings up the macro editor which would throw off my users.  Tried a MssBox, but that does not let me edit the Word doc until you click OK, which then unpauses the Macro.  Any ideas?
LVL 2
rvfowler2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nffvrxqgrcfqvvcCommented:
You should use SetWaitableTimer() here is example

How To Use SetWaitableTimer With Visual Basic
http://support.microsoft.com/kb/231298 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PandaPantsCommented:
Here's a thought, continuing the idea presented in your previous question. There are two macros, one of which ends by creating a toolbar with a button in your document; when the user clicks that button, it launches a second macro ("Macro2"). The way I have it set up, that macro finishes by deleting the toolbar from the document.
Public Sub AddBreakAndContinueWithNewMacro()
    MsgBox "This is the first macro. When you click OK, it will end and display a toolbar over your document. Click the button on that toolbar to start the next macro.", vbOKOnly
    ActiveDocument.CommandBars.Add Name:="ClickMe"
    Set myControl = ActiveDocument.CommandBars("ClickMe").Controls.Add(Type:=msoControlButton)
    With myControl
        .Caption = "Click to start Macro2"
        .FaceId = 4 ' Printer icon
        .OnAction = "Macro2"
    End With
    ActiveDocument.CommandBars("ClickMe").Visible = True
    
End Sub
Public Sub Macro2()
    MsgBox "This is Macro2"
    ActiveDocument.CommandBars("ClickMe").Delete
End Sub

Open in new window

0
rvfowler2Author Commented:
Both excellent answers.  The disadvantage of stopping the macro is one has to redefine all variables in the second macro.  Yet, the advantage of that is that these become standalone macros so that I can, for example, print a pdf, or insert my picture, or insert my final excel attachment into my Word document as a separate action from my main macro that creates the basic Word doc.  Thanks.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

rvfowler2Author Commented:
Actually, an addendum.  On another question, I did receive a solution to losing the content of the variables when the first macro ends.  The answer suggested storing the content of the variables in the Custom Document Properties of the Word doc before the first macro finished so they can be reused for the seconde macro.  See below.


Here's the deal with custom document properties, as used in the code sample earlier in this thread. We want to create a place in the document to keep information that will remain there even after the macros have been run. I like to use custom document properties (CDPs) for this purpose because they don't appear in the document text anywhere (unless you want them to) and so are not prone to accidental erasure.

Creating a CDP is easy, with a simple Add to the CustomDocumentProperties collection:
ActiveDocument.CustomDocumentProperties.Add _
            Name:=sMyCDP, LinkToContent:=False, Value:="upc_setup_r" & MyValue & PropValue & "test.doc", _
            Type:=msoPropertyTypeString

Parameters:
* The Name parameter can be anything (no reason not to hardcode it, if you're only going to be working with the one CDP).
* LinkToContent ties it to a value in the document, which I never do. I set it to False based on a choice I made in the late 90s, but you can probably just ignore it. (I don't remember anymore why I did that.)
* Value is what it says it is.
* Type is where you declare it as a string (you can also choose dates, numbers, etc.).

So, that much is easy. I've run into problems, though, trying to Add a CDP that already exists, so I've gotten into the habit of always checking whether it exists, first. I do this by cycling through all the CDPs in the collection and comparing each name to the one I want to Add (or Update):

    Dim xx As DocumentProperty
    For Each xx In ActiveDocument.CustomDocumentProperties
        If LCase(xx.Name) = LCase(sMyCDP) Then
            bCDPFound = True
        End If ' xx.Name <> CdpName
    Next xx

As before, you can hardcode the name if you prefer (I'm using the string variable sMyCDP in the example). There's also no particular reason to use a boolean to track whether the CDP was found; that's another bit of legacy code that follows me around. To ditch it, you can replace the "bCDPFound = True" statement with the code that sets the value of an existing CDP:

ActiveDocument.CustomDocumentProperties(sMyCDP) = "upc_setup_r" & MyValue & PropValue & "test.doc"

In that case (skipping the boolean), you'd want to put the CDP creation code in the Else side of the If..Then..Else..End If section. Here's the whole thing in that format:

   Dim xx As DocumentProperty
    For Each xx In ActiveDocument.CustomDocumentProperties
        If LCase(xx.Name) = LCase(sMyCDP) Then
            ' Populate the CDP with the current name:
            ActiveDocument.CustomDocumentProperties(sMyCDP) = "upc_setup_r" & MyValue & PropValue & "test.doc"
        Else
           ' Didn't find one, so Add it and populate it
            ActiveDocument.CustomDocumentProperties.Add _
                Name:=sMyCDP, LinkToContent:=False, Value:="upc_setup_r" & MyValue & PropValue & "test.doc", _
                Type:=msoPropertyTypeString
       End If
    Next xx
   

Of course, you can use a variable for the Value parameter, such as sFileName...but you know all that.

The code snippet I've attached is a routine I use to create/add a CDP and give it a value. You'll pass in the CDP name and the value, and it does the rest. Since it refers to yet another routine (CdpExists), I've included that, too. Finally, I'm throwing in the one I use to create a CDP with a numerical value instead of a string (I didn't even remember I had this, so I can't say why I chose Currency).
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:      '--------------
Public Sub CreateAndPopulateOneCDP(ByVal psCdpName As String, ByVal psCdpValue As String)
        If CdpExists(psCdpName) Then
            ActiveDocument.CustomDocumentProperties(psCdpName) = psCdpValue
        Else ' CdpExists(psCdpName) = False
            ActiveDocument.CustomDocumentProperties.Add _
                Name:=psCdpName, LinkToContent:=False, Value:=psCdpValue, _
                Type:=msoPropertyTypeString
        End If ' CdpExists(psCdpName)
End Sub ' CreateAndPopulateOneCDP(ByVal psCdpName As String, ByVal psCdpValue As String)
'--------------
Public Function CdpExists(ByVal CdpName As String) As Boolean
Dim xx As DocumentProperty
    For Each xx In ActiveDocument.CustomDocumentProperties
            If LCase(xx.Name) = LCase(CdpName) Then
                CdpExists = True
                Exit Function
            End If ' xx.Name <> CdpName
    Next xx
    CdpExists = False
End Function
'--------------
Public Sub CreateAndPopulateOneCDP_Number(ByVal psCdpName As String, ByVal pcurCdpValue As Currency)
        If mxcCdpExists(psCdpName) Then
            ActiveDocument.CustomDocumentProperties(psCdpName) = psCdpValue
        Else ' CdpExists(psCdpName) = False
            ActiveDocument.CustomDocumentProperties.Add _
                Name:=psCdpName, LinkToContent:=False, Value:=pcurCdpValue, _
                Type:=msoPropertyTypeNumber
        End If ' CdpExists(psCdpName)
End Sub ' CreateAndPopulateOneCDP_Number(ByVal psCdpName As String, ByVal pcurCdpValue As Currency)
'--------------_
0
rvfowler2Author Commented:
If you are still linked to this.  Having problems with the CDP code.  Received a "catastrophic failure.
0
PandaPantsCommented:
Ouch. That doesn't sound good. I hope no one was injured. Can you tell at what point the code errored out?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.