recognizing an active document under a generic name

C

ccornett

I am writing macros to use on hundreds of documents with different names. I
am copying from each and pasting in a different template. When I have the
document I am currently working on open, how do I get Word to recognize the
specific document name as a generic one (i.e. doc1) without actually saving
as a different filename? Otherwise I will have to write in a separate file
name each time I apply the macro to a separate document. I have tried using
Dim statements, Set oWord, etc. Please help! Thanks.
 
G

Greg Maxey

Not sure that I understand your question completely.

The document that you are currently working on is the active docuement.
Open a few documents and run this code:

Sub ScratchMacro()
Dim oDoc As Document
MsgBox ActiveDocument.Name
For Each oDoc In Application.Documents
MsgBox oDoc.Name
Next
End Sub

The document that you are working on (visible and with a cursor) is the
active document.
 
C

ccornett

Thanks for the help. I believe that I was unclear. Here is what I am trying
to do: copy information out of a completed form (Word document) and paste it
into a template (a seperate Word document). I have to get several bits of
information from the form and wrote the macro in stages reflecting copying
and pasting each bit. There are >500 forms that I need to process through
and this is where the complication started. When I wrote each macro I
switched between the form and template, and the macro recorded the name of
the form and template. I therefore need to assign a generic name to the
opened form (because the document file will change). But retain the name of
the template file. Your code works, but did not allow me to switch to the
template to paste the inforamtion.

See an excerpt of my code below where I switched between the form and
template. I started the macro in the form, found and copied information and
then switched to the template to paste. "SMMS- template2" is the template
file and "" is one of the form files.

Any suggestions are much appreciated! Thank you.


Selection.Find.ClearFormatting
With Selection.Find
.Text = "Data Set:"
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy
Windows("SMMS- template2").Activate
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Island of "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.PasteAndFormat (wdPasteDefault)
Selection.HomeKey Unit:=wdStory
Windows("DATA SET SUMMARY HA001KAPA1993SEP1").Activate
Selection.EndKey Unit:=wdLine
...
 
G

Greg Maxey

I may be appearing a bit dense, but I am still not sure exactly what it is
that you are trying to do. I think you have a document open and you want to
run some code that finds text in the documetn and then finds a place in a
template to paste it: Something like this perhaps:

Sub ScratchMacro()
Dim oTgtDoc As Document
Dim oDoc As Document
Dim oRng As Word.Range
Set oDoc = ActiveDocument
Set oTgtDoc = Documents.Open(FileName:="C:\test.dot")
oDoc.Activate
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Data Set:"
.Execute
End With
With oRng
.Collapse wdCollapseEnd
.MoveEndUntil (vbCr)
.Copy
End With
oTgtDoc.Activate
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Island of "
.Execute
End With
oRng.PasteAndFormat (wdPasteDefault)
oTgtDoc.Close SaveChanges:=wdSaveChanges
oDoc.Activate
End Sub

You might consider putting all the files that you want to process in a
common folder and process the entire batch of files in one swoop. See:

http://gregmaxey.mvps.org/Process_Batch_Folder.htm
 
C

ccornett

Thanks! It was difficult to explain flipping between documents (both within
a macro and then multiple macros) in words, but you understood! Once
everything is prepped, as you suggested, I will run all of the documents in a
single batch.
 

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