Choose ActiveDocument

R

Richard

Hi

I have a form.doc (A) which I need to e-mail to 3rd parties without the code
to reduce file size.

Code has been written to copy the text from the form to a new document (B),
which I then need to save to enable it to be e-mailed via Outlook.

The original document (A) is still the activeDocument, but to save the new
one (B), (B) needs to be the activeDocument for the code:

ActiveDocument.Save

to work.

So, How do I make (B) the ActiveDocument (Which is not saved) & later make
(A) the activeDocument to perform further tasks?

Templates (.dot) are deliberately not being used as references get lost when
files are moved in a big organisation.

All contributions gratefully received.

Thanks - Richard
 
J

Jay Freedman

There is no need to make any particular document active in order to
save it.

Word VBA has the idea of a Document object, and you can declare a
variable of type Document that can be used regardless of whether it's
"active" (has the focus).

Start out with code like this:

Dim DocA As Document, DocB As Document
Set DocA = ActiveDocument
Set DocB = Documents.Add ' new blank document

' copy text from DocA to DocB as needed

DocB.Save ' <= doesn't matter which document is active

DocA.Activate ' if necessary

' more processing


Also, I think your description is inaccurate in one respect: When you
make a new document, it automatically becomes active, and the document
that was active at the time (DocA) is not active. Your statement "The
original document (A) is still the activeDocument" should not be true.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
G

Greg Maxey

Sub ScratchMaco()
Dim oDocA As Word.Document
Dim oDocB As Word.Document
Set oDocA = ActiveDocument 'Set the active document
Set oDocB = Documents.Add 'Create new document. It becomes active
MsgBox ActiveDocument
oDocA.Activate 'make oDocA active
MsgBox ActiveDocument
oDocB.Activate 'make oDocB active
MsgBox ActiveDocument
oDocB.Close wdDoNotSaveChanges 'close the active document
MsgBox ActiveDocument 'remaining open document becomes active.
End Sub
 
R

Richard

Thank you Jay & Greg

Will put into practice, I think you have cracked it for me!

Many thanks
 
R

Richard

Hi

Many thanks for help. Original question answered & all working.

If one wants to then delete the new document (oDocB), how would you go about
that?
 
D

DaveLett

Hi Richard,
I think you're looking for something like the following:

Dim oDocA As Document
Dim oDocB As Document

Set oDocA = ActiveDocument
Set oDocB = Documents.Add


Then, you can just call each document object as needed. Such as the following:
oDocB.Paragraphs(1).Range.text = oDocA.Paragraphs(4).Range.text
oDocB.Save '''or SaveAs, depending on what you need.

HTH,
Dave
 
G

Greg Maxey

If is just a temporary document that you are using for processing then just
close it without saving:

oDocB.Close wdDoNotSaveChanges
 
R

Richard

Hi

I do seem to be having trouble with these ActiveDocuments, together with
oDocA & oDocB both of which have been declared as Documents.

Code as follows:

Sub NewDocReprimand()
Set DocA = ActiveDocument
On Error Resume Next

Unprotect ‘Subroutime

ActiveDocument.Sections(2).Range.Select ‘Copy text from original Document
Selection.Copy

Protect ‘Subroutine


'Documents.Add DocumentType:=wdNewBlankDocument
Set DocRep = Documents.Add ‘New Document
Selection.PasteAndFormat (wdPasteDefault) ‘Text pasted into new
Document
'DocRep.Activate ‘Attempt to get page setup below to work

With ActiveDocument.PageSetup 'Have tried “With Docrep.Pagesetup
also
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = CentimetersToPoints(1)
.BottomMargin = CentimetersToPoints(1)
.LeftMargin = CentimetersToPoints(2)
.RightMargin = CentimetersToPoints(1)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.27)
.FooterDistance = CentimetersToPoints(0.5)
.PageWidth = CentimetersToPoints(21)
.PageHeight = CentimetersToPoints(29.7)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
End With ‘None of the Page Setup worked


Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend

'delete spaces at end of doc
Selection.Delete

Selection.HomeKey Unit:=wdStory 'Delete Appendix A
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
‘These lines of code work fine

Selection.HomeKey Unit:=wdStory


Set rngTable = ActiveDocument.Tables(6).Rows(2).Cells(2).Range

‘Have also tried Docrep.tables….
Dat = Left(rngTable.Text, Len(rngTable.Text) - 2)
If Dat = "" Then ‘No value given to Dat (String)
With ActiveDocument ' or DocRep
.Range(.Tables(5).Range.End, .Tables(6).Range.Start).Select
End With
ActiveDocument.Tables(6).Delete ‘Deletion of table works if Dat = “â€
Selection.Delete
End If


My logic says:

Have tried with both 'ActiveDocument' and 'Docrep' through the code with no
result
With docrep.Pagesetup .. the formatting of the page should work, but no
effect

Also Values should be given to Dat variable but aren’t! The two lines of
code to extract that value work fine elsewhere, but not in the new document!
Why should the lines of code to delete the empty tables work when the rest
isn’t?

Aaaaaagh!!

Any ideas?

Cheers

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top