Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2005

Tip: Looking for answers? Try searching our database.

Trim/Split Filename and insert as text field

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Reiner Griess - 26 Jul 2005 13:56 GMT
Hi there,

hopefully you guys guide me... Im not familiar with VB.

I want to add the filename of a special format in the footer of a word
doc, automatically everytime I save the doc.

This is what I want in detail:

1. When file gets saved
   - set file name (document property) to the name of the file, trimmed
     down to the characters before the first underscore (_).

     E.g. Filename is  "myfile_sometxt.doc" -> will get "myfile"

2. Automatically set the filename variable (trimmed down in step 1) into
   the footer of the document.

Any help would be reaaaally appreciated.
Tank you!
reiner

PS: This is what I've done so far...

Macro to get the trimmed down filename:
Sub InsertFnameOnly()
  Dim fname, fname2 As String
   ActiveDocument.Save
   fname2 = Selection.Document.Name
   fname = Left(fname2, (InStr(fname2, "_") - 1))
   Selection.TypeText fname
End Sub

Problems:
- how to set the filename in document properties?
- how to paste the trimmed filename into the footer?
- how to do this automatically (ie without the need of explicitely
  execute any macro)?
Jezebel - 26 Jul 2005 14:18 GMT
1. Create a Custom document property called eg FileShortName. (Use File >
Properties > Custom. Give the property any value you like.)

2. In the footer of the document, insert a DocProperty field: { DocProperty
FileShortName }

3. Write a macro called something like SetName:

Private Sub SetName()

   Dim pName as string
   Dim pIndex as long
   Dim pDialog as Word.Dialog

   set pDialog = Dialogs(wdDialogFileSaveAs)

   'Show the dialog without carrying out any actions
   '(returns TRUE if user clicks OK)
   If pDialog.Display then
       pName = pDialog.Name
       pIndex = instr(pName, "_")

       'User might not enter a name with an underscore
       if pIndex > 0 then
           pName = left$(pName, pIndex - 1)
       end if

       'Set the property
       ActiveDocument.CustomDocumentProperties("FileShortName") = pName

       'Update fields in the footer. Might need to change this if you have
a first page footer, or multiple sections, etc
       ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Fields.Update

       'Save the document under the new name
       pDialog.Execute
   end if

End Sub

4. Write a macro called FileSaveAs to run in place of the built-in SaveAs
command. All this macro does is call SetName.

5. Write a macro called FileSave. This calls SetName only if the document
has never been saved; otherwise it simply saves the document.

> Hi there,
>
[quoted text clipped - 34 lines]
> - how to do this automatically (ie without the need of explicitely
>   execute any macro)?
Reiner Griess - 26 Jul 2005 14:55 GMT
Hey Jezebel,

simply greeeeat!

I thank you *very* much for this 100% and *fast* answer.

Wow, that was fast!

Yours, scincerely
reiner

> 1. Create a Custom document property called eg FileShortName. (Use File >
> Properties > Custom. Give the property any value you like.)
[quoted text clipped - 80 lines]
>>- how to do this automatically (ie without the need of explicitely
>>  execute any macro)?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.