Add FileName & Document title to Report

S

StanC

I'm extracting information from all documents in a folder to a report. I
want the FileName w/out extension and the document's title to print just
before the extracted info is pasted into the report document.

What's the most efficient way to get that info into the report document?
From Autotext properties?
 
J

Jay Freedman

StanC said:
I'm extracting information from all documents in a folder to a
report. I want the FileName w/out extension and the document's title
to print just before the extracted info is pasted into the report
document.

What's the most efficient way to get that info into the report
document? From Autotext properties?

If you have the filename in a string variable, use the function
WordBasic.FilenameInfo$(filename, 4) to return the part without the
extension. See http://word.mvps.org/FAQs/MacrosVBA/WordBasicCommands.htm for
this little-known function.

The title is one of the members of the BuiltInDocumentProperties collection:
ActiveDocument.BuiltInDocumentProperties("Title")
If you need to get it without opening the document first, see
http://word.mvps.org/FAQs/MacrosVBA/DSOFile.htm.
 
S

StanC

Thanks! I'll definitely work with those.

But, what piece of code will cause those properties to appear in the
report document? Is it enough to simply put that code in a sub? TypeText
appears to accept only a string of text.

I'm a beginner and trying to do some self-teaching. Apologies for such
rudimentary questions.
 
J

Jay Freedman

StanC said:
Thanks! I'll definitely work with those.

But, what piece of code will cause those properties to appear in the
report document? Is it enough to simply put that code in a sub?
TypeText appears to accept only a string of text.

I'm a beginner and trying to do some self-teaching. Apologies for such
rudimentary questions.

Hi Stan,

It would be a bit easier to explain this if I could see the macro code you
already have, or at least the part that inserts the extracted information
you referred to.

I'll assume that your code actually opens each "source" document to extract
the data. Assuming the filename of the current source document is in the
string variable FileNm, and the cursor is in the "destination" document at
the point where the information is to be inserted, this line will put in the
name without the extension:

Selection.TypeText WordBasic.FilenameInfo$(FileNm, 4)

This will work because WordBasic.FilenameInfo$ is a function whose return
value is a string.

When you're collection information in the source document, use code like
this to get the title and store it in a string variable:

Dim Title As String
Title = ActiveDocument.BuiltInDocumentProperties("Title")

Later, when the cursor is in the destination document, use

Selection.TypeText Title

to put that string in place.

When you get more time to learn about VBA, look in the Help file and the
macro articles at http://word.mvps.org to learn about creating Document
variables and Range objects so you can avoid moving the cursor around with
the Selection. It's much faster and more reliable that way.
 
S

StanC

Many thanks! And I will definitely go through the documents on macros at
that link. Also, I finally received my copy of VBA Developers Reference
and another more specifically on developing VBA-based solutions from
Office Components. I'm hoping these will structure my learning a little
better.
 

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