Open latest pdf file with vba - vba

I have been looking for codes on the internet and writing some myself to open the latest pdf file in a sharepoint folder. The files that I am interested in the folder are all named as such "SD Progress_YYYYMMDD.pdf". So I tried having a for loop through all the files in this folder and comparing the YYYYMMDD in each file names and keeping the highest value (classic max value programming). Unfortunately I am quite new with vba and I believe that I have a mistake with string or array dimensions in my code below but I can't quite figure it out. The following error occurs at the first If statement:
Run-time error '13': Type mismatch
You guys are the experts so if you have any advices for my code below please I am very interested. Thank you
CODE BELOW HAS BEEN EDITED AND WORKS NOW. THANK YOU.
Sub Shop_Drawing_Status()
Dim MyPath As String
Dim LatestDate As Integer
Dim MyFile As String
MyPath = "C:\Users\Documents...etc\"
MyFile = Dir(MyPath & "*.pdf", vbNormal)
While Len(MyFile) > 0
If Right(MyFile, 3) = "pdf" Then
LatestFile = Split(MyFile, ".")
If Right(LatestFile(0), 4) > LatestDate Then
LatestDate = Right(LatestFile(0), 4)
End If
End If
MyFile = Dir()
Wend
ActiveWorkbook.FollowHyperlink (MyPath & "SD Progress_2020" & LatestDate & ".pdf")
On Error Resume Next
End Sub

Related

Open the most recent file in a shared folder

I would like to open the latest file in a shared folder.
I have a code to check the files in a folder of my laptop, like "Downloads" but I have to open a folder in a shared drive and then copy the info of this workbook and paste in another file.
'Force the explicit declaration of variables
Option Explicit
Sub OpenLatestFile()
'Declare the variables
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
'Specify the path to the folder
MyPath = "P:\GTS\zdss\"
'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
'Get the first Excel file from the folder
MyFile = Dir(MyPath & "*.xls", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'Loop through each Excel file in the folder
Do While Len(MyFile) > 0
'Assign the date/time of the current file to a variable
LMD = FileDateTime(MyPath & MyFile)
'If the date/time of the current file is greater than the latest
'recorded date, assign its filename and date/time to variables
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
'Get the next Excel file from the folder
MyFile = Dir
Loop
'Open the latest file
Workbooks.Open MyPath & LatestFile
End Sub
Here are 3 ideas. I am not sure whether one will solve your problem but maybe it could help you.
1) I have found this discussion on a forum (http://www.vbaexpress.com/forum/showthread.php?19669-Workbooks-Open-using-network-path). In my understanding, the problem seems close to your. Maybe try to use the few lines using to manage errors (begin by On Error Resume next).
2) I think you have already verified but the extension of the file is ".xls", not ".xlsx" ?
3) Doing operations on dates in VBA needs particular functions. Here, you are doing a comparison as if it is integers (LMD > LatestDate). Moreover, I am not sure that LatestDate will have a proper value, because you never define it in the beginning. I suggest to change the code in this way. First define LatestDate before the While statement with an arbitrary low value (so, you are sure that the variable has a value and the If LMD > LatestDate statement will work properly).
LatestDate = Format("01.01.1900", "dd.mm.yyyy")
Second, change the If LMD > LatestDate statement :
If DateDiff("d",LatestDate,LMD) > 0 Then
Of course, you need to change the parameter "d" (for day) in case you want a comparison in other unit.
Cheers.

Combining CSV files from one folder into one file through MS Acces s vba

Hi there so I finished the section of a program which calculates and exports a csv with results. (ends up about 1600 csv files) each having only 1 column and between 20 and 0 rows. I would like my MS Access VBA program to join them together into one larger CSV. So Same header only once at the top of the new file.
The program i have so far seems to fall over at the part where it tries to import the Reg. Number of the File.
Dim db As DAO.Database
Set db = CurrentDb
MTH = Format(Date, "mmm")
UserInput = InputBox("Enter Country Code")
Dim strSourcePath As String
Dim strDestPath As String
Dim strFile As String
Dim strData As String
Dim x As Variant
Dim Cnt As Long
Dim r As Long
Dim c As Long
Dim wks As Excel.Worksheet
Application.Echo False
'Change the path to the source folder accordingly
strSourcePath = "Q:\CCNMACS\AWD" & CTRY
If Right(strSourcePath, 1) <> "\" Then strSourcePath = strSourcePath & "\"
'Change the path to the destination folder accordingly
strDestPath = "Q:\CCNMACS\AWDFIN"
If Right(strDestPath, 1) <> "\" Then strDestPath = strDestPath & "\"
strFile = Dir(strSourcePath & "*.csv")
Do While Len(strFile) > 0
Cnt = Cnt + 1
If Cnt = 1 Then
r = 1
Else
r = Cells(Rows.Count, "A").End(xlUp).Row + 1
End If
Open strSourcePath & strFile For Input As #1
If Cnt > 1 Then
Line Input #1, strData
End If
Do Until EOF(1)
Line Input #1, strData
x = Split(strData, ",")
For c = 0 To UBound(x)
wks.Cells(r, c + 1).Value = Trim(x(c)) 'Error is here: Run time error '91': Object variable or With Block variable not set
Next c
r = r + 1
Loop
Close #1
Name strSourcePath & strFile As strDestPath & strFile
strFile = Dir
Loop
Application.Echo True
If Cnt = 0 Then _
MsgBox "No CSV files were found...", vbExclamation
Your question isn't absolutely definitive as to what you're trying to do, but if I understand correctly, you just need to append several files to the end of each other, to make "one big CSV".
If that's true then there are several ways to do this a lot simpler than using VBA. .CSV files are just plain text files with comma's separating each field, and a .CSV filename extension.
Personally I would use Notepad++ (I assume it's capable of this; it does everything else), or perhaps even easier, I would use the Windows Command Prompt.
Let's say you have a folder with files:
File1.csv
File2.csv
File3.csv
...etc
Open the Windows Command Prompt. (One way is with the Windows key + R, then type cmd and hit Enter.)
Change directory with to the file location using cd (same as ChDir).
(For example, you might use cd c:\users\myFolder,
and then hit Enter)
To combine all CSV's in the folder into one, you could use a command like:
copy *.csv combinedfile.csv
That's it!
A file is created named combinedfile.csv. You can open in Excel or a text editor (like Notepad) to double-check it and adjust manually if necessary.
Obviously there are many ways you could vary the command, like if you only wanted the files that start with the word File you could use:
copy file*.csv combinedFile.csv
This should do what you want.
Sub Import()
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = True
' Replace C:\Documents\ with the real path to the folder that
' contains the EXCEL files
strPath = "C:\your_path_here\"
' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "Table1"
strFile = Dir(strPath & "*.csv")
Do While Len(strFile) > 0
strPathFile = strPath & strFile
DoCmd.TransferText acImportDelim, "", strTable, strPathFile, blnHasFieldNames
' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
' Kill strPathFile
strFile = Dir()
Loop
End Sub
See the links below for additional details pertaining to this topic.
https://anthonysmoak.com/2018/04/10/how-to-fix-an-import-specification-error-in-microsoft-access/
https://www.oakdome.com/programming/MSAccess_ExportSpecifications_TransferText_To_CSV.php

Retrieving the last modified file with a partly variable name

We have a system that automatically downloads data and puts it in excel and other sheets. I am trying to write a macro for a master spreadsheet that retrieves the latest version of a certain file to edit, copy and paste into the master sheet.
I have trouble retrieving the file as the filenames include dates.
I am quite new to VBA and am still just throwing pieces of code together to get a working thing, but I cannot find exactly what I am looking for.
Filename is for example 'ML0003 - Daily Order Entry Information - 170927'
The last 6 figures represent the date and changes every time.
This is my code so far:
Dim dtTestDate As Date
Dim sStartWB As String
Const sPath As String = "D:\Berry\AddIn\Testing\"
Const dtEarliest = #1/1/2010#
dtTestDate = Date
sStartWB = ActiveWorkbook.Name
While ActiveWorkbook.Name = sStartWB And dtTestDate >= dtEarliest
On Error Resume Next
Workbooks.Open sPath & "ML0003 - Daily Order Entry Information - " & " ****** " & ".xls"
dtTestDate = dtTestDate - 1
On Error GoTo 0
Wend
If ActiveWorkbook.Name = sStartWB Then MsgBox "Earlier file not found."
I was under the assumtion that the asterix would allow any character there, but this does not seem to work. Any ideas?
You will want to use the Dir function to look for a file using the wildcard, like this:
Dim sFilename As String
While ActiveWorkbook.Name = sStartWB And dtTestDate >= dtEarliest
sFilename = Dir(sPath & "ML0003 - Daily Order Entry Information - *.xls*")
If sFilename <> "" Then Workbooks.Open sPath & sFilename
Wend

Excel macro to open file with partial filename

I have tried looking on various forums and cannot seem to find a solution that fits my needs.
I have a file "BABERs FORMULAS - 24 Jan 2017 - Rev 079 11-27.xlsm". I use a macro to save the changes to the file which adds the date, revision number and timestamp to the filename. The file is located at the following path "D:\FORMULAS".
The macro I have is as below but this then gives me a Run-time error '1004' and says to check that the spelling of the file name and verify the location is correct.
Dim fname as Variant
fname = Dir("D:\FORMULAS\BABERs FORMULAS*")
If fname <> "" then
Workbooks.open (fname)
End If
Any ideas where I could be going wrong? Any assistance would be appreciated.
Try something like the code below:
Dim fname As Variant
Dim myPath As String
myPath = "D:\FORMULAS\"
fname = Dir(myPath & "BABERs FORMULAS*")
If fname <> "" Then
Workbooks.Open (myPath & fname)
End If

how to convert .docx and .pdf to .txt file

I am working on an application for which i need to convert .docx and .pdf file to .txt
file with basic formatting. I searched it in internet but couldn't find any free third party dlls. Can any one suggest me best way and some dlls reference for this.
Thanks in Advance
http://support.microsoft.com/kb/316383 describes what you want to do with .docx files very well.
http://visualbasic.about.com/od/quicktips/qt/disppdf.htm describes the same, but with .pdf files.
Once you have read files into your code, output to a txt file using VB.NET's built in file writing functions.
The code below will handle the job for you. It is something I wrote for the big boss haha. I hope it helps. The code reads the first cell in the work sheet as the folder where docx files are present and then converts them to txt files one by one saving in the same folder.
Const wdFormatText = 2
If Not Len(Cells(1, "A").Value) > 0 Or Dir(Cells(1, "A").Value, vbDirectory) = "" Then
MsgBox ("Invalid Folder")
Exit Sub
End If
Dim StrFile As String
StrFile = Dir(Cells(1, "A").Value & "\*.docx")
Do While Len(StrFile) > 0
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(Cells(1, "A").Value & "\" & StrFile, False, True)
objDoc.SaveAs Cells(1, "A").Value & "\" & StrFile & ".txt", wdFormatText
objWord.Quit
StrFile = Dir
Loop