Solved

How do I Truncate Filename Word 2010?

Posted on 2012-04-05
5
607 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

626 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