# Visual Basic > Office Development >  How to rename a file with changing variables and remove avery specific middle portion

## Maverick69

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. 



```
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.

----------


## jmcilhinney

> 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.

----------


## Maverick69

> 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*).

----------


## jmcilhinney

> 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.

----------


## Maverick69

> 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.

----------


## jmcilhinney

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.

----------


## Maverick69

> 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.

----------


## OptionBase1

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

----------


## dday9

_Moved to Office Development_

----------


## westconn1

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

----------

