Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7491289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:57:30+00:00 2026-05-29T15:57:30+00:00

I need a Word macro that adds the file name of a document to

  • 0

I need a Word macro that adds the file name of a document to the first line of that Word document. But not just one document at a time. I would like the Macro to add these file names to a series of Word documents in a particular folder (with each document getting their own file name).

A Macro that adds the file name to document is simple:

Selection.HomeKey Unit:=wdStory
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    Text:= "FILENAME  "
Selection.TypeParagraph

But how can I get it to add file names to an entire series of documents in a folder? I suppose the folder could be named in the Macro. For example: C:\Users\username\Desktop\somefolder\. I also suppose that a Loop could be used to go through the folder until the loop gets to the end of the documents in the folder.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:57:31+00:00Added an answer on May 29, 2026 at 3:57 pm

    This should at least get you started. I haven’t tested it, but I’ve written this type of thing a few times before, so the basic idea works.

    Dim filePath As String
    Dim thisDoc As Document
    Dim wordApp As Word.Application
    Set wordApp = New Word.Application
    
    'wordApp.Visible = True ' uncomment if you want to watch things happen
    
    'Get first file that matches
    filePath = Dir("C:\Users\username\Desktop\somefolder\*.doc") ' or *.whatever
    Do While filePath <> ""
        Set thisDoc = wordApp.Documents.Open(filePath)
    
        'Add filename at top of document
        Selection.HomeKey Unit:=wdStory
        Selection.InsertAfter filePath
        'Selection.InsertAfter ThisDocument.FullName ' alternative
        'Your way using .Fields.Add is also fine if you want the file name as 
        ' a field instead of 
        ' plain text.
    
        thisDoc.Close SaveChanges:=True
    
        'Get next file that matches and start over
        filePath = Dir()
    Loop
    
    wordApp.Quit
    Set wordApp = Nothing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing app that need embed word document in itself. since I didn't
I'm working on creating a macro in Microsoft Word (2007) for a document that
I have a word document template that I need to do a find and
I need to generate the .bin file that contains the VBA macro part of
We need to open a word document which is located on a server on
I need to replace a word AAAA in a file using dictionary: dictionary.txt AXF1
I need create a document word with Java. And I ask, how can I
I need to do a bunch of word replacements in a file and want
I'm working on a Word 2007 template with a macro that will apply character
I found this macro for Word that replaces text anywhere (headers, footers, body, text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.