Visual Basic 6.0 FileListBox - vba

How do I show only specific file types in the file list box? For example, show all .docx files from the whole computer directories. I would also want people to choose from the files listed in the "File list box" and open it through a command button.

The FileListBox has "Pattern" property. Enter *.docx there.
Also: add a DirListBox and a DriveListBox too.
Private Sub Drive1_Change()
Dir1.Path=Drive1.Drive
End Sub
Private Sub Dir1_Change()
File1.Path=Dir1.Path
End Sub

Related

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.

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

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).

How to get the path of selected folder in vb .net?

I have created a small application in vb .net to load all the files present inside the current folder where my application is running. I want to customize this application in such a manner that, when the user right click on any folder my application name also should appear in that menu. When the user click's that option, all the files present inside that corresponding folder should get listed.
I have achieved this partially. I have added my application to the right click menu item of all folders. But when I click my application name, all the files present in the parent directory is listed. I want to customize my coding to receive the path of the selected folder. So, how to do that?
There's a whole tutorial on MSDN: Verbs and File Associations
For example, the open verb normally launches a program to open a file. The command string typically looks as follows:
"My Program.exe" "%1"
You can get the parameters by the code below:
Public Sub Main(ByVal cmdArgs() As String)
If cmdArgs.Length > 0 Then
'Process the command.
'cmdArgs(0) is program name
'cmdArgs(1) is the path of your folder / file.
End If
End Sub
For Windows Forms, please see this .You'll need a function like below:
Public Sub Main(ByVal cmdArgs() As String)
'Process cmdArgs here (same as above)
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1)
End Sub

How do I extract from a password protected zip file using VBA?

I have been struggling with this problem for quite some time now, and have trawled through various forums trying to find an answer.
I have a mailbox which received 5 emails each morning which have zipped, password-protected .csv files which require opening and processing in Excel. I am semi-competent in VBA so the Excel side of things is no issue, but it would save me a lot of time if I could automatically unzip these files and save them to a folder.
These emails have the same subjects, attachments and passwords each day, and are from the same sender to the same mailbox. I have code which can automatically process and save the .zip files to a location, but I am still stuck with the problem of having to go into each one, enter the password, open the file, save it, etc.
I have refrained from attaching this code because I would like to see if there are better solutions to my one, and it is the unzipping and password entering that I really need help with. This being said if you would like me to attach my code I will be happy to do so :)
To expand my comment, no input is required - the password is obtained via an event so can be provided in code.
Download the demo project and DLL
In the outlook VBA editor, right click the Project in the tree, Import File and add
cUnzip.cls
mUnzip.bas
Put the DLL in \System32 (or \SysWoW64 on 64bit)
As a naive example, add a new class cWhatever:
Private WithEvents Unzip As cUnzip
Private Sub Class_Initialize()
Set Unzip = New cUnzip
End Sub
Sub RunUnzip()
With Unzip
.ZipFile = "c:\blah\qqq.zip"
.UnzipFolder = "c:\temp"
.Unzip
End With
End Sub
Private Sub Unzip_PasswordRequest(sPassword As String, bCancel As Boolean)
sPassword = "password123"
End Sub
Then to run from elsewhere:
Dim x As New cWhatever
x.RunUnzip
First time you run there is an error highlighting App.EXEName, just delete the App.EXEName &

Get file from "Windows Explorer - Open With"

I'm not quite sure where to start with this. On right-clicking on a generic file in Windows Explorer (e.g. *.doc for a Word document) one can choose "Open with...". I 'd like to know how the program knows what file has been "passed" (is that the right word?). Is it done via arguments? How can I implement this in my own application?
I tried manually adding a file path to the arguments of one of my applications when it is run, but the path includes spaces (which denotes a new argument). How does Windows get round this/what do I need to do to solve this?
Regards,
Robbie
To retrieve the arguments used from the command line:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim sMsg As String = ""
For Each sArg As String In My.Application.CommandLineArgs
sMsg &= sArg & ": "
Next
MsgBox(sMsg)
End Sub
Place the code in the WinForm _Load, Console Main, etc.
If the above was run with: Hello World as the command line Hello: World: would display.
Here is some VB code to open a file:
Case Keys.F4
Process.Start("H:\OIS\PROCEDUR\OIS8ProcedureManual.doc")
In this case Windows looks up .doc in the file types and uses the .doc entry to run Word and pass it the filename.
Process.Start has a second parameter that contains Arguments so you could provide a path to an .exe in the first param and the argument(s) in the second. Actually there are 5 signatures for Process.start. The most powerful ones uses the ProcessStartInfo class to provide you with the most control.
Post the code you wrote for the second group of questions if the above didn't help.