Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2007

Tip: Looking for answers? Try searching our database.

recognizing an active document under a generic name

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ccornett - 27 Aug 2007 23:22 GMT
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.
Greg Maxey - 28 Aug 2007 00:04 GMT
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.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> I am writing macros to use on hundreds of documents with different
> names.  I am copying from each and pasting in a different template.
[quoted text clipped - 4 lines]
> macro to a separate document.  I have tried using Dim statements, Set
> oWord, etc.  Please help!  Thanks.
ccornett - 28 Aug 2007 01:22 GMT
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
   ...

> Not sure that I understand your question completely.
>
[quoted text clipped - 20 lines]
> > macro to a separate document.  I have tried using Dim statements, Set
> > oWord, etc.  Please help!  Thanks.
Greg Maxey - 28 Aug 2007 02:11 GMT
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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> 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
[quoted text clipped - 76 lines]
>>> apply the macro to a separate document.  I have tried using Dim
>>> statements, Set oWord, etc.  Please help!  Thanks.
ccornett - 28 Aug 2007 03:26 GMT
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.

> 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
[quoted text clipped - 114 lines]
> >>> apply the macro to a separate document.  I have tried using Dim
> >>> statements, Set oWord, etc.  Please help!  Thanks.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.