Assistance with code that will make copy of master docm

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Assistance with code that will make copy of master docm

Post by ABabeNChrist »

I use this code below with a command button to create a copy from master workbook.

Code: Select all

    If Application.Dialogs(xlDialogSaveAs).Show(arg2:=xlOpenXMLWorkbookMacroEnabled) Then
        Dim WSHShell As Object
        Dim strDesktopPath As String
        Set WSHShell = CreateObject("WScript.Shell")
        ' Read desktop path using WshSpecialFolders object
        strDesktopPath = WSHShell.SpecialFolders("Desktop")
        If Not Right(strDesktopPath, 1) = "\" Then
            strDesktopPath = strDesktopPath & "\"
        End If
        With WSHShell.CreateShortcut(strDesktopPath & "\" & ActiveWorkbook.Name & ".lnk")
            ' Set shortcut object properties and save it
            .TargetPath = ActiveWorkbook.FullName
            .Save
        End With
        Set WSHShell = Nothing

        MsgBox ("Your new report has been saved and a shortcut icon for this new report has been added to your desktop")

        ActiveWorkbook.Close

    Else
        MsgBox ("You have selected cancel")
    End If
I was wondering is it possible to have this code not run if name is the different from the master copy.
That way when the code is used and a new workbook is created with a different name then this code will not run within new workbook
I know there is going to be an "Else" in there somewhere :scratch:

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Assistance with code that will make copy of master docm

Post by HansV »

What does this have to do with a macro-enabled Word document (.docm)?

You could add code above the code that you posted to check the name of the active workbook:

Code: Select all

    If ActiveWorkbook.Name <> "MasterWorkbook.xlsm" Then
        Exit Sub
    End If
Replace MasterWorkbook.xlsm with the actual name of your master workbook.
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Assistance with code that will make copy of master docm

Post by ABabeNChrist »

Awesome Hans
Thank you, worked great
That was easy enought