I'm trying to create a macro that will allow my end users to click on one macro that will ask them for a PDF file that they would like to add as a new page. The page is based off a template and have a merged cell in the center for the image. I'm able to get the new page created as well as get a file explorer window with specific filters opened, but I have a couple questions for this.
Is it possible to get the full filepath from the file explorer to be put into a variable to later use during the PDF insertion(as my end users may have PDF's saved in different directories)?
Code for the question:
With fd
.AllowMultiSelect = False
' Set the title of the dialog box.
.Title = "Please select the file."
' Clear out the current filters
.Filters.Clear
.Filters.Add "PDF Files", "*.PDF"
.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
txtFileName = .SelectedItems(1) 'txtFileName will return the file
End If
End With
Is it possible to insert a PDF image into an excel cell and have it fit the cells dimensions?
Code for the question:
tempRow = lastRow + 9
'Sets the correct row/column
Set rng = crrntWorkbook.Sheets(1).Range("B" & tempRow)
'This portion is trying to insert the PDF as an OLEObject picture
crrntWorkbook.Sheets(1).Range("B" & tempRow) = crrntWorkbook.Sheets(1).OLEObjects.Add(Filename:=txtFileName,_
DisplayAsIcon:=False).activate
Any help with these issues would be greatly appreciated!
Related
I gave up on named ranges, switching to the built-in import from Excell tool. When it comes to selecting the spreadsheet to import, the open file dialog won't work. Yes, I added the required reference, when prompted.
According to all the documentation I have read, this should work:
Dim FilePath As Variant
Dim dlg As Office.FileDialog
Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
With dlg
.AllowMultiSelect = False
.Title = "Select Log Sheet"
.Filters.Clear
.Filters.Add "Select Log Sheet", "*.xlsx"
If .Show = True Then
FilePath = dlg.SelectedItems
Else
MsgBox "You Clicked Cancel"
End If
End With
however, when it gets to .Filters.Add, I get
"Object doesn't support the property or method"
I'm starting to think Access hates me. I came out of retirement to write a quick log sheet processing app and the struggle with Access is getting annoying.
Thanks for any help. By the way, the next step is to feed the returned file to the saved import, and so far I've not seen how to pass a file argument to it.
You are using msoFileDialogFolderPicker dialog,
This is for selecting a folder (not a file) and thus doesn't support filtering of file type (as you are attempting to do).
Suggest using msoFileDialogFilePicker instead. This is for getting a target file path (which is what your code implies you're after).
Separately, you have a second bug and can trim your code a tad (as shown below).
Note the use of .SelectedItems(1) and doing away with setting a dialog object:
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Select Log Sheet"
.Filters.Clear
.Filters.Add "Select Log Sheet", "*.xlsx"
If .Show = True Then
FilePath = .SelectedItems(1)
Else
MsgBox "You Clicked Cancel"
End If
End With
I'm using the following VBA code to let the user choose a path to save a xlsm excel file as a xlsx file. The functionality is triggered by clicking a button in the xlsm excel file.
When I try it out, the name of the file in the saveas-window is empty, how can I set a default name (e.g. report_xyz) so the user does not have to type it himself?
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogSaveAs).Show
'determine what choice the user made
If intChoice <> 0 Then
'get the file path selected by the user
strPath = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1)
'displays the result in a message box
Call MsgBox(strPath, vbInformation, "File got saved as")
End If
' Set to a xlsx file from xlsm
Application.DisplayAlerts = False 'switching off the alert button
Worksheets("Import_Sheet").Delete 'Delete Import Sheet
ActiveWorkbook.SaveAs strPath, FileFormat:=FileFormatNum ' save with new name and format
Preset the "save as type" field while using Application.FileDialog(msoFileDialogSaveAs) with MSAccess
i think
Application.FileDialog(msoFileDialogSaveAs).InitialFileName = "test"
before
intChoice = Application.FileDialog(msoFileDialogSaveAs).Show
will do the job
In MS Access, I created a form with Form Name "Form" and created a Text Box named "Path".
When I enter a path in form with "c:Desktop.....xlsx". I should be able to import data from Excel to Access.
I tried using the Macro version in Access using the below code, but I encounter an error.
Function Import()
On Error GoTo Import_Err
Dim Str As String
Str = Forms!Form!Path.Caption
DoCmd.TransferSpreadsheet acImport, 10, "EM", Str, True, ""
Import_Exit:
Exit Function
Import_Err:
MsgBox Error$
Resume Import_Exit
End Function
Error: "Object doesn't support this property or method".
As I plan to give the file to others to run, they will have a different path, so we need to prefix it in their system. So instead if we have form path, then users can input the path in form and run it.
Use this:
Str = Forms!Form.Path
I would (in your shoes) use the File Open Dialog instead letting users type the file with path:
Set FD = Application.FileDialog(msoFileDialogOpen)
Dim FileChosen As Integer
FileChosen = FD.show
FD.Title = "Choose workbook"
FD.InitialView = msoFileDialogViewList
FD.Filters.Clear
FD.Filters.add "Excel workbooks", "*.xlsx"
FD.Filters.add "All files", "*.*"
FD.FilterIndex = 1
FD.ButtonName = "Choose this file"
If FileChosen <> -1 Then
'didn't choose anything (clicked on CANCEL)
MsgBox "No file opened", vbCritical
GoTo Exit_Function
Else
fileNamePath = FD.SelectedItems(1)
End If
Set FD = Nothing
and after that
DoCmd.TransferSpreadsheet acImport, 10, "EM", fileNamePath , True
The Access Textbox control doesn't have a Caption property. But it does have two other properties:
Text property
Value property.
AFAICT, Text returns the text currently in the textbox, while Value returns the last persisted value. If the user has modified the text in the textbox but the focus is still in the texbox, Text will return the user-entered new value, but Value will return the value before the user started editing.
Note that you can only get and set Text when the control has the focus.
So, I'm opening a FileDialog from a workbook that lets the user choose and import files. I have a bit of logic that recommends a specific directory and file via .InitialFileName, depending on the criteria set by the user. This code handles the FileDialog:
With objFileDialog
.InitialFileName = strIFN 'This is the relevant line
.AllowMultiSelect = False
.ButtonName = "Select"
.Title = "Please select the file containing " & whichFile
If .Show > 0 Then
End If
If (.SelectedItems.Count > 0) Then
strPath = .SelectedItems(1)
End If
End With
strIFN contains the path of the recommended file, for example:
\\Company-Server\Users\Username\Desktop\Intern Unterlagen\Projektcontrolling\Testlauf\AK\201909_Company_Zeiteinträge_AK.xlsx
The path works fine, but in the opened FileDialog I see this:
As you can see, the file name is cut short by a weird scroll setting. The box does in fact contain the whole file name, but doesn't show it until you click into it and scroll to the left. As this confuses the user, I am trying to display the whole file name. I'd appreciate any tips on this.
As a bonus I would ideally want to already select the recommended file (having it highlighted in blue), but that is not essential for usability.
This appears to be the normal behavior, I can replicate it pretty easily in Excel 2016. While SendKeys is usually frowned upon, it seems to be useful for this case:
With objFileDialog
.InitialFileName = strIFN 'This is the relevant line
.AllowMultiSelect = False
.ButtonName = "Select"
.Title = "Please select the file containing " & whichFile
On Error Resume Next
SendKeys "{HOME}"
On Error GoTo 0
If .Show > 0 Then
strPath = .SelectedItems(1)
End If
End With
When the dialog is displayed, the text cursor is at the end of the filename, and the text box containing the filename has focus. So, this was a bit of a shot in the dark, but I figured that "{HOME}" ought to return the cursor to the beginning of the filename, just like it would if the user hit the HOME key with the dialog open.
Note: In my observation, it seems to make no difference whether you include Wait:=True to SendKeys.
I want to write a macro that will open 30 excel files which all have the same structure.
The macro should do operation on all files, and take the results from every file and put it in another excel file.That means: all Results (Values) will be copied into the destination file.
How do I write VBA code to open multiple files?
How do I take the results from every file and place them in my destination.xls file?
Try checking out FileSearch.LookIn Property
modify that example to something similar. (This would require that all your 30 files are in one folder though)
Dim WrkBook as Workbook
Set fs = Application.FileSearch
With fs
.SearchSubFolders = False
.LookIn = "C:\My Documents" 'Path of folder to search
.FileName = "*.xls" 'Limit to excel files
If .Execute > 0 Then
Debug.Print "There were " & .FoundFiles.Count & " file(s) found."
For i = 1 To .FoundFiles.Count
WrkBook = Workbooks.Open(Filename:=.FoundFiles(i))
WrkBook.Worksheets(1).Select
ThisWorkbook.Worksheets(1).Cells(DestinationRange) = WrkBook.Worksheets(1).Cells(SourceRange).Value
Next i
Else
Debug.print "There were no files found."
End If
End With
You could also try
Workbooks("Destination.xls").Worksheets("Sheet1").Cells(DestinationRange) = WrkBook.Worksheets(1).Cells(SourceRange).Value
I think you're after a solution, that requires the user to choose the files to open and process?
Try this...
Option Explicit
Sub opening_multiple_file()
Dim i As Integer
'Opening File dialog box
With Application.FileDialog(msoFileDialogFilePicker)
'Enabling multiple files select
.AllowMultiSelect = True
.Filters.Clear
'Only Excel files can be selected
.Filters.Add "Excel Files", "*.xls*"
If .Show = True Then
For i = 1 To .SelectedItems.Count
'Opening selected file
Workbooks.Open .SelectedItems(i)
'etc do other things with it
Next i
End If
End With
End Sub