Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I Truncate Filename Word 2010?

Posted on 2012-04-05
5
Medium Priority
?
614 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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

670 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