Results 1 to 10 of 10

Thread: How to rename a file with changing variables and remove avery specific middle portion

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    7

    How to rename a file with changing variables and remove avery specific middle portion

    Hi guys, I'm new here and really don't know where to post my question. Although this involves VBA, my question is more about renaming and moving a file in Explorer.

    Mods please feel free to delete my post. (or move to the correct forum).

    In Word, I've got a VBA that prints the active document to a specific printer. It's actually a virtual printer that can convert the Word doc into a PDF. And yes, I do realize that Word has a built-in feature to "Save As PDF" already. I need to print to this virtual printer for a specific reason and I don't want to get into the technical reasons right now.

    Code:
    Sub MyPrint()
        Dim sPrinter As String
        With Dialogs(wdDialogFilePrintSetup)
        sPrinter = .Printer
        .Printer = "Ghost PDF Printer"
        .DoNotSetAsSysDefault = True
        .Execute
        Application.PrintOut FileName:=""
        .Printer = sPrinter
        .Execute
        End With
    End Sub

    The virtual printer and the VBA routine works just fine. However, it always creates the PDF file as:
    "Microsoft Word - something something.docx.pdf"


    The virtual printer always takes on the name of the application (in this case MS Word) and puts that the very beginning of the filename as shown in my example. I really don't like all that extra stuff in the filename. Is it possible to have a VB script that can remove whatever appears before the actual filename and remove the file extension that appears before the "*.pdf" portion that the end result looks like this?

    "something something.pdf"


    I would like for the VB script to work for all Windows applications. Sometimes I need to print to this virtual printer in MS Notepad and the output looks like this:

    "Microsoft Notepad - something something.txt.pdf"


    I have this virtual printer programmed so that it generates the PDF files to a default location:

    E:\Ghost PDF output\

    After the file has been renamed, I'd like for the script to move the file to one of these 3 locations:

    E:\Personal Archive\Converted Files
    E:\Personal Archive\Correspondence
    E:\Household\My projects

    Would it be possible to have a drop down list or a radio style menu option where I can choose from 1 of the 3 and have the script move it there?

    I'm running Win7 (64-bit) and MS Office 2010 (64-bit).

    Any help would be greatly appreciated.
    Last edited by Maverick69; Dec 9th, 2020 at 09:51 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: How to rename a file with changing variables and remove avery specific middle por

    Quote Originally Posted by Maverick69 View Post
    Although this involves VBA, my question is more about renaming and moving a file in Explorer.
    None of that has anything to do with UWP, which is Windows Store apps. If it is a question about VBA code then it belongs in the forum dedicated to VBA, which is the Office Development forum, described thusly:
    Post all your questions here regarding Microsoft Office Automation. This can include VSTO, VSTA, and VBA questions.
    I've asked the mods to move this thread.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    7

    Re: How to rename a file with changing variables and remove avery specific middle por

    Quote Originally Posted by jmcilhinney View Post
    None of that has anything to do with UWP, which is Windows Store apps. If it is a question about VBA code then it belongs in the forum dedicated to VBA, which is the Office Development forum, described thusly:

    I've asked the mods to move this thread.
    As I've already stated:

    Although this involves VBA, my question is more about renaming and moving a file in Explorer.

    Mods please feel free to delete my post. (or move to the correct forum).

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: How to rename a file with changing variables and remove avery specific middle por

    Quote Originally Posted by Maverick69 View Post
    As I've already stated:
    That's all well and good but it's better to look for the best forum yourself and avoid creating work for the mods and others. For one thing, they don't go looking for threads to move so there's no guarantee that any of them would have seen this thread if I hadn't reported it. I'm trying to provide you with information that wiill help you avoid making the same mistake in future, so you should probably just accept that and avoid making the same mistake in future. The mods are always free to delete or move your post anyway, so saying so doesn't really achieve anything.

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    7

    Re: How to rename a file with changing variables and remove avery specific middle por

    Quote Originally Posted by jmcilhinney View Post
    The mods are always free to delete or move your post anyway, so saying so doesn't really achieve anything.

    You seem quite riled up about an innocent post/question. Unless you're a mod here, I suggest you back off and let the mods do their own work.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: How to rename a file with changing variables and remove avery specific middle por

    I now see how wrong I was to point out that you could have done things better and to try to help you do so and, in so doing, improve your chances of getting the help you wanted and reducing the workload for the moderators. You did nothing wrong and you should go on believing that. To simply accept the criticism and avoid repeating the mistake is certainly not something you should have done because that would require admitting to yourself that you didn't really try to post in the right place.

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    7

    Re: How to rename a file with changing variables and remove avery specific middle por

    Quote Originally Posted by jmcilhinney View Post
    I now see how wrong I was to point out that you could have done things better and to try to help you do so and, in so doing, improve your chances of getting the help you wanted and reducing the workload for the moderators. You did nothing wrong and you should go on believing that. To simply accept the criticism and avoid repeating the mistake is certainly not something you should have done because that would require admitting to yourself that you didn't really try to post in the right place.
    Wow, I've never seen such hot air coming from an person who seems exceedingly emotional about a innocent mistake someone made. If you're so desperate to correct others, I suggest to apply to be a mod. You sure seem to be power hungry sitting on a high perch your royal highness.

    And nice way to $hit on someone's thread.

  8. #8
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,106

    Re: How to rename a file with changing variables and remove avery specific middle por

    If the filename is always in the format of "Unwanted beginning stuff - Keep this at the.end.pdf", then you can use instr to locate the first "-" character in the file and use mid to put everything after that into a new string.

    Removing the extraneous previous file extension from the name can be similarly done but will require a few more lines of code and extra logic, which I'll let you work out on your own.

    Good luck.

    https://docs.microsoft.com/en-us/off...instr-function
    https://docs.microsoft.com/en-us/off...p/mid-function

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,698

    Re: How to rename a file with changing variables and remove avery specific middle por

    Moved to Office Development
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to rename a file with changing variables and remove avery specific middle por

    for what you want to do, it would appear that you need to monitor the folder E:\Ghost PDF output\
    whenever a pdf file is saved to that folder then start some automation, and based on whatever criteria rename the file to including changing the directory

    there is 2 methods to monitor a folder the first is using windows APIs, the second is using scripting, but while either method can be invoked from vba it is not necessarily the best choice as the monitoring would be required to run continuously from windows start up, until such time as windows shuts down or some other reason requires the monitoring to stop or restart
    running a hidden instance of word or excel would be a fairly heavyweight solution, you would be better to create a stand alone application, or even possibly a script

    i do have a stand alone application in VB6 that does similar to what you require, whenever a pdf is saved to a specific folder the pdf is processed and an email sent

    you could use vb.net or c# to create a standalone application, the express edition is free to download and use
    or a VBS script could also work, but may be a bit less stable, though it would not hurt to trial it

    renaming /moving the file is the simplest part of every thing, figuring the criteria to know which folder to move to etc are the more difficult parts
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width