I have a minor issue for one of the features on my userform. If a user clicks the "Browse" button but doesn't click the "Open" button after selecting a file, and instead either closes the window or selects "Cancel", a Run-time error '5': Invalid procedure call or argument is displayed.
Ideally if the user closes the window or selects "Cancel" I would like the user to be returned to the form. I know I'm missing some code but don't know what or where it should go. Below is the code I have;
Private Sub CommandButtonPicture_File_Browse_Click()
Dim FindFile As Office.FileDialog
Dim FoundFile As Variant
Set FindFile = Application.FileDialog(msoFileDialogOpen)
With FindFile
.Filters.Clear
.Filters.Add "Jpegs", "*.jpg;*.jpeg*"
.Filters.Add "Png", "*.png*"
.Filters.Add "Gif", "*.gif*"
.Filters.Add "Bitmaps", "*.bmp*"
.Filters.Add "PDF", "*.pdf*"
.Filters.Add "All Files", "*.*"
.AllowMultiSelect = False
If .Show = True Then
TextBoxPicture_File_Link.Value = .SelectedItems(1)
End If
Me.TextBoxPicture_File_Link = .SelectedItems(1)
End With
End Sub
Please note, the code currently works it adds the file link to text box as intended, it's just the issue discussed above that's a problem.
Related
I have an image control in a Word UserForm and a button which opens a dialog box to let the user select an image. If I want to copy this image to another document, how do I do it via code?
Here's the code to select the image via the button:
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.Title = "Select Photo"
.Filters.Clear
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1
If .Show = -1 Then
filetoinsert = .SelectedItems(1)
For Each vrtSelectedItem In .SelectedItems
Next vrtSelectedItem
Me.myImage.Picture = LoadPicture(.SelectedItems(1))
Else
End If
End With
Set fd = Nothing
End Sub
At the bottom of this form, I have another button that will copy this photo to another document. Here's what i've tried so far:
Private Sub cmdTransferPhoto_Click()
'Copy and Paste photo on "NextDoc" document. On the this document, I have a Picture Content Control called "Picture" where i plan to paste the copied image
Set Picture = NextDoc.SelectContentControlsByTitle("Picture").Item(1)
Picture.Range.InlineShapes.AddPicture _
Me.myImage.Picture, linktofile:=False, savewithdocument:=True
End Sub
When I click, the cmdTransferPhoto button, I get this error
Run-time error '5152'
This is not a valid file name.
Try one or more of the following:
* Check the path to make sure it was type correctly.
*Select a file from the list of files and folders.
Ideas are welcome! Thanks.
As you could have discovered from the documentation, and should have noticed from the Intellisense, .InlineShapes.AddPicture takes FileName, datatype string, as the required argument. As you are attempting to pass an image (which will be a rather poor quality bitmap) into this argument it quite naturally fails.
You already obtained the filename which you used to load the picture into the image control, so why not use it?
Picture.Range.InlineShapes.AddPicture FileName:=filetoinsert, LinkToFile:=False, SaveWithDocument:=True
Now I have this code in order to open a dialog box to search for a concrete type of file, a .txt called memory: memory.txt
So:
Dim S As String
S = OpenCommDlg("C:\memory.txt")
If IsNull(S) Or S = "" Then Exit Sub
Unfortunately, this opens a dialog box in C:\, of course, but looking for image type archives, which is absolutely not what I'm looking for. You can see this at the right side of the attached image:
Anyone knows how to modify this code in order to find the kind of archive we are looking for, and it's name...
You can use the FileDialog method. This will save the full file path to your s string, and you can add multiple filters to filter by file type.
Dim s As String
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.Clear
.Filters.Add "Text Files", "*.txt"
.Filters.Add "All Files", "*.*"
If .Show Then s = .SelectedItems(1)
End With
Debug.Print s
If you want to automatically fill in the inputbox with your filename, then you can just add this line to the with statement:
.InitialFileName = "C:\memory.txt"
I am very new to VBA and I am creating a template for my boss. I want to force users to "save as" so that they don't overwrite the template. In other words, I'd like to disable the cancel button when the save as dialog box pops up.
Here is my code:
Dim fPth As Object
Set fPth = Application.FileDialog(msoFileDialogSaveAs)
With fPth
.InitialFileName = CTAPath
.InitialFileName = CTAName & "_CAP DATA"
.Title = "Save with your CTA file"
.InitialView = msoFileDialogViewList
.FilterIndex = 2
If .Show = -1 Then
.Execute
End If
End With
I'm thinking I should create an ELSE statement within the IF statement but I can't figure out what it should be. I've tried searching and I'm not coming up with any solutions.
Thank you!
That's not the way to do this: if the users choose the file itself in the "Save as" filelist, you might end up in the same situation.
I'd advise you to make the file read-only, so nobody can change or overwrite it.
I'm not sure you're able to disable Cancel button, but there is workaround...
You can loop .Show method till user hits Save button ;)
For example:
Sub PreventCancel()
Dim fPth As Object
Set fPth = Application.FileDialog(msoFileDialogSaveAs)
Dim result As Variant
With fPth
.InitialFileName = CTAPath
.InitialFileName = CTAName & "_CAP DATA"
.Title = "Save with your CTA file"
.InitialView = msoFileDialogViewList
.FilterIndex = 2
Do
result = .Show
Loop While result <> True
.Execute
End With
End Sub
[EDIT]
I'd suggest to use Application.GetSaveAsFilename Method (Excel) instead of FileDialog, because it gives you more control over it.
Please, read valuable comments to your question also.
When I open the standard fileopen dialog using VBA (Word)
e.g., Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
I get what I ask for, but it's not what I want. I am defaulted to opening the selected file in the parent program. (If in Word, and displaying Excel files, the selected Excel file will open in Word, not the program associated with the .xls extension; if displaying PDF, the selected file will open in Word, etc.).
How can I get a Window's level (as opposed to application level) dialog to open so that when I click a document with a non-Word extension, the proper program associated with the file extension will be called. (I know that I can always right click on the file and click 'Open With . . . ', but I don't want to have to teach this to my staff if I can avoid it.)
Private Sub Tester()
Dim fd As FileDialog, sh As Object
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "All files", "*.*"
If .Show = -1 Then
Set sh = CreateObject("Shell.Application")
sh.Open .SelectedItems(1)
End If
End With
End Sub
I have this task to convert recenntly written VBA code to OpenOffice version. I tried to launch it from OpenOffice, but it doesn't work (mostly "unsatisfied query..." error. I am now stuck on Open File Dialog, I can either use VBA compatible open file dialog as mine looks now like that (giving error):
FileToOpen = Application.GetOpenFilename("Please choose a file to import", "Excel Files *.dbf (*.dbf)")
I can also use OpenOffice file dialog box, but couldn't find any information on this.
Thanks in advance
I'm confused on what you're asking, but if you're having trouble creating a file dialog box, this is VBA code that will do it for you. I think this is what you're asking, but I could be wrong.
Private Sub cmdFileDialog_Click()
' This requires a reference to the Microsoft Office 11.0 Object Library.
Dim fDialog As Office.FileDialog
Dim varFile As Variant
' Clear the list box contents.
Me.FileList.Value = ""
' Set up the File dialog box.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
' Change allowmultiselect to true if you want them to be able to select multiple files
.AllowMultiSelect = False
' Set the title of the dialog box.
.Title = "Select One or More Files"
' Clear out the current filters, and then add your own.
.Filters.Clear
.Filters.Add "All Files", "*.*"
' Show the dialog box. If the .Show method returns True, the
' user picked at least one file. If the .Show method returns
' False, the user clicked Cancel.
If .Show = True Then
' Loop through each file that is selected and then add it to the list box.
For Each varFile In .SelectedItems
Me.FileList.Value = varFile
Next
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End Sub