INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to alter the code to find the file on users machine

How to alter the code to find the file on users machine

(OP)
I have an export code on my report that allows the end-user to select Excel or PDF. The Excel code never finds the file because users put the file anywhere they want in their machine and I want some code that will find that file on their machine. If the template does not actually exist, then they have to create it. I remember it was something like Environ & ....forgot...But here is what I have so far:

[code]
Dim mydlg As Office.FileDialog
Set mydlg = Application.FileDialog(msoFileDialogSaveAs)
Dim myfile As String
With mydlg
.AllowMultiSelect = False
'.Filters.Clear
.InitialFileName = CurrentProject.path
If msgbox("Do you want to export to Excel?", _
vbQuestion + vbYesNo) = vbYes Then
'.Filters.Add "Excel File", "*.xlsx;*.xls"
If Dir(CurrentProject.path & "\frmQualityGraph.xlsx") = "" Then
msgbox "Template excel file is not present in the current directory. So Export is not possible", vbOKOnly + vbInformation, "Excel"
Set mydlg = Nothing
Exit Sub
End If

If .Show = -1 Then
myfile = .SelectedItems(1)
Else
msgbox "You must select a file to export", vbOKOnly + vbInformation, "Excel"
Set mydlg = Nothing
Exit Sub
End If
Dim myxls As New Excel.Application
Set myxls = New Excel.Application
Dim mybook As Excel.Workbook
Set mybook = myxls.Workbooks.Open(CurrentProject.path & "\frmQualityGraph.xlsx")
Dim mysheet As Excel.Worksheet
Set mysheet = mybook.Worksheets(1)
mysheet.Activate
mysheet.Range("A2:T65000").Select
myxls.Selection.ClearContents
Dim k As Long, m As Long
k = 2
Dim myset As DAO.Recordset
Set myset = CurrentDb.OpenRecordset("qryResult", dbOpenSnapshot)
Dim myfld As DAO.Field
With mysheet
Do Until myset.EOF
For m = 0 To myset.Fields.Count - 1
.Cells(k, m + 1) = myset.Fields(m)
Next
.Rows(k).RowHeight = 18.75
k = k + 1
myset.MoveNext
Loop
End With
If Dir(myfile) <> "" Then Kill myfile
mybook.Sheets("PivotChart").Select
mybook.ActiveSheet.ChartObjects("Chart 1").Activate
mybook.ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
mybook.SaveAs myfile
Set mysheet = Nothing
myset.close
Set myset = Nothing

Set myfld = Nothing
myxls.Visible = True
Set myxls = Nothing


Else
'.Filters.Add "PDF File", "*PDF"
If .Show = -1 Then
myfile = .SelectedItems(1)
Else
msgbox "You must select a file to export", vbOKOnly + vbInformation, "PDF"
Set mydlg = Nothing
Exit Sub
End If

DoCmd.OutputTo acOutputReport, "rptSearchQuality", acFormatPDF, myfile, True

End If
Set mydlg = Nothing
End With
[\code]

RE: How to alter the code to find the file on users machine

Sub FindMaster()
Dim wbResults As Workbook

With Application.FileSearch
.NewSearch
.LookIn = "C:\"
.FileType = msoFileTypeExcelWorkbooks
.Filename = "Master.xls"
If .Execute > 0 Then 'Workbooks in folder
Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
Else
MsgBox "Not found"
End If
End With
End Sub

Is this any use?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close