Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-16-2015, 01:03 AM
gazeranco gazeranco is offline Problem getting filename and directory of current word doc. Windows 7 64bit Problem getting filename and directory of current word doc. Office 2010 64bit
Novice
Problem getting filename and directory of current word doc.
 
Join Date: Dec 2015
Posts: 5
gazeranco is on a distinguished road
Question Problem getting filename and directory of current word doc.


Hey All,

New here so welcome to me, and thanks for having me!

I did a quick search to no avail. I use some CAD software and it produces lists for me in .xsr format, not really sure what that is but once done I open them with word, fix the formatting and then save as a .doc.

I recorded a macro to do this for me automatically, but the first issue I had was it was calling everyfile the same name. So I setup a variable/method to get the current file name, but it always uses the same name and its not the corrrect name.

It always saves the file as "templates.doc" in "C:\users\me\AppData\Roaming\Microsoft\Templates\T emplates.doc"

instead of where the file really is (on my desktop in a folder) and its real name (not template)

Any ideas? Code below!

Code:
Sub Format()
'
' Format Macro
' Report Format
'
    Dim fso As New FileSystemObject
    myPath = ThisDocument.Path
    myName = fso.GetBaseName(myPath)
    
    With Selection.PageSetup
        .LineNumbering.Active = False
        .Orientation = wdOrientPortrait
        .TopMargin = CentimetersToPoints(1.27)
        .BottomMargin = CentimetersToPoints(1.27)
        .LeftMargin = CentimetersToPoints(1.27)
        .RightMargin = CentimetersToPoints(1.27)
        .Gutter = CentimetersToPoints(0)
        .HeaderDistance = CentimetersToPoints(1.25)
        .FooterDistance = CentimetersToPoints(1.25)
        .PageWidth = CentimetersToPoints(21)
        .PageHeight = CentimetersToPoints(29.7)
        .FirstPageTray = wdPrinterDefaultBin
        .OtherPagesTray = wdPrinterDefaultBin
        .SectionStart = wdSectionNewPage
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .VerticalAlignment = wdAlignVerticalTop
        .SuppressEndnotes = False
        .MirrorMargins = False
        .TwoPagesOnOne = False
        .BookFoldPrinting = False
        .BookFoldRevPrinting = False
        .BookFoldPrintingSheets = 1
        .GutterPos = wdGutterPosLeft
    End With

    Selection.WholeStory
    Selection.Font.Size = 10
    Selection.Font.Name = "Courier New"
    ChangeFileOpenDirectory (myPath)
    ActiveDocument.SaveAs2 fileName:=(myName), FileFormat:= _
        wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
        True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
        False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False, CompatibilityMode:=0
End Sub
Reply With Quote
  #2  
Old 12-16-2015, 02:09 AM
gmayor's Avatar
gmayor gmayor is offline Problem getting filename and directory of current word doc. Windows 10 Problem getting filename and directory of current word doc. Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

'ThisDocument' is the document containing the macro - probably the normal template. What you are interested in here is the 'ActiveDocument'. The code you need is

Code:
Dim myName As String
    myName = Left(ActiveDocument.FullName, InStrRev(ActiveDocument.FullName, Chr(46))) & "docx"
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 12-16-2015, 02:36 AM
gazeranco gazeranco is offline Problem getting filename and directory of current word doc. Windows 7 64bit Problem getting filename and directory of current word doc. Office 2010 64bit
Novice
Problem getting filename and directory of current word doc.
 
Join Date: Dec 2015
Posts: 5
gazeranco is on a distinguished road
Default

Graham, works like a charm! Thank you.

Maybe you can help me solve another issue, currently I am using python to say...

for each document in folder... open document... run macro....

Would you know another way of achieving this without python. Say a .bat or something? What I don't want to do is install python on everyone PC in the office who would also benefit from this 'auto' formatting of word docs. All I really know is Python so its my goto language.
Reply With Quote
  #4  
Old 12-16-2015, 02:59 AM
gazeranco gazeranco is offline Problem getting filename and directory of current word doc. Windows 7 64bit Problem getting filename and directory of current word doc. Office 2010 64bit
Novice
Problem getting filename and directory of current word doc.
 
Join Date: Dec 2015
Posts: 5
gazeranco is on a distinguished road
Default

Graham,

Actually I think I can do this in c# and add it to the software we use to produce the actual lists, that will be a clean solution which will work for everyone. Thanks for your help again!
Reply With Quote
  #5  
Old 12-16-2015, 05:17 AM
gmayor's Avatar
gmayor gmayor is offline Problem getting filename and directory of current word doc. Windows 10 Problem getting filename and directory of current word doc. Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

http://www.gmayor.com/document_batch_processes.htm demonstrates a function to process all documents in a folder (and sub folders if required).
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #6  
Old 10-28-2016, 01:46 PM
es35 es35 is offline Problem getting filename and directory of current word doc. Mac OS X Problem getting filename and directory of current word doc. Office for Mac 2011
Novice
 
Join Date: Aug 2016
Posts: 9
es35 is on a distinguished road
Default

Incredible that you need this much progamming to simply see the name of the file you are editing! There must be an easier way for the name to displayed.
Reply With Quote
  #7  
Old 10-28-2016, 03:40 PM
macropod's Avatar
macropod macropod is offline Problem getting filename and directory of current word doc. Windows 7 64bit Problem getting filename and directory of current word doc. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by es35 View Post
Incredible that you need this much progamming to simply see the name of the file you are editing! There must be an easier way for the name to displayed.
So, what's incredible about the need for a single line of code? VBA gives you the options of:
ActiveDocument.FullName - for the name & path
ActiveDocument.Name - for the name
ActiveDocument.Path - for the path
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 10-28-2016, 06:42 PM
es35 es35 is offline Problem getting filename and directory of current word doc. Mac OS X Problem getting filename and directory of current word doc. Office for Mac 2011
Novice
 
Join Date: Aug 2016
Posts: 9
es35 is on a distinguished road
Default siingle line of code?

what is the single of line of code and where does it go (how is is used?)
Reply With Quote
  #9  
Old 10-29-2016, 12:13 AM
macropod's Avatar
macropod macropod is offline Problem getting filename and directory of current word doc. Windows 7 64bit Problem getting filename and directory of current word doc. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

That depends on what you want to do with it. In this thread at least, you've provided nothing that could pass for context.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Active Directory Domain Services is unavailable problem October 2015 scottstone04 Office 0 10-15-2015 01:46 PM
Problem getting filename and directory of current word doc. Appending the filename with the current date sheeand Word VBA 2 05-14-2012 05:18 AM
Problem getting filename and directory of current word doc. How to insert current date into default filename ? czomberzdaniela Word 1 12-27-2011 07:18 PM
Problem getting filename and directory of current word doc. How to call current PC date and/or current PC year KIM SOLIS Excel 2 11-04-2011 06:09 PM
Problem getting filename and directory of current word doc. Auto insert current month's name and current year Styler001 Word 4 01-25-2010 06:40 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:58 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft