Libreoffice Multiple Backups? - backup

Is there a way, setting, macro, or otherwise, that can automatically create backups of the current document in a series? Such as, working on a Writer document, pressing a macro button, and creating a backup at that time, so that there is another backup added to the previous backups in a folder?

Well, try this
Sub createBackUp()
Dim sURL As String
Dim aURL As Variant
Dim saveTime As String
sURL = ThisComponent.getURL()
If Trim(sURL) = "" Then Exit Sub ' No name - cannot store
saveTime = "_" & FORMAT(Now,"YYYYMMDD\_HHmmSS")
aURL = Split(sURL, ".")
If UBound(aURL) < 1 Then ' No extention?
sURL = sURL & saveTime
Else
aURL(UBound(aURL)-1) = aURL(UBound(aURL)-1) & saveTime
sURL = Join(aURL,".")
EndIf
On Error Resume Next
ThisComponent.storeToURL(sURL,Array())
On Error GOTO 0
End Sub
Also you can try Timestamp Backup

Related

Create email, save to draft after attachment has uploaded

The following code loops through a folder, creates email with attachment and saves to draft.
Public Sub saveFileTodownload()
Set draftItems = Outlook.Session.Folders("My Email").Folders("Drafts").Items
strFile = Dir("d:\ga\localsdk\")
Do While Len(strFile)
Debug.Print strFile
Set mail = draftItems.Add("IPM.NOTE")
mail.Subject = "1downloadme" & Count
mail.Attachments.Add ("d:\ga\localsdk\" & strFile)
mail.Save
strFile = Dir
Loop
End Sub
I need to wait until the attachment has completely uploaded then call Save.
Is there any way to wait until the attachment has uploaded.
Note: If I debug it and wait a minute after the Attachment.Add line, the attachment has uploaded and Save works.
MailItem.Attachments.Add is synchronous; by the time the call returns, the attachment is added to the message. What makes you believe this is not so?
user93865
I tested your code and it works for me(No error message). However, it will be gets stuck if there are lots of attachments upload.
You could try this code :
Public Sub saveFileTodownload()
Dim store As Outlook.store
Dim space As Outlook.NameSpace
Dim folder As Outlook.folder
Set space = Outlook.Application.GetNamespace("MAPI")
Set store = space.DefaultStore
Set folder = store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts)
Set Item = folder.Items.Add("IPM.NOTE")
Item.Subject = "1downloadme"
Item.Body = "one"
Dim r As Long
r = 0
Do While r < 100
Item.Attachments.Add ("C:\Users\temp\Desktop\notes\abd.docx")
r = r + 1
Loop
'Item.Display
Item.Save
Item.Close
End Sub

VBA - Unable to map drive to sharepoint on another computer

I'm mapping to the company's sharepoint drive using VBA. The intention is to save local file to sharepoint, and delete local file and unmapped the drive after success.
On my machine(Windows 10 64bits), the code works perfectly fine, successfully mapped the drive, created folder and file, successfully uploaded to sharepoint and unmap the drive.
However, when I run the same excel workbook that contains the same code on my colleague's computer(Window 7), it failed. There's no error being shown, except that it keeps on loading and loading until Excel Not Responsive. I tried manually mapping the drive, it success.
I tried to debug and found out that the code stops (keeps on loading) at MsgBox "Hello" but could not figure out what's missing.
Both are using Excel 2016
Any help and suggestions are appreciated. let me know if more info is needed. Thanks in advance.
This is my vba code
Sub imgClicked()
Dim fileName As String
Dim SharePointLib As String
Dim MyPath As String
Dim folderPath As String
Dim objNet As Object
Dim copyPath As String
Dim copyFilePath As String
folderPath = Application.ThisWorkbook.path
MyPath = Application.ThisWorkbook.FullName
Dim objFSO As Object
Dim strMappedDriveLetter As String
Dim strPath As String
Dim spPath As String
strPath = "https://company.com/sites/test/test 123/" 'example path
spPath = AvailableDriveLetter + ":\test.xlsm" 'example path
copyPath = folderPath + "\copyPath\"
'Add reference if missing
Call AddReference
Set objFSO = CreateObject("Scripting.FileSystemObject")
With objFSO
strMappedDriveLetter = IsAlreadyMapped(.GetParentFolderName(strPath))
If Not Len(strMappedDriveLetter) > 0 Then
strMappedDriveLetter = AvailableDriveLetter
If Not MapDrive(strMappedDriveLetter, .GetParentFolderName(strPath)) Then
MsgBox "Failed to map SharePoint directory", vbInformation, "Drive Mapping Failure"
Exit Sub
End If
End If
' Check file/folder path If statement here
End With
Set objFSO = Nothing
End Sub
Code for getting available drive
' Returns the available drive letter starting from Z
Public Function AvailableDriveLetter() As String
' Returns the last available (unmapped) drive letter, working backwards from Z:
Dim objFSO As Object
Dim i As Long
Set objFSO = CreateObject("Scripting.FileSystemObject")
For i = Asc("Z") To Asc("A") Step -1
Select Case objFSO.DriveExists(Chr(i))
Case True
Case False
Select Case Chr(i)
Case "C", "D" ' Not actually necessary - .DriveExists should return True anyway...
Case Else
AvailableDriveLetter = Chr(i)
Exit For
End Select
End Select
Next i
Set objFSO = Nothing
MsgBox "This is the next available drive: " + AvailableDriveLetter ' returns Z drive
MsgBox "Hello" ' After this msgBox, starts loading until Not Responsive
End Function
Function to Map drive
Public Function MapDrive(strDriveLetter As String, strDrivePath As String) As Boolean
Dim objNetwork As Object
If Len(IsAlreadyMapped(strDrivePath)) > 0 Then Exit Function
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter & ":", strDrivePath, False
MapDrive = True
MsgBox "Successfully Created the Drive!"
Set objNetwork = Nothing
End Function
Code for MappedDrive
Public Function GetMappedDrives() As Variant
' Returns a 2-D array of (1) drive letters and (2) network paths of all mapped drives on the users machine
Dim objFSO As Object
Dim objDrive As Object
Dim arrMappedDrives() As Variant
Dim i As Long
Set objFSO = CreateObject("Scripting.FileSystemObject")
ReDim arrMappedDrives(1 To 2, 1 To 1)
For i = Asc("A") To Asc("Z")
If objFSO.DriveExists(Chr(i)) Then
Set objDrive = objFSO.GetDrive(Chr(i))
If Not IsEmpty(arrMappedDrives(1, UBound(arrMappedDrives, 2))) Then
ReDim Preserve arrMappedDrives(1 To 2, 1 To UBound(arrMappedDrives, 2) + 1)
End If
arrMappedDrives(1, UBound(arrMappedDrives, 2)) = Chr(i) ' Could also use objDrive.DriveLetter...
arrMappedDrives(2, UBound(arrMappedDrives, 2)) = objDrive.ShareName
End If
Next i
GetMappedDrives = arrMappedDrives
Set objDrive = Nothing
Set objFSO = Nothing
End Function
Public Function IsAlreadyMapped(strPath As String) As String
' Tests if a given network path is already mapped on the users machine
' (Returns corresponding drive letter or ZLS if not found)
Dim strMappedDrives() As Variant
Dim i As Long
strMappedDrives = GetMappedDrives
For i = LBound(strMappedDrives, 2) To UBound(strMappedDrives, 2)
If LCase(strMappedDrives(2, i)) Like LCase(strPath) Then
IsAlreadyMapped = strMappedDrives(1, i)
Exit For
End If
Next i
Set objNetwork = Nothing
End Function
Add Reference
Sub AddReference()
'Macro purpose: To add a reference to the project using the GUID for the
'reference library
Dim strGUID As String, theRef As Variant, i As Long
'Update the GUID you need below.
strGUID = "{420B2830-E718-11CF-893D-00A0C9054228}"
'Set to continue in case of error
On Error Resume Next
'Remove any missing references
For i = ThisWorkbook.VBProject.References.Count To 1 Step -1
Set theRef = ThisWorkbook.VBProject.References.Item(i)
If theRef.isbroken = True Then
ThisWorkbook.VBProject.References.Remove theRef
End If
Next i
'Clear any errors so that error trapping for GUID additions can be evaluated
Err.Clear
'Add the reference
ThisWorkbook.VBProject.References.AddFromGuid _
GUID:=strGUID, Major:=1, Minor:=0
'If an error was encountered, inform the user
Select Case Err.Number
Case Is = 32813
'Reference already in use. No action necessary
Case Is = vbNullString
'Reference added without issue
Case Else
'An unknown error was encountered, so alert the user
MsgBox "A problem was encountered trying to" & vbNewLine _
& "add or remove a reference in this file" & vbNewLine & "Please check the " _
& "references in your VBA project!", vbCritical + vbOKOnly, "Error!"
End Select
On Error GoTo 0
End Sub
Procedure imgClicked is calling function AvailableDriveLetter multiple times. Remember that the function has to execute each time you refer to it.
I ran imgClicked (assuming that's the procedure you start with) and I was told, twice, "Next available letter = Z" and "Hello" and then it crashed Excel (perhaps getting stuck in a loop of creating FileSystem objects to look for an available drive letter?)
Try assigning AvailableDriveLetter to a variable (string) at the beginning of the procedure and referring to the variable each time you need the value, and see if you still have the issue.
(Remember to save before execution -- I get frustrated when troubleshooting "application hanging" issues because I keep forgetting to save my changes and then lose them on the crash!)
If this doesn't work, add a breakpoint (F9) on the End Function line after your "Hello" box and see if the code stops there. (I have trouble believing the MsgBox or End Function are the culprit.) If not, which procedure runs after that?
One more thing whether the issue is resolved or not:
Add Option Explicit at the very beginning of your module and then Compile the project and fix your missing variable declaration(s).
This is recommended whenever troubleshooting an issue as a means to eliminate variable declaration issues as a possible cause.

Creating and opening a Word Document using Access 2007 VBA

Can anyone point to what I'm doing wrong. I'm trying to output an Access 2007 report to Word (using .rtf!) but I can't seem to then point my code at the file. I keep getting Type Mismatch errors.
Here is my code:
Private Sub CatalogTitle_Click()
Dim AppWord As Object
Dim Doc As Object
Dim FileName As String
Dim DateTime As Date
Dim DTString As String
Dim x As Integer
Set AppWord = CreateObject(Class:="Word.Application") ' create an instance of Word
DateTime = Now() ' build a date string which is compatible with the Windows File structure
DTString = CStr(DateTime)
For x = 1 To Len(DTString) ' changes dd/mm/yy hh:mm:ss into dd_mm_yy hh_mm_ss
If (Mid(DTString, x, 1) = "/" Or Mid(DTString, x, 1) = ":") Then
Mid(DTString, x, 1) = "_"
End If
Next x
' build the full .rtf filename
FileName = "Titles " & DTString & ".rtf"
' and output the rtf file to it
DoCmd.OutputTo acReport, "ItemsReportByTitle", acFormatRTF, FileName
' then open the Word instance with the file just created
Set Doc = "AppWord.Documents.Open(FileName)"
' and make the instance visible
AppWord.Visible = True
End Sub
I keep failing on the 'Set Doc...' line.
Remove the double quotes around the object/method...
Set Doc = AppWord.Documents.Open(FileName)

SQL "%" equivalent in VBA

Is there any SQL equivalent of "%" sign in VBA?
I need to return a few files just with some characters in the middle.
Help really appreciated!
For instance here is my code: I need to download all file that has in the name 2013 from that webpage and save and call them differently. Is this mission possible?
Sub Sample()
Dim strURL As String
Dim strPath As String
Dim i As Integer
strURL = "http://cetatenie.just.ro/wp-content/uploads/Ordin-********.2013.pdf"
strPath = "C:\Documents and Settings\ee28118\Desktop\178.pdf"
Ret = URLDownloadToFile(0, strURL, strPath, 0, 0)
If Ret = 0 Then
MsgBox "File successfully downloaded"
Else
MsgBox "Unable to download the file"
End If
End Sub
You can use the Like Operator.
Characters in pattern Matches in string
? Any single character.
* Zero or more characters.
# Any single digit (0–9).
[charlist] Any single character in charlist.
[!charlist] Any single character not in charlist
Example :
Dim MyCheck
MyCheck = "aBBBa" Like "a*a" ' Returns True.
MyCheck = "F" Like "[A-Z]" ' Returns True.
MyCheck = "F" Like "[!A-Z]" ' Returns False.
MyCheck = "a2a" Like "a#a" ' Returns True.
MyCheck = "aM5b" Like "a[L-P]#[!c-e]" ' Returns True.
MyCheck = "BAT123khg" Like "B?T*" ' Returns True.
MyCheck = "CAT123khg" Like "B?T*" ' Returns False.
When you navigate to the uploads folder, you get a directory listing of all the files in it. You can loop through the hyperlinks on that listing and test each to see if it meets your criterion and, if so, download it. You need a reference to MSXML and MSHTML. Here's an example.
Sub Sample()
Dim sUrl As String
Dim xHttp As MSXML2.XMLHTTP
Dim hDoc As MSHTML.HTMLDocument
Dim hAnchor As MSHTML.HTMLAnchorElement
Dim Ret As Long
Dim sPath As String
Dim i As Long
sPath = "C:\Documents and Settings\ee28118\Desktop\"
sUrl = "http://cetatenie.just.ro/wp-content/uploads/"
'Get the directory listing
Set xHttp = New MSXML2.XMLHTTP
xHttp.Open "GET", sUrl
xHttp.send
'Wait for the page to load
Do Until xHttp.readyState = 4
DoEvents
Loop
'Put the page in an HTML document
Set hDoc = New MSHTML.HTMLDocument
hDoc.body.innerHTML = xHttp.responseText
'Loop through the hyperlinks on the directory listing
For i = 0 To hDoc.getElementsByTagName("a").Length - 1
Set hAnchor = hDoc.getElementsByTagName("a").Item(i)
'test the pathname to see if it matches your pattern
If hAnchor.pathname Like "Ordin-*.2013.pdf" Then
Ret = UrlDownloadToFile(0, sUrl & hAnchor.pathname, sPath, 0, 0)
If Ret = 0 Then
Debug.Print sUrl & hAnchor.pathname & " downloaded to " & sPath
Else
Debug.Print sUrl & hAnchor.pathname & " not downloaded"
End If
End If
Next i
End Sub
Edit
I assumed that URLDownloadToFile was already written. I didn't write one, I just used the below function to test the code that iterates through the files. You can use it to make sure the above code works for you, but you'll need to write the actual code to download the file eventually. With all the arguments to URLDownloadToFile, I'm surprised it doesn't exist already.
Function UrlDownloadToFile(lNum As Long, sUrl As String, sPath As String, lNum1 As Long, lNum2 As Long) As Long
UrlDownloadToFile = 0
End Function
Try below code : The boolean function would return true if the string has the string 2013 in it.
Sub Sample()
Dim result As Boolean
result = has2013("http://cetatenie.just.ro/wp-content/uploads/Ordin-********.2013.pdf")
Debug.Print result
result = has2013("http://cetatenie.just.ro/wp-content/uploads/Ordin-********.2014.pdf")
Debug.Print result
End Sub
Function has2013(lnk As String) As Boolean
has2013 = lnk Like "*2013*"
End Function
in VBA use the LIKE function with wildcard characters:
here is an example (copied from Ozgrid Forums)
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
If sht.Name Like "FRI*" Then
'Add code for Friday sheets
Else
If sht.Name Like "MON*" Then
'Add code for Monday sheets
End If
End If
Next
The multiplication character * takes the place of zero or more characters, whereas ? takes the place of exactly 1 character, and # takes the place of 1 number. There are other more specific char. matching strategies if you only want to match certain characters.
so there you go!
Also, you could take a look at Ozgrid Forums: Using Regular Expressions in VBA
To get a list of the files on the server, read up on FTP (using DIR) at Mr Excel - List files using FTP

Import Lotus Notes Database into Access/SQL - What about Documents?

I have a few Lotus Notes 'databases' that i'd like to import into Access or SQL.
I think I have most of the steps down (install NotesSQL ODBC driver, setup ODBC connection to Lotus DB, import data into Access), but I can't figure out what to do with all the documents, eg: Word Files, PDF Docs, Excel Workbooks that were in the Lotus DB.
The Lotus Notes DB is full of them. After importing, I do notice a table in Access called 'Documents,' but I don't know what to do with it. I see a line/record for each document in the Lotus DB, but it's not like SQL where there is a column for the actual file data.
Please let me know how I can actually use the documents I pull out of the Lotus DB.
Your best bet is to extract the documents from the database and store them on a file share. That will give you the most flexibility. To retain the association with the original Notes documents, you may want to export them with filenames or into folders with folder names that include the ID of the associated record in Access. Or at least make sure the records include the path of the document.
I don't believe you can pull in attachments via the NotesSQL driver.
Here's an example script that you can put into an Agent to extract attachments from your database: (from http://www.notes411.com/dominosource/tips.nsf/0/4F1FF33C52F08D76802570C2003A2FD6!opendocument)
Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim collection As NotesDocumentCollection
Dim doc As NotesDocument
Set db = session.CurrentDatabase
Set collection = db.UnprocessedDocuments
Set doc = collection.GetFirstDocument()
While Not(doc Is Nothing)
Call extractMyAttachment( doc )
Set doc = collection.GetNextDocument(doc)
Wend
End Sub
Function extractMyAttachment (doc)
Dim emb As Variant
Dim nid As String
nid = doc.NoteID
Dim rtitem As Variant
Set rtitem = doc.GetFirstItem( "Body" )
Dim pathName As String, fileName As String, mydir As String,
newfilename As String
mydir = "Coda"
pathName$ = "P:\" & mydir
fileName$ = Dir$(pathName$, 16)
Dim boxType As Long, answer As Integer
boxType& = 36
If fileName$ = "" Then
answer% = Messagebox("Directory "& pathName$ &" does not exist,
would you like to create it ?", boxType&, "Create" & mydir & " on P:\ ?")
If answer% = 6 Then
Mkdir pathname$
fileName$ = Dir$(pathName$, 16)
If filename$ <> "" Then
If ( rtitem.Type = RICHTEXT ) Then
Forall o In rtitem.EmbeddedObjects
If ( o.Type = EMBED_ATTACHMENT ) Then
newfilename$ = pathname$ & "\" &
o.source
Call o.ExtractFile (newfilename$
)
End If
End Forall
End If
End If
End If
Else
If ( rtitem.Type = RICHTEXT ) Then
Forall o In rtitem.EmbeddedObjects
If ( o.Type = EMBED_ATTACHMENT ) Then
newfilename$ = pathname$ & "\" & o.source
fileName$ = Dir$(NewFileName$, 0)
If fileName$ <> "" Then
answer% = Messagebox("File "&
NewFileName$ &" already exists, would you like to overwirite it ?",
boxType&, "Overwrite" & NewFileName$ & " ?")
If answer% = 6 Then
Call o.ExtractFile (newfilename$
)
End If
Else
Call o.ExtractFile (newfilename$ )
End If
End If
End Forall
End If
End If
End Sub