I have a report in which I allow multiple users to generate their specific version and then save the file but I do not want them to overwrite my master file.
So I created a save-as dialog that saves the file with a specific name (the parameters after .show property are just different parts of the file name).
But the dialog allows users to save the file in the same folder I have the master file by default and I would like to change this to by default offering Documents folder. Is there a way to change this?
Application.Dialogs(xlDialogSaveAs).Show FYandQName & " " & _
CountryName & " " & BusValue & " " & "Financial Narratives"
Try this:
Dim s As String
'assuming an .xlsx file
s = FYandQName & " " & CountryName & " " & BusValue & " " & "Financial Narratives.xlsx"
With Application.FileDialog(msoFileDialogSaveAs)
.InitialFileName = Environ$("USERPROFILE") & "\Documents\" & s
.Show
.Execute
End With
Related
I have an application written in Microsoft Access using VBA which opens Excel files, processes the contents in various ways and then closes them. If the a file cannot be opened, then I need to detect this and skip the file otherwise the application effectively freezes.
The Excel files come from numerous sources and if they are restricted I don't have the account credentials to open them.
With a password protected file I can supply an incorrect password, detect the error and then skip the file.
Application.Workbooks.Open(FileName, False, , , "xxxx", , True)
If the Excel file has had IRM (Information Rights Management) Restrictions applied to it, then when you open the file in the Excel application you are prompted to sign into Excel with an account that has permission to open the file.
If you try to open the file using the VBA code above with the Excel application not visible, then the process just halts and no error is generated.
What I need to do is either detect that the file has IRM applied to it before trying to open it or try to open it and generate an error that I can detect.
Thanks in advance for any help in solving this.
After trying a number of approaches to this I found that restricted files contain a string that indicates that they are secured with rights management.
So if you open the file and examine the contents you can detect if it's restricted without trying to load it into Excel.
This function works with all the files that I have tried it with.
Function IsFileRestriced(FileName As String) As Boolean
Dim lngFile As Long, lngPos As Long
Dim strContent As String
'Open the file in binary mode
lngFile = FreeFile
Open FileName For Binary As lngFile
'Read the whole file into a string
strContent = Space$(LOF(lngFile))
Get #lngFile, , strContent
'Check if the file has the rights management string
lngPos = InStr(1, strContent, "Microsoft Rights Label")
'Close the file
Close #lngFile
'Return the result
If lngPos > 0 Then IsFileRestriced = True
End Function
There is a clear example in the documentation:
Sub ShowPermissions()
Dim irmPermission As Office.Permission
Dim strIRMInfo As String
Set irmPermission = ActiveWorkbook.Permission
If irmPermission.Enabled Then
strIRMInfo = "Permissions are restricted on this document." & vbCrLf
strIRMInfo = strIRMInfo & " View in trusted browser: " & _
irmPermission.EnableTrustedBrowser & vbCrLf & _
" Document author: " & irmPermission.DocumentAuthor & vbCrLf & _
" Users with permissions: " & irmPermission.Count & vbCrLf & _
" Cache licenses: " & irmPermission.StoreLicenses & vbCrLf & _
" Request permission URL: " & irmPermission.RequestPermissionURL & vbCrLf
If irmPermission.PermissionFromPolicy Then
strIRMInfo = strIRMInfo & " Permissions applied from policy:" & vbCrLf & _
" Policy name: " & irmPermission.PolicyName & vbCrLf & _
" Policy description: " & irmPermission.PolicyDescription
Else
strIRMInfo = strIRMInfo & " Default permissions applied." & vbCrLf & _
" Default policy name: " & irmPermission.PolicyName & vbCrLf & _
" Default policy description: " & irmPermission.PolicyDescription
End If
Else
strIRMInfo = "Permissions are NOT restricted on this document."
End If
MsgBox strIRMInfo, vbInformation + vbOKOnly, "IRM Information"
Set irmPermission = Nothing
End Sub
SOURCE: https://learn.microsoft.com/en-us/office/vba/api/office.permission
I am using a VBA code to write an email in Outlook with the information from a PowerPoint file and saving it as a draft, in the format ".msg".
With OutMail
.To = name_email
'Add file likethis
.Attachments.Add ("C:... " & numb_slide & ".pptx")
.Subject = "... " & date_c & " | Open Tasks " & name_project & " | Feedback ... " & dead_line_date & ",..."
.Body = StrBody
'.SaveAs "C:... " & CStr(date_c) & " | ... " & CStr(name_project) & ".msg", 5
.SaveAs "C:..." & numb_slide & ".msg", 5
'.Display Or use .Send
End With
I have two problems:
1) When I save the file using:
.SaveAs "C:..." & numb_slide & ".msg", 5
The program does not give me an error but I cannot open the draft that was saved, the error states:
If I create a normal email and save it as a draft, I can open it later.
2) If I change the way I save the file, like:
.SaveAs "C:... " & CStr(date_c) & " | ... " & CStr(name_project) & ".msg", 5
Or
.SaveAs "C:... " & date_c & " | Open Tasks " & name_project & ".msg", 5
It gives me the following error before finishing the task:
The variables I am writing in the name are strings, but I also tried to write them using CStr() to check if it would make any difference, and it does not!
I think you want 3 instead of 5.
From the olSaveAsType enumeration:
3 corresponds to Outlook message format (.msg)
5 corresponds to HTML format (.html)
Alternately, you could just drop the file type. From the MailItem.SaveAs documentation,
If the file type is not specified, the MSG format (.msg) is used.
There are numerous Q&A's for Excel WINDOWS, but none for Excel MAC that specifically answer this question.
Using Excel Mac 2011 VBA, how do I open a non-Excel file using the file's default app?
After much research and testing, I figured out the answer. See below.
This works running Microsoft Excel 14.7.2 (14.7.2) on macOS 10.12.6.
```vb
Sub open_file()
Dim scriptStr As String
Dim hfsPath As String
hfsPath = "~:Documents:Test File To Open From Excel.txt"
'--- Create AppleScript to Open Non-Excel File ---
' (Note: You cannot use POSIX path or POSIX commands)
' So, I have allowed for the tilde in a HFS path
' to mean the same as in a POSIX path: Users Home Folder
scriptStr = "set hfsPath to """ & hfsPath & """" & vbNewLine & _
"if (hfsPath starts with ""~"") then" & vbNewLine & _
" set homePath to (path to home folder) as text" & vbNewLine & _
" set hfsPath to homePath & (text 3 thru -1 of hfsPath)" & vbNewLine & _
"end if" & vbNewLine & _
"tell application ""Finder"" to open file hfsPath" & vbNewLine & _
"return hfsPath"
Debug.Print scriptStr
'--- Execute AppleScript to Open Non-Excel File ---
hfsPath = MacScript(scriptStr)
Debug.Print hfsPath
End Sub
```
For Excel Windows, see
How can Excel Windows VBA open file using default application
I am trying to set up an auto save function on an excel template so that the original template isnt over written.
Dim NameFile As String
'Gets the users username
UserName = Environ$("UserName")
'User is to input data type
datat= Application.InputBox("Enter a Data Type", "Data Type")
With Worksheets("Home")
'Sets up auto filename with YearMonthDay - Username - Filename(From a specific Cell) - Data Type
NameFile = Format(Date, "yyyymmd") & " - " & UserName & " - " & Range("A1") & " - " & datat & ".xlsm"
End With
'Sets up save location
NameFile = Application.GetSaveAsFilename(InitialFileName:=Environ("USERPROFILE") & "\Desktop\" & NameFile, Filefilter:=" Excel (*.xlsm), *.xlsm")
If NameFile = False Then
'Tell user with a caution that the file has not been saved
MsgBox "File not saved", vbCritical, "Caution"
Exit Sub
Else
ThisWorkbook.SaveAs Filename:=NameFile
MsgBox "File Saved"
End If
When I dont save the file and click cancel i get the message box telling me that the file hasnt been save... which is what I want.
But when I do save the file with the given name I get a Run-Time error '13', Type Mismatch
What am I doing wrong?
You should Dim NameFile as Variant
That way it can hold either a Boolean or a String.
The lines below are an extract from code used to generate a XML.
Access 2010 freezes then crashes when executing the Set dlgSaveAs line. This seems to only happen when data is imported using .xls files, .csv's seem to work fine.
Don't see what's wrong with the code and why it doesn't work with .xls/.xlsx files.
Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs)
With dlgSaveAs
.InitialFileName = strDate & " " & Replace(strTime, ":", "") & " " & strFFI_Filename & " XML Export" & ".xml" 'Set the default filename and directory
.InitialView = msoFileDialogViewDetails 'Set the default folder view
.Title = "Please provide a file name" 'Set your own dialog title
End With
Have you tried doing the following?
With Application.FileDialog(msoFileDialogSaveAs)
.InitialFileName = strDate & " " & Replace(strTime, ":", "") & " " & strFFI_Filename & " XML Export" & ".xml" 'Set the default filename and directory
.InitialView = msoFileDialogViewDetails 'Set the default folder view
.Title = "Please provide a file name" 'Set your own dialog title
End With
Obviously any other code associated with dlgSaveAs would need moving in the With-End With block