I am working on a macro to open a file(might already be open) and save with new name and then open the new file from vba in excel.
This file can Powerpoint,mathcad,visio, word etc..(can also be template files such as dotx etc..)
So my idea is that:
- I first need to figure out if the application is open or not,
- then I somehow need to figure if the file is open or not,
- then save it with the new filename.
- Open the new document
- Go through the document and dumps custom variables into the database, populate custom variables from database(Not shown in code below, seperate module)
Activate the new document so that the user can edit it.
Public Sub saveAsVBADocument(filenameNew As String, fileNameOld As String, applicationType As String) Dim objectApplication As Object Dim documentApplication As Object On Error Resume Next Set objectApplication = GetObject(, applicationType) On Error GoTo 0 If objectApplication Is Nothing Then Set objectApplication = CreateObject(applicationType) End If objectApplication.Visible = True On Error Resume Next Set documentApplication = objectApplication.Workbooks(FileHandling.GetFilenameFromPath(fileNameOld)) 'Excel Set documentApplication = objectApplication.Documents(FileHandling.GetFilenameFromPath(fileNameOld)) 'Word Set documentApplication = objectApplication.WorkSheets(FileHandling.GetFilenameFromPath(fileNameOld)) 'Mathcad Set documentApplication = objectApplication.Presentations(FileHandling.GetFilenameFromPath(fileNameOld)) 'PowerPoint Set documentApplication = objectApplication.Projects(FileHandling.GetFilenameFromPath(fileNameOld)) 'MS Project "Msproject.Application" Set documentApplication = objectApplication.Documents(FileHandling.GetFilenameFromPath(fileNameOld)) 'MS Visio "Visio.Application" If documentApplication Is Nothing Then Set documentApplication = objectApplication.FileOpen(fileNameOld) ' add read only End If documentApplication.SaveAs filename:=filenameNew Set objectApplication = Nothing Set documentApplication = Nothing End Sub
What is a possible solution to handle all vba acceptable document types?