Solved

How do I Truncate Filename Word 2010?

Posted on 2012-04-05
5
568 Views
Last Modified: 2012-04-23
I would like to write a macro that will add a document name to our documents (not in a footer).  Our document names (from our document management system [DM]) look like this:  DOCS-#585185-v1-GENERAL_FORMS___Audit_Letter.docx.  All I really need is 585185-v1.

VB is very new to me and I'm anxious to learn more.

(Windows 7, Word 2010, sp1)
0
Comment
Question by:MariaJoyal
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 37812072
It does depend on how consistent the pattern is, but this should do it:
Sub InsertID()
    Dim strFileName As String
    Dim strID As String
    
    strFileName = ActiveDocument.Name 'e.g. "DOCS-#585185-v1-GENERAL_FORMS___Audit_Letter.docx."
    strID = Mid(strFileName, 7, 9)
    ActiveDocument.Range(0, 0).Text = strID
End Sub

Open in new window

0
 

Author Comment

by:MariaJoyal
ID: 37812302
This returns the ID exactly as I need it!  The only adjustment would be that I would like the ID wherever my cursor is.  This places the ID at the beginnng of the document.  

The only variables going forward would be when we reach document numbers over 999999 and, more immediate, versions over 9.

This is soooo cool!  Thank you.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 37812769
OK, try this:
Sub InsertID()
    Dim strFileName As String
    Dim strID As String
    
    strFileName = ActiveDocument.Name 'e.g. "DOCS-#585185-v1-GENERAL_FORMS___Audit_Letter.docx."
    strID = Mid(strFileName, 7, 9)
    SElection.Text = strID
End Sub

Open in new window

0
 

Author Comment

by:MariaJoyal
ID: 37823234
Beautiful.  One last question...  Is it possible that this could be a field name that would automatically update as the file is resaved and thus has a new name?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 37823343
Unfortunately not. There is a filename field, but there are no field functions for that can manipulate the text. You could use a bookmark and run this modified code.

Sub InsertID()
    Dim strFileName As String
    Dim strID As String
    Dim strBookmark As String
    Dim rng As Range
    
    strBookmark = "FileID"
    strFileName = ActiveDocument.Name 'e.g. "DOCS-#585185-v1-GENERAL_FORMS___Audit_Letter.docx."
    strID = Mid(strFileName, 7, 9)
    Set rng = ActiveDocument.Bookmarks(strBookmark).Range
    rng.Text = strID
    ActiveDocument.Bookmarks.Add strBookmark, rng 're-add bookmark to ensure that it contains the text
End Sub

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now