Drag file into Access, how to check the file details? - vba

I like to drag a file from Windows Explorer or an attachment from an Outlook mail into MS-Access.
I discovered already I can use the Access BoundObjectFrame (https://msdn.microsoft.com/en-us/library/office/ff835725.aspx) as a target to drag and drop files from the Windows Explorer.
And with the following code I see that something was dropped into the field:
Private Sub OLE1_GotFocus()
Debug.Print "OLE1_GotFocus()"
Debug.Print " OLE1.Value: " & OLE1.Value
End Sub
But the value is just some binary information.
I want to know the file name which was dropped or I want to read what is in the dropped file (i.e. a text file is dropped).
I looked at all the properties and searched on the internet but I did not find a solution. I would have guessed many people tried before what I want to do.
Any ideas?

I don't think the BoundObjectFrame will get you what you want.
I suggest using a ListView Control instead, an ActiveX control. It has inbuilt Drag&Drop support.
Demo:
On a form, insert a Microsoft ListView Control, version 6.0 ActiveX control.
Name it lvwDD.
In right-click, ListViewCtrl-object, Properties: set OLEDropMode to 1 - ccOLEDropManual.
Insert this event procedure:
Private Sub lvwDD_OLEDragDrop(Data As Object, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
Dim i As Long
' https://msdn.microsoft.com/en-us/library/aa244109(v=vs.60).aspx
Const vbCFFiles = 15
If Data.GetFormat(vbCFFiles) Then
' https://msdn.microsoft.com/en-us/library/aa267471(v=vs.60).aspx
For i = 1 To Data.Files.Count
Debug.Print Data.Files(i)
Next i
Else
Debug.Print "No file(s) dropped."
End If
End Sub
Drag&Drop one or multiple files on the control, and see the output in the Immediate window (Ctrl+G).

Related

Word Save As Default and Iteration

I have tried many different ways of doing this but I can't find one that works for the application.
I have a MS Word Template that will be used by another team.
This is the criteria I have been given.
The initial file name must be 'PL' & the right most figures of a text control box. It should also say Issue 01.
The user must be able to chose the save location.
If the user then opens the document at a later date and saves it should retain the name and path.
If the user 'saves as' then it should up issue the Issue number.
The closest I can get to this working is this: -
In the top section I have this: -
Private WithEvents App As Word.Application and Dim n as long
Then in the Document New I have: -
Private Sub Document_New()
Set App = Word.Application
n = 0
End Sub
Then for the execution I have done this: -
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
ActiveDocument.SaveAs2 "PL" & Right(ActiveDocument.SelectContentControlsByTitle("Works Order Number").Item(1).Range.Text, 5) & " Issue " & Format(n, "00") & ".docx", wdFormatDocumentDefault
End Sub
However, this saves before the user has chosen a location. It works but the user needs to chose the location. So I tried this, this just does the same thing.
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If Cancel = False Then
n = n + 1
ActiveDocument.SaveAs2 "PL" & Right(ActiveDocument.SelectContentControlsByTitle("Works Order Number").Item(1).Range.Text, 5) & " Issue " & Format(n, "00") & ".docx", wdFormatDocumentDefault
ElseIf Cancel = True Then
Exit Sub
End If
End Sub
Any suggestions or help would very much be welcomed. I basically just want to suggest the filename which up issues with every save as. But I cant find a way to influence that without physically saving.
Thank you in advance for your time and support.
You can repurpose ribbon controls (Save) to call your event handler first. Moreover, if required, you may cancel the default action in the event handler. See Temporarily Repurpose Commands on the Office Fluent Ribbon for more information. Repurposing ribbon controls give you a big plus - corresponding keyboard shortcuts are handled by your code as well. So, you will be able to intercept keyboard shortcuts by the same code.
In case of Backstage UI you can hide the SaveAs button and add your own. Read more about the Backstage UI in the following articles:
Introduction to the Office 2010 Backstage View for Developers
Customizing the Layout of Columns in the Office 2010 Backstage View
You really need to learn how to use the online documentation for VBA. If you had looked up DocumentBeforeSave you would have learned that Cancel is always False when the event is triggered. If you set it to True in the event handler it cancels the save.
You need to intercept the save before the dialog has been displayed, but the event is only triggered after the dialog. Because the criteria for Save is to use the standard functionality it is only the FileSaveAs you need to intercept.
Prior to the implementation of the Backstage view (the File tab) this could be solved simply by creating a routine named FileSaveAs. You can still do this and it will intercept the keyboard shortcut or clicking the QAT button. But it will not intercept the backstage commands. Only the event can do that without rebuilding the Backstage view, and the event won't work for you...
As far as intercepting the dialog to set the initial file name, that has been covered on SO before, here for example.

Disable Save As but not Save in Word 2010

I am looking to disable Save As in a Word 2010 file but still allow save. In other words I want users to be able to update the existing file but not create copies. I realize that this is impossible to truly do for people who know workarounds but for the general user I have successfully done this in Excel but am pretty new to word VBA.
When I add the following to a brand new document everything works as intended:
Sub FileSaveAs()
MsgBox "Copies of this file cannot be created. Please save changes in the original document." & _
, , "Copy Cannot be Created"
End Sub
My document has other macros for various command buttons but none of them involve saving the document (under original name or save as). There is also a macro running on open but that is 1 line going to a bookmark. When I try to "save as" in this document I get the message box as intended. When I try to "save" though things get strange: I get the save as dialogue (problem 1). Whether I try to save either under same name or other name the dialogue behaves as it normally would except it doesn't save and the dialogue box opens again automatically essentially creating an endless loop until I hit cancel (problem 2). I also intermittently get a "disk is full" warning pop-up after trying to save which I can dismiss but appears a few minutes later as long as he file is open (perhaps related to autosave?)
Since the macro works in the test file I assumed this strange behavior must be something elsewhere in my code but my document with the other macros saves normally as long as I don't include the save as code above so now I'm totally confused. Before I put up the rest of my code which is lengthy and for the reasons stated above I would not think impact things, I figured I'd ask this:
1. Is there any place other than my other command button macros that could be causing this behavior?
2. Is there a better method people recommend to achieve my ultimate goal of disabling save as but not save?
Thanks in advance for any advice you can provide.
The Word application has a DocumentBeforeSave event. To enable application events I suggest to create a class module by the name of ThisApplication and paste the following code into it.
Option Explicit
Private WithEvents App As Application
Private Sub Class_Initialize()
Set App = Word.Application
End Sub
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, _
SaveAsUI As Boolean, _
Cancel As Boolean)
If SaveAsUI Then
MsgBox "Please always use the ""Save"" command" & vbCr & _
"to save this file.", _
vbExclamation, "SaveAs is not allowed"
Cancel = True
End If
End Sub
Add the following code to your ThisDocument module.
Dim WdApp As ThisApplication
Private Sub Document_Open()
Set WdApp = New ThisApplication
End Sub
You may add the Set App = ... line to your existing Document_Open procedure. After the WdApp variable has been initialised all application events will be received by the ThisApplication class where the DocumentBeforeSave event procedure is programmed not to allow SaveAs.
Of course, this is a blanket refusal for all documents. Therefore you may wish to add code to the procedure to limit the restriction to certain documents only. The proc receives the entire document object with all its properties, including Name, Path, FullName and built-in as well as custom properties. You can identify the files you wish to be affected by any of these.
Note that the WdApp variable will be erased in case of a program crash. If this happens the application events will no longer fire. It may be useful to know that application events occur before document events. This is if you wish to use the application's DocumentOpen event as well as or instead of the document's Document_Open event.

Remove a VBA Project Reference

In VBA I can see three different references for PDFCreator. One of them (see the second image) is a version of the software stored locally, and it works. I'd like to use this reference.
The other two are references to versions stored on a server, and they're broken (at this stage, I don't have permission to reinstall or delete them).
My problem is that after selecting the desired reference (see the second image) and clicking 'Ok', it resets to an incorrect reference, as shown in the third image.
How can I either override whatever's going on and select the desired reference or remove the incorrect references? While I'm not able to uninstall these versions from the server, I see no reason that my Excel would need to reference them. Can they just be removed from the list?
Image 1: Default state of the VBA Project References (PDFCreator not selected)
Image 2: Selecting the correct PDFCreator version
Image 3: Re-opening the menu shows that the incorrect PDFCreator version is selected
You might be able to something like the following...
To Remove broken references:
Private Sub RemoveBrokenReferences(ByVal Book As Workbook)
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Dim oRefS As Object, oRef As Object
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Set oRefS = Book.VBProject.References
For Each oRef In oRefS
If oRef.IsBroken Then
Call oRefS.Remove(oRef)
End If
Next
End Sub
To Remove a specific reference:
Use something like:
Call ActiveWorkbook.VBProject.References.Remove(oReference)
and you can get the oReference from:
Private Function GetReferenceFromPath(ByVal FilePathName As String) As Object
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Dim oFs As Object, oReferenceS As Object, oReference As Object
Dim sFileName As String, sRefFileName As String
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Set oFs = Interaction.CreateObject("Scripting.FileSystemObject")
sFileName = oFs.GetFileName(FilePathName)
Set oReferenceS = ActiveWorkbook.VBProject.References
For Each oReference In oReferenceS
sRefFileName = oFs.GetFileName(oReference.FullPath)
If StrComp(sFileName, sRefFileName, vbTextCompare) = 0 Then
Set GetReferenceFromPath = oReference
End If
Next
End Function
Public Sub RemoveReference()
On Error GoTo EH
Dim RefName As String
Dim ref As Reference
RefName = "Selenium"
Set ref = ThisWorkbook.VBProject.References(RefName)
ThisWorkbook.VBProject.References.Remove ref
Exit Sub
EH:
'If an error was encountered, inform the user
Select Case Err.Number
Case Is = 9
MsgBox "The reference is already removed"
Exit Sub
Case Is = 1004
MsgBox "You probably do not have to have Trust Access To Visual Basic Project checked or macros enabled"
Exit Sub
Case Else
'An unknown error was encountered
MsgBox "Error in 'RemoveReference'" & vbCrLf & vbCrLf & Err.Description
End Select
End Sub
P.S It is not possible to remove A MISSING/ broken references programmatically after MISSING occurs, only before it happens or manually after it happens. Most cases of MISSING/ broken references are caused because the type library has never before been registered on that system.
See How to Remove Reference programmatically?
I had a broken reference problem with a large number of Excel spreadsheets when I uninstalled Flash (which for some unknown reason I had included as a reference).
I got round the problem as follows:
BE CAREFUL BECAUSE THIS INVOLVES A REGISTRY HACK AN IS COMPLICATED.
MAKE A BACKUP OF REGISTRY BEFORE HACKING.
I wrote VBA code to find the Guid of the broken reference.
I used Regedit to insert a DUMMY TypeLib entry as follows:
D27CDB6B-AE6D-11CF-96B8-444553540000 was the Guid of the Broken Reference.
HKEY_CLASSES_ROOT\TypeLib{D27CDB6B-AE6D-11CF-96B8-444553540000}
HKEY_CLASSES_ROOT\TypeLib{D27CDB6B-AE6D-11CF-96B8-444553540000}\1.0 Adobe Acrobat 7.0 Browser Control Type Library 1.0
HKEY_CLASSES_ROOT\TypeLib{D27CDB6B-AE6D-11CF-96B8-444553540000}\1.0\0
HKEY_CLASSES_ROOT\TypeLib{D27CDB6B-AE6D-11CF-96B8-444553540000}\1.0\0\win32 C:\Program Files (x86)\Common Files\Adobe\Acrobat\ActiveX\AcroPDF.dll
HKEY_CLASSES_ROOT\TypeLib{D27CDB6B-AE6D-11CF-96B8-444553540000}\1.0\FLAGS 0
HKEY_CLASSES_ROOT\TypeLib{D27CDB6B-AE6D-11CF-96B8-444553540000}\1.0\HELPDIR C:\Program Files (x86)\Common Files\Adobe\Acrobat\ActiveX\
I based the above on another TypeLib entry.
Then I wrote VBA code to read each Reference.Guid in turn and if the Guid matched {D27CDB6B-AE6D-11CF-96B8-444553540000} to remove the reference using References.Remove Reference.
Code for doing this is available all over the forums so I won't repeat here.
After modifying all the affected Workbooks I reinstated the saved registry.
Hope this works for you.
BE CAREFUL BECAUSE THIS INVOLVES A REGISTRY HACK AN IS COMPLICATED.
MAKE A BACKUP OF REGISTRY BEFORE HACKING.

VBA - How to set focus on OLE object window (IBM DOORS)

I'm writing a script in the VB to be used in excel, I'm using OLE to run a DXL script in DOORS. The DOORS script creates a popup window, which I'd like to give focus to when it's created.
Currently I have reference to the DOORS object, but I can't seem to find out how to set focus to the window. It might be that it's something that I have to do in the dxl, but I was wondering if there's a way to do this on the VB side of things.
So far I have:
Public Sub DoThing()
Const DxlFilepath As String = "C:\FilePath"
Dim DOORSObj As Object
'Double check the user wants to do this
vbCreateList = MsgBox("Current list will be lost. Please confirm to proceed? (Note: Parent folder must be selected in DOORS popup)", vbOKCancel, "Do thing")
If (vbCreateList = vbCancel) Then
Exit Sub
End If
'Get access to the DOORS application database
Set DOORSObj = CreateObject("DOORS.Application")
DOORSObj.result = "OK"
DOORSObj.runFile (DxlFilepath)
End Sub
Thanks in advance

Access autocad object properties without opening it by VBA

I have been using folder browser for VBA, I could paste the code of it, but bottom line is that I get returned file name as a string.
Is there any way to access drawing properties (i.e number of layouts) without open?
Public Sub TestFileDialog()
dwgname = FileBrowseOpen("C:", "*", ".dwg", 1) 'dwgname is typeof string
End Sub
Its only the first step (use of FileBrowseOpen function is shown, but also i can use FolderBrowse and collect all .dwg inside of folder),actually i had in mind to batch export all layouts of selected .dwgs to currenty open one. Is there any chance for that?
To effectively read a .dwg file you'll need to open AutoCAD, otherwise the information is not accessible. Some properties may be, such as author, but not number of layouts...
But you can use AutoCAD Console (accoreconsole.exe) to run a headless AutoCAD and use APIs to read any information you need. This is really fast for reading lot's of files and the user will not see it running (but it needs to be installed anyway).
http://aucache.autodesk.com/au2012/sessionsFiles/3338/3323/handout_3338_CP3338-Handout.pdf
you could stay in VBA and use ObjectDBX
it leads to a very similar approach as accoreconsole.exe on in .NET does, i.e you won't see any drawing open in UI since it works on the database itself
It requires adding library reference (Tools->References) to "AutoCAD/ObjectDBX Common XX.Y Type Library", where "XX.Y" is "19.0" for AutoCAD 2014
a minimal functioning code is
Sub main()
Dim myAxDbDoc As AxDbDocument
Dim FullFileName As String
FullFileName = "C:\..\mydrawing.dwg" '<== put here the full name of the file to be opened
Set myAxDbDoc = AxDb_SetDrawing(FullFileName)
MsgBox myAxDbDoc.Layers.Count
End Sub
Function AxDb_SetDrawing(FullFileName As String) As AxDbDocument
Dim DBXDoc As AxDbDocument
Set DBXDoc = Application.GetInterfaceObject("ObjectDBX.AxDbDocument.19") '<== place correct AutoCAD version numeber ("19" works for AutoCAD 2014)
On Error Resume Next
DBXDoc.Open FullFileName
If Err <> 0 Then
MsgBox "Couldn't open" & vbCrLf & vbCrLf & FullFileName, vbOKOnly + vbCritical, "AxDB_SetDrawing"
Else
Set AxDb_SetDrawing = DBXDoc
End If
On Error GoTo 0
End Function
Still, you must have one AutoCAD session running from which make this sub run! But you should have it since talked about "currently open" drawing