Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-23-2016, 11:46 PM
staicumihai staicumihai is offline Word 2010 Macro to rename Folder in Directory Windows 7 32bit Word 2010 Macro to rename Folder in Directory Office 2010 32bit
Advanced Beginner
Word 2010 Macro to rename Folder in Directory
 
Join Date: Dec 2015
Posts: 33
staicumihai is on a distinguished road
Default Word 2010 Macro to rename Folder in Directory

Hi guys,

In this folder D:\MIHAI\DOSARE\BAAR I have a folder that my collegues put there every few hours, the name of the folder is never the same.

I want to know if it is possible to make a VBA code that changes the folder name to "MICHAEL.current date_current hour" (ex. "MICHAEL.24.10.2016_09:43:23") without knowing the initial name of the folder (it is the only folder in the directory D:\MIHAI\DOSARE\BAAR)

I need to use the VBA code from Microsoft Word 2010.



Any ideas ?
Thanks!

Last edited by staicumihai; 10-24-2016 at 03:30 AM.
Reply With Quote
  #2  
Old 10-24-2016, 04:22 AM
gmayor's Avatar
gmayor gmayor is offline Word 2010 Macro to rename Folder in Directory Windows 10 Word 2010 Macro to rename Folder in Directory 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

The short answer is no, as you cannot use the colon symbol in a Windows file/folder name. The following will use a period/full stop instead. It will work provided the folder is the only sub folder, or if that subfolder does not begin with 'MICHAEL_'. If you are sure there will only be one subfolder you can remove the name check.
Code:
Option Explicit
Sub Rename_Folder()
Dim oFSO As Object
Dim oFolder As Object
Dim oSubFolder As Object
Dim i As Integer
Dim strPath As String
Dim strSubFolderPath As String
Dim strSubFolderNewName As String

    strPath = "D:\MIHAI\DOSARE\BAAR"
    strSubFolderNewName = strPath & "\" & "MICHAEL_" & Format(Date, "dd.mm.yyyy_") & _
                          Format(Time, "HH.MM")

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder(strPath)

    For Each oSubFolder In oFolder.SubFolders
        If Not oSubFolder.Name Like "MICHAEL_*" Then 'Optional
            strSubFolderPath = oSubFolder.Path
            Name strSubFolderPath As strSubFolderNewName
            Exit For
        End If 'Optional
    Next oSubFolder
lbl_Exit:
    Set oFSO = Nothing
    Set oFolder = Nothing
    Set oSubFolder = Nothing
    Exit Sub
End Sub
__________________
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 10-28-2016, 03:01 AM
staicumihai staicumihai is offline Word 2010 Macro to rename Folder in Directory Windows 7 32bit Word 2010 Macro to rename Folder in Directory Office 2010 32bit
Advanced Beginner
Word 2010 Macro to rename Folder in Directory
 
Join Date: Dec 2015
Posts: 33
staicumihai is on a distinguished road
Default

Thanks a lot man!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word 2010 Macro to rename Folder in Directory Word 2010 - directory mail merge - two separate group of data in one document htsu Mail Merge 2 03-07-2016 03:10 PM
Word 2010 Macro to rename Folder in Directory vba code for folder name, subfolder directory, file name & revision times klpw Excel Programming 2 12-24-2015 12:31 AM
Can a macro rename Excel files based on a cellname? chrisd2000 Excel Programming 1 06-23-2014 06:50 PM
Macro to rename multiple Word file with same suffix ozil61 Word VBA 2 05-06-2014 07:36 AM
Word 2010 Macro to rename Folder in Directory Rename Outlook 2003 Contacts Folder sfpermits Outlook 6 07-21-2010 09:56 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:14 AM.


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