Consulting

Results 1 to 5 of 5

Thread: change/make copy of windows document file name based on content

  1. #1

    change/make copy of windows document file name based on content

    HI

    I have nearly 580+ word documents(*.doc) file in one folder. I want to make copy of that file in the same folder with the name that is appearing inside the word file after "product name:******".

    Example: original file name output file copy based on the content file name
    xyz.doc product name:G5.doc
    pqr.doc product name: G6.doc
    abc.doc product name:G7.doc


    sample file is attached. Can anybody try and explain the code logic?
    Attached Files Attached Files

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Can you get the product name?


    Option Explicit
    
    Sub test()
        Dim s As String
    '
        s = ActiveDocument.Tables(1).Cell(2, 2).Range.Text
        s = Trim(Replace(Mid(s, 2), Chr(13) & Chr(7), ""))
    
    
        MsgBox "Product Name is : " & s
        
    End Sub

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    After looking at your sample document getting the product name consistently could be problematic. However, if the Product name is defined consistently in your 580 documents, then you could process the folder using my Batch Process AddIn and the following user defined procedure. Save this procedure in your Normal template:

    Function SaveAsProductName(oDoc As Document) As Boolean
    Dim strProduct As String
    Dim oDocSave As Document
      On Error GoTo Err_Handler
       strProduct = Trim(Left(oDoc.Tables(1).Cell(2, 2).Range.Text, Len(oDoc.Tables(1).Cell(2, 2).Range.Text) - 2))
       strProduct = Right(strProduct, Len(strProduct) - 2)
       oDoc.SaveAs2 oDoc.Path & Application.PathSeparator & strProduct, 0
       SaveAsProductName = True
    lbl_Exit:
      Exit Function
    Err_Handler:
      SaveAsProductName = False
      Resume lbl_Exit
    End Function
    You can download the addin here: http://gregmaxey.mvps.org/word_tip_p...der_addin.html
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    Thanks for your time and effort but it does not run at all. It just created the copy of same file name with .docx extension.

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    It runs fine here. Put a stop in the code after the "On Error GoTo Err_Handler " e.g.,

    On Error GoTo Err_Handler
    Stop

    Then run. When you reach the stop, step through using F8 and see what happens.
    Attached Images Attached Images
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •