PDA

View Full Version : switching documents and continuing macro??



Kilroy
07-29-2016, 04:26 AM
I have a series of macros that I run on a document one of them creates a new document (Thanks Greg). I then have to switch to the new document and run a second series of macros. Is there a way to use one macro that runs on the first one then switches to the newly created document and continues with the second series?

gmaxey
07-29-2016, 06:01 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oDoc As Document
Dim oDoc2 As Document
Set oDoc = ActiveDocument
MsgBox "Do stuff with oDoc"
Set oDoc2 = Documents.Add
MsgBox "Do stuff with oDoc2"
lbl_Exit:
Exit Sub
End Sub

Kilroy
07-29-2016, 07:00 AM
Thanks Greg. I added this between the first and second macros. The windows pop up and a third document opens blank.

gmaxey
07-29-2016, 07:15 AM
You added what between the first and second macros? What first and second macro?

I am shooting in the dark here. Unless your code is a state secret then post it with an explanation of what it is that you are trying to achieve!

Kilroy
07-29-2016, 07:26 AM
No state secret all basic stuff I have about 16 macros running (around 17 pages of code now). The first one you wrote that takes my document and populates a table in a new document and then I run the other 15 macros on the newly created document.

Running two series of macros isn't a huge issue but ideally I would like to figure out how to do it in one go. Enter all of the macros in the first master document like I do with the table maker, and then watch as it compiles the table in the new document and then runs the rest of the macros.

gmaxey
07-29-2016, 07:41 AM
Without seeing what you are doing a terrible job of explaining, it seem that you have already been given that fish.

Here it is again:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oDoc As Document
Dim oDoc2 As Document

Set oDoc = ActiveDocument
MsgBox "I'm the original document"
Set oDoc2 = Documents.Add
oDoc2.Tables.Add oDoc2.Range, 2, 2
MsgBox "I'm the document with a table created using Greg's code."
oDoc2.Tables(1).Cell(1, 1).Range.Text = "I'm a cell in the new document getting worked over by this code."
lbl_Exit:
Exit Sub
End Sub