Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I Truncate Filename Word 2010?

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

926 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