I have a code that tries to run an IExplorer that would load a google map. Anyways, It bugs everytime it tries to run the code where it reads the HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\" & "App Paths\IEXPLORE.EXE\
with the error: Invalid Root in registry key
Dim obj_FSO As Object
Dim obj_Shell As Object
Dim str_IEVersion As String
Dim StrArray() As String
Dim iVersion As Integer
Set obj_FSO = CreateObject("scripting.filesystemobject")
Set obj_Shell = CreateObject("wscript.shell")
str_IEVersion = obj_FSO.GetFileVersion(obj_Shell.RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\" & "App Paths\IEXPLORE.EXE\"))
Related
Our SharePoint site used to be on-prem, and it migrated to the one hosted on the cloud.
We had the following VBA code to upload a file to its document library, and it stopped working although we have the correct new URL. It doesn't throw any error, but it just doesn't upload the file. Is there another way do upload a file? Thank you very much.
' To read source files as byte
Dim binaryByte() As Byte
Dim binaryByteData As Variant
'Get lngFileLength for binarybyte initialization.
Dim lngFileLength As Long
'Initilizate the scripting object for getting folder information.
Dim objFSO As Scripting.FileSystemObject
Set objFSO = New Scripting.FileSystemObject
' Initialize the xmlhttp object
Dim LobjXML As Object
Set LobjXML = CreateObject("Microsoft.XMLHTTP")
'Set sharepoint URL from tbl_Lookup
sSharePointURL = ELookup("SharePointFolderURLPath", "tbl_Lookup")
'Get path of file to publish
sFilePath = CurrentProject.Path & "\Published"
' Get the filename
sFileNameWithPath = sFilePath & "\" & ELookup("FrontEndFileNameAccdb", "tbl_Lookup")
'Array length identification
lngFileLength = FileLen(sFileNameWithPath) - 1
'Reinitialize the byte array
ReDim binaryByte(lngFileLength)
'Open file binary consumption
Open sFileNameWithPath For Binary As #1
Get #1, , binaryByte
Close #1
' Convert to variant to upload.
binaryByteData = binaryByte
'Destination URL
sDestinationURL = sSharePointURL & ELookup("FrontEndFileNameAccdb", "tbl_Lookup")
' Upload the data to the server, false means synchronous.
LobjXML.Open "PUT", sDestinationURL, False
Set LobjXML = Nothing
Set objFSO = Nothing
I'm hoping someone is able to point me in the correct direction on this. I've spent hours trying to get my code to work without success.
I wrote a program in Visual Studio using VB.NET that would open a directory, iterate through all the files, and rename each/copy them to to a new folder. I'm now trying to get the program to read a particular attribute of each file and include that in the file name. This will either be the Media Created or Date Created attribute. It appears I can do this using the Folder.GetDetailsOf method.
The example code given by Microsoft for VB is as shown below:
Private Sub btnGetDetailsOf_Click()
Dim objShell As Shell
Dim objFolder As Folder
Set objShell = New Shell
Set objFolder = objShell.NameSpace("C:\WINDOWS")
If (Not objFolder Is Nothing) Then
Dim objFolderItem As FolderItem
Set objFolderItem = objFolder.ParseName("clock.avi")
If (Not objFolderItem Is Nothing) Then
Dim szItem As String
szItem = objFolder.GetDetailsOf(objFolderItem, 2)
End If
Set objFolderItem = Nothing
End If
Set objFolder = Nothing
Set objShell = Nothing
End Sub
However, when trying to compile I get lots of errors, including Shell, Folder, and FolderItem not being defined.
After lots of searching and reading articles I've got to this point with my code, but it throws an error when trying to set objFolderItem.
Dim di As New DirectoryInfo(c:\folder)
Dim fileArray As FileInfo() = di.GetFiles()
Dim file As FileInfo
Dim objShell As Object
Dim objFolder As Object
Dim objFolderItem As Object
objShell = CreateObject("Shell.Application")
objFolder = objShell.NameSpace(c:\folder) 'create folder object
For Each file In fileArray
objFolderItem = objFolder.ParseName(file.Name) 'create file object
MsgBox(objFolder.getdetailsof(objFolderItem, 201))
Next file
When I check the running code it appears objShell.NameSpace doesn't return anything.
Can someone advise how I can create objShell correcting in VB.NET or provide an updated version of Microsoft's example code so I can use that as a basis?
Thanks
Thanks for your help. I managed to get this working by importing Microsoft Shell Controls And Automation and using the code below:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim objShell As Object
Dim objFolder As Object
objShell = New Shell32.Shell
objFolder = objShell.NameSpace("D:\FOLDERNAME")
If (Not objFolder Is Nothing) Then
Dim objFolderItem As Object
objFolderItem = objFolder.ParseName("FILENAME")
MsgBox(objFolderItem.name & " " & objFolder.getdetailsof(objFolderItem, 208))
objFolderItem = Nothing
End If
objFolder = Nothing
objShell = Nothing
End Sub
Many thanks for your help again
That's a lot of code for a simple problem. You don't need to use the shell. To copy/move:
My.Computer.FileSystem.CopyDirectory(<SourceDir>, <DestinationDir>)
To rename a file:
My.Computer.FileSystem.RenameFile("CurrentName", "DesiredName")
To get a file's attributes:
attributes = File.GetAttributes(path)
I'm currently working on developing a macro that will input various forms into an access database.
Due to the nature of the beast of this program, I've had to split my main program into two sub programs and call them, but I need to use getobject to call a file path twice now.
I use getobject to open a file, and then use myrec.fields(~column name~) = xlsht.cells(1, "a") to populate various column values. I'm unsure if there are other "efficient" ways to accomplish this.
I was wondering if it is possible to use a variable in place of the filepath with the GetObject command, instead of needing to manually replace the file path in the code.
I've tested a fair amount of different code, including the path, class functionality but I don't think I understand VBA enough to truly make the best use of that.
I can make it work using this
Dim XL As Variant
Dim XLApp As Variant
Dim XLsht As Variant
Dim XLwrkbk As Variant
Set XL = CreateObject("Excel.Application")
Set XLwrkbk = GetObject(~file path~)
Set XLsht = XLwrkbk.Worksheets(1)
Set MyRec = CurrentDb.OpenRecordset("database name")
Ideally I would like it to be
Dim filename As String
Dim XL As Variant
Dim XLApp As Variant
Dim XLsht As Variant
Dim XLwrkbk As Variant
filename = " ~insert file path~ "
Set XL = CreateObject("Excel.Application")
Set XLwrkbk = GetObject(filename)
Set XLsht = XLwrkbk.Worksheets(1)
Set MyRec = CurrentDb.OpenRecordset("database name")
I receive a run time error
Run-time error '5':
Invalid procedure call or argument.
Try something like this:
Dim XL As New Excel.Application, Filename As String
Filename = "~ your file ~"
XL.Workbooks.Open (Filename)
myrec.fields(~column name~) = XL.Worksheets(1).Range("A1").value
I am trying to open a file but the Office application hangs during document opening in VB.NET.
I have this code:
Dim oProp As Object
Dim strPropValue As String
Dim lngRetVal As Integer
Dim strmsg As String
Dim lngretcode As Integer
Dim strPropertyName As String
Dim oWordDoc As Word.Document
Dim ObjOfficeAPP As Object
ObjOfficeAPP = New Word.Application()
GetWORDKEYS = cstFAILURE
ObjOfficeAPP.DisplayAlerts = WdAlertLevel.wdAlertsAll
ObjOfficeAPP.Application.Visible = True
oWordDoc = ObjOfficeAPP.Documents.Open(FileName:=strpFileName, Visible:=False)
I have problems on the line:
oWordDoc = ObjOfficeAPP.Documents.Open(FileName:=strpFileName, Visible:=False)
The debugger hangs on the Documents.Open() call, and just stays there waiting - without firing any type of exception or error. We have looked in the event log but only found the following.
My problem is, how can I set to open this document and not to block on the line with the Documents.Open() call?
Here are few points that could help:
Don't use the Application property for setting the Visible property:
ObjOfficeAPP.Visible = True
Don't set the DisplayAlerts property before opening a document.
Use the System.Reflection.Missing.Value for missing arguments.
You may find the How to automate Word from Visual Basic .NET to create a new document article helpful which explains the required steps for automating Word and provides a sample code:
'Start Word and open the document template.
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add
Trying to use Excel VBA to capture all the file attributes from files on disk, including extended attributes. Was able to get it to loop through the files and capture the basic attributes (that come from the file system):
File Path
File Name
File Size
Date Created
Date Last Accessed
Date Last Modified
File Type
Would also like to capture the extended properties that come from the file itself:
Author
Keywords
Comments
Last Author
Category
Subject
And other properties which are visible when right clicking on the file.
The goal is to create a detailed list of all the files on a file server.
You say loop .. so if you want to do this for a dir instead of the current document;
Dim sFile As Variant
Dim oShell: Set oShell = CreateObject("Shell.Application")
Dim oDir: Set oDir = oShell.Namespace("c:\foo")
For Each sFile In oDir.Items
Debug.Print oDir.GetDetailsOf(sFile, XXX)
Next
Where XXX is an attribute column index, 9 for Author for example.
To list available indexes for your reference you can replace the for loop with;
for i = 0 To 40
debug.? i, oDir.GetDetailsOf(oDir.Items, i)
Next
Quickly for a single file/attribute:
Const PROP_COMPUTER As Long = 56
With CreateObject("Shell.Application").Namespace("C:\HOSTDIRECTORY")
MsgBox .GetDetailsOf(.Items.Item("FILE.NAME"), PROP_COMPUTER)
End With
You can get this with .BuiltInDocmementProperties.
For example:
Public Sub PrintDocumentProperties()
Dim oApp As New Excel.Application
Dim oWB As Workbook
Set oWB = ActiveWorkbook
Dim title As String
title = oWB.BuiltinDocumentProperties("Title")
Dim lastauthor As String
lastauthor = oWB.BuiltinDocumentProperties("Last Author")
Debug.Print title
Debug.Print lastauthor
End Sub
See this page for all the fields you can access with this: http://msdn.microsoft.com/en-us/library/bb220896.aspx
If you're trying to do this outside of the client (i.e. with Excel closed and running code from, say, a .NET program), you need to use DSOFile.dll.
'vb.net
'Extended file stributes
'visual basic .net sample
Dim sFile As Object
Dim oShell = CreateObject("Shell.Application")
Dim oDir = oShell.Namespace("c:\temp")
For i = 0 To 34
TextBox1.Text = TextBox1.Text & oDir.GetDetailsOf(oDir, i) & vbCrLf
For Each sFile In oDir.Items
TextBox1.Text = TextBox1.Text & oDir.GetDetailsOf(sFile, i) & vbCrLf
Next
TextBox1.Text = TextBox1.Text & vbCrLf
Next
I was finally able to get this to work for my needs.
The old voted up code does not run on windows 10 system (at least not mine). The referenced MS library link below provides current examples on how to make this work. My example uses them with late bindings.
https://learn.microsoft.com/en-us/windows/win32/shell/folder-getdetailsof.
The attribute codes were different on my computer and like someone mentioned above most return blank values even if they are not. I used a for loop to cycle through all of them and found out that Title and Subject can still be accessed which is more then enough for my purposes.
Private Sub MySubNamek()
Dim objShell As Object 'Shell
Dim objFolder As Object 'Folder
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace("E:\MyFolder")
If (Not objFolder Is Nothing) Then
Dim objFolderItem As Object 'FolderItem
Set objFolderItem = objFolder.ParseName("Myfilename.txt")
For i = 0 To 288
szItem = objFolder.GetDetailsOf(objFolderItem, i)
Debug.Print i & " - " & szItem
Next
Set objFolderItem = Nothing
End If
Set objFolder = Nothing
Set objShell = Nothing
End Sub
Lucky discovery
if objFolderItem is Nothing when you call
objFolder.GetDetailsOf(objFolderItem, i)
the string returned is the name of the property, rather than its (undefined) value
e.g. when i=3 it returns "Date modified"
Doing it for all 288 values of I makes it clear why most cause it to return blank for most filetypes
e.g i=175 is "Horizontal resolution"