Private Sub bBrowse_Click()
Const msoFileDialogFilePicker As Long = 3
Dim objDialog As Object
Set objDialog = Application.FileDialog(msoFileDialogFilePicker)
With objDialog
.AllowMultiSelect = True
.Show
If .SelectedItems.Count = 0 Then
MsgBox "No file selected."
Else
Me.[File Link].Value = Dir(.SelectedItems(1))
End If
End With
End Sub
I was able to get it to add in the cell I need it to but it when it is clicked it will not open the file or path
Please read this: Debugging VBA Code
to learn how to step through code and inspect variables.
.SelectedItems(1) already contains the full path, but Dir(.SelectedItems(1)) returns only the file name. So remove the Dir().
Now to actually open the file from a record, you need additional code e.g. in a button next to the File Link textbox, or in its DblClick event.
See here: Open Hyperlinks in Access
Related
In ms access 2013, I have a user form (frm_viewer) containing a web browser control named wbContent.
I wrote the following code to populate and display a local PDF file but cannot seem to get it to function correctly.
I did manage to get it working by referencing the Control Source property of the control to a textbox on the same form (i.e. Control Source -> Base URL -> Expression Builder -> =[MyTextbox]) but I do not want to use this method, I prefer to populate it on the fly using variables.
Private Sub lblBrowse_Click()
'declare file dialog with late binding ->
Dim fDialog As Object, strPath As String
Set fDialog = Application.FileDialog(3) 'msoFilePicker
'set parameters ->
Me.wbContent.ControlSource = ""
'initializing the file dialog ->
With fDialog
.AllowMultiSelect = False
.Filters.Clear '
.title = "Please select a file..."
'display the dialog box. If the .Show method returns True
'the user picked a file. If the .Show method returns False
'the user clicked Cancel.
If .show = True Then
strPath = .SelectedItems(1)
Debug.Print "SELECTED_FILE: " & strPath
'set source property to the string containing the full path ->
Me.wbContent.ControlSource = strPath
Me.wbContent.Requery
Else
End If
End With
End Sub
Could someone please take a look at my code and let me know how I can get it to function correctly?
Thanks!
Try this:
Me.wbContent.ControlSource = "='" & strPath & "'"
The control source needs to be a string like so: ='http://www.address.com'
the following code is attached to the button_click on a report. I want to save the report under a name selected by the user.
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogSaveAs)
fd.Show
When run, the save as dialog box pops up, but it does not save the file. Am I missing a step?
You should, next, Export the report to pdf. Replace the line fd.Show with:
If fd.Show then
DoCmd.OutputTo acOutputReport, "ReportNameHere", "PDF Format (*.pdf)", fd.SelectedItems(1), True
End IF
The last Parameter True is to open the pdf after exported. Please remove if not not needed.
NOTE: fd.SelectedItems(1) is the file the user selected.
You should also dim a boolean called notCancel, set it equal to .Show, and then using an If statement, use .Execute, the following code shows this:
Sub SaveFile()
Dim fd As FileDialog
Dim notCancel As Boolean
Set fd = Application.FileDialog(msoFileDialogSaveAs)
With fd
notCancel = .Show
If notCancel Then
.Execute
End If
End With
End Sub
I am trying to import an Excel spreadsheet into Access 2013 using a macro. Is there a way to make the macro prompt for a file path instead of having a static file path? I would like to have a message box or something similar be provided to the user to define the file path for each Excel file they would like to import. Is there a macro for this?
You could use a InputBox via vba
Dim p As String
p = InputBox("please input file path")
Debug.Print p
I am not sure however how to achieve the same thing with just a macro.
Use this function:
Function seleccionarArchivo()
Set f = Application.FileDialog(3)
f.AllowMultiSelect = False
f.Filters.Clear
f.Filters.Add "Todos", "*.*"
If f.Show Then
seleccionarArchivo = f.SelectedItems.Item(1)
End If
Set f = Nothing
End Function
To try, put a button and add this in the click event:
MsgBox (seleccionarArchivo)
I have a inventory/Contact database where I need to store a lot of images (10k items, 1k people). Now, obviously ole object is out of the question due to the sheer bloat.
Is there a better way to do this, such as storing the pathway to the image ( would be stored in a folder with the database) and having that image displayed where I need it(this would be great because some items are repeated)? Is there anyway to do this? (also, i really need to have a filebrowser to the actual image instead of typing the path manually (that would be hell))
Here is a concept
Sub Locate_File()
Dim fDialog As Office.FileDialog
Dim file_path As String
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
'Set the title of the dialog box.
.Title = "Please select one or more files"
'Clear out the current filters, and add our 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
file_path = .SelectedItems(1)
Copy_file(file_path,Right(file_path, Len(file_path) - InStrRev(file_path, "\")))
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With
End
Sub Copy_file(old_path As String, file_name As String)
Dim fs As Object
Dim images_path As String
images_path = CurrentProject.Path & "\images\"
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile old_path, images_path & file_name
Set fs = Nothing
'Update your database with the file location of images_path & file_name
End
You may need to make changes and you must require the Microsoft Office 12.0 Object Library for FileDialog to work. Much of the FileDialog code was taken from Microsoft
I want to save complex Excel file to html format. It can be easily done with Save As dialog, but it generates htm file and folder with same name + .files, where is filelist.xml, sheet001.htm, sheet002.htm, ...
How can I save only first sheet to htm format? So all the information lies in single htm file?(Like MS Word do)
Delete the other sheets before exporting.
(right click on the sheet tab and click delete)
When saving change the selection to web page and select the selection:sheet button press publish and continue as appropriate.
What you can do is copy the sheets you want to save as HTML to a new workbook and save that new workbook as HTML instead. For example:
Public Sub doIt()
hardCopyToNewBook
saveFile "C:\temp\fileName.html"
End Sub
Private Sub hardCopyToNewBook()
Dim tabs As Variant
Dim s As Worksheet
tabs = Array("Sheet1", "Sheet2")
Sheets(tabs).Copy
For Each s In ActiveWorkbook.Sheets
With s
.Cells.Copy
.Cells.PasteSpecial Paste:=xlPasteValues
End With
Next s
Application.CutCopyMode = False
End Sub
Private Sub saveFile(htmlFileName As String)
Application.DisplayAlerts = False
Application.DefaultWebOptions.SaveHiddenData = False
On Error Resume Next
Call ActiveWorkbook.SaveAs(fileName:=htmlFileName, FileFormat:=xlHtml)
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub
Found Solution - there is radio button to save only selected sheets