Excel VBA to get UNC from cell and open it in explorer.exe - windows

I have a file with a list of PC hostnames I want to be able to connect to the C drive of a specific one by clicking in a cell or button or something.
Let's say I currently have the hostname in Column A. I use CONCATENATE to turn it into a proper network path \\hostname\C$ and put that in Column B.
Now how can I make it so I can just click on the cell in column B to open that location in explorer.exe?
I have 450 PCs so i need to be able to specify the range, feed the network path into VBA and then open that in explorer.exe
does this make any sense? :P
Would really, really appreciate any help. Thanks.

Wrap the concatenated value in a "Hyperlink()". From then on it is clickable and it will open the explorer.
=HYPERLINK(CONCATENATE("\\";A1;"\C$");A1)
Or you put this code in your Worksheet code pane and double click the cells, where your links stand. But then you mustnt use it in combination with HYPERLINK.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column <> 2 Then Exit Sub
Dim sh As Object
Set sh = CreateObject("Wscript.Shell")
sh.Run ("explorer " & Target.Value)
Cancel = True
End Sub

Related

There is another way do Copy and Paste in VBS or BAT?

I was trying to automatize some repeated fill forms that I do in my job. I automate a lot of in VBA Excel, and I wanted to do with VBS too, but the company's computer doesn't work "^{C}", eather "%{TAB}".
In resume, ALT + or CTRL+ doesn't has the common response and a lot of functions are blocked...
There is another way to Change Window and Copy/Paste?
I found the problem:
.vbs has a lot of bugs in the blocked computer.
Ctrl + C or V works better as lower case: "^{c}" & "^{v}"
And the % + something doesn't work in any way.
So worked like this way for me:
Dim WS
set WS = CreateObject("WScript.Shell")
WS.AppActivate "Protocolos - Internet Explorer" ' name of the sheet
CScript.Sleep 100
WS.SendKeys "^{c}"
CScript.Quit

Code that will allow me to Left mouse click, I can only use Notepad to write the code and java is installed

I'm trying to write a script in VBScript that executes a series of events and one of them includes a left mouse click and release.
I know that I can't use VBScript in order to do the left mouse click and I can't install any software. I am only able to use Notepad in order to do all the code writing. I do have Microsoft word as well but I don't know if you can use that to write any code.
Is there a way for me to write some simple code in another language that allows me to left mouse click and release? I'm then planning on importing this code into my VBScript and continue on from there.
What I have so far:
Option Explicit
Dim Excel, x, y
Dim s
Set Excel = WScript.CreateObject("Excel.Application")
x = "10"
y = "10"
Excel.ExecuteExcel4Macro ( _
"CALL(""user32"",""SetCursorPos"",""JJJ""," & x & "," & y & ")")
WScript.Sleep (1000)
WScript.Sleep (1000)
WScript.Echo "Stop Script, Test test"
Thanks for reading!

How do I drag and drop an external file to my xojo app and return the path

I wanted to drag a file (say abc.txt) to my xojo program and let it to write out the path of the dropped file, returning something like C:\\mydata\abc.txt.
How do I go about doing it? Do I need to enable some properties?
I can't find anything useful from manual or forum.
First, add a File Type Set to your project. It'll be named FileTypes1 initially, but better rename it to "DropTypes". Add file types to it that you like to accept. To accept any file, click on the center of these buttons in the IDE's File Type Set editor:
Choose special/any.
Next, add this line to the Open event of the control or window that should allow drops:
me.AcceptFileDrop DropTypes.All
Then add this code to the control's or window's DropObject event:
if obj.FolderItemAvailable then
dim f as FolderItem = obj.FolderItem
' Now you have the file reference in f.
' Get the path:
dim path as String = f.NativePath ' (in older RB versions, use *f.AbsolutePath* instead)
' Show the path:
MsgBox path
end

How can you sort tabs in Visual Studio 2008 by file type?

I have been trying (without much luck) to write a macro for Visual Studio 2008 that will sort opened .cpp and .h files into two tab groups, placing headers on the left tab group and .cpp files on the right. I would love to have this feature because of the amount of time I spend moving my tabs around so that I can see both parts of the class that I am working on. I know that there is a non-free add-on for visual studio that allows tab managing, but it conflicts with an add-on that I need to use for work, making a macro my best option so far.
I am sure that this could be applied to other layouts and sorting needs as well if I can get it working. My thinking was to make the sorting happen automatically for me each time I open a document window, so I created a visual basic macro in the environment events section of the Visual Studio Macro IDE. Below is the code that I have so far:
Public Sub keepHeaderAndCxxInDifferentTabs() Handles WindowEvents.WindowCreated
Dim openedFile As String
openedFile = ActiveWindow.Document.FullName
If openedFile.Contains(".h") Then
' if the file being opened is a header file make sure it appears on the left
If DTE.ActiveDocument.ActiveWindow.Left > 600 Then
DTE.ExecuteCommand("Window.MovetoNextTabGroup")
End If
ElseIf openedFile.Contains(".cpp") Then
' if the file being opened is a cpp file make sure it appears on the right
If DTE.ActiveDocument.ActiveWindow.Left < 250 Then
DTE.ExecuteCommand("Window.MovetoNextTabGroup")
End If
Else
' if the file being opened is anything else make sure it appears on the right
If DTE.ActiveDocument.ActiveWindow.Left < 250 Then
DTE.ExecuteCommand("Window.MovetoNextTabGroup")
End If
End If
End Sub
Sadly this macro currently does not do anything. My best guess was that I could use the 'Left' property of the active window to figure out which tab group the new window appeared in and then move the window to the next tab group if it is not where I want it to be. I have tried a range of different values for the 'Left' property but so far none of my tabs move.
Does anyone know what I did wrong or have any suggestions? Thank you in advance for your time.
I found a way to do what I wanted using the function below. As long as I have my two vertical tabs setup when I run the macro it puts all headers in the left tab group and all other files in the right tab group. This can be further extended so that when I open any files using any other macros I write it sorts them as well by making a call to it after the macro has run. Sadly this does not work automatically, I am having problems getting it to actually perform the sorting whenever a specific event is triggered (using the environment events section).
'=====================================================================
' Sorts all opened documents putting headers into the left tab group
' and everything else into the right tab group
'=====================================================================
Public Sub SortFilesInTabs()
For i = 1 To DTE.Windows.Count Step 1
If DTE.Windows.Item(i).Document IsNot Nothing Then
If DTE.Windows.Item(i).Document.FullName.Contains(".h") Then
' if the file is a header file make sure it appears on the left
If DTE.Windows.Item(i).Document.ActiveWindow.Left > 600 Then
WriteOutput("moved file " & DTE.Windows.Item(i).Document.FullName & " to left")
DTE.Windows.Item(i).Document.Activate()
DTE.ExecuteCommand("Window.MovetoPreviousTabGroup")
End If
ElseIf DTE.Windows.Item(i).Document.FullName.Contains(".cpp") Then
' if the file is a cpp file make sure it appears on the right
If DTE.Windows.Item(i).Document.ActiveWindow.Left < 250 Then
WriteOutput("moved file " & DTE.Windows.Item(i).Document.FullName & " to right")
DTE.Windows.Item(i).Document.Activate()
DTE.ExecuteCommand("Window.MovetoNextTabGroup")
End If
ElseIf DTE.Windows.Item(i).Document.FullName.Length > 0 Then
' if the file is any other valid document then make sure it appears on the right
If DTE.Windows.Item(i).Document.ActiveWindow.Left < 250 Then
WriteOutput("moved file " & DTE.Windows.Item(i).Document.FullName & " to right")
DTE.Windows.Item(i).Document.Activate()
DTE.ExecuteCommand("Window.MovetoNextTabGroup")
End If
End If
End If
Next i
End Sub
If anyone can improve this further pl

Using AppActivate and Sendkeys in VBA shell command

I want to switch back and forth between application with the shell command in VBA.
I am using SendKeys to do stuff in process A then move to process B, then to process A then process B. It works fine for the first iteration. When I use AppActivate to go back to process B, it actually does switch focus back to process B. HOWEVER, it ignores subsequent commands from SendKeys.
Example code:
Sub pastePDF2TXT_v3(pdfName As String, txtName As String)
Dim acrobatID
Dim acrobatInvokeCmd As String
Dim acrobatLocation As String
Dim notepadID
Dim acrobatID2
Dim notepadID2
Debug.Print "here"
acrobatLocation = "C:\Program Files\Adobe\Acrobat 9.0\Acrobat\Acrobat.exe"
acrobatInvokeCmd = acrobatLocation & " " & pdfName
acrobatID = Shell(acrobatInvokeCmd, 1)
AppActivate acrobatID
SendKeys "^a", True '^A selects everything already in the pdf file.
SendKeys "^c", True '^C copies the selection to the clipboard.
notepadID = Shell("NOTEPAD.EXE " & txtName, 1) ' invoke notepad on the text file.
AppActivate notepadID ' set the new app as teh active task.
SendKeys "^a", True '^A selects everything already in the text file.
SendKeys "^v", True '^V pastes the new stuff over the top of the old text file (deleting the old stuff)
SendKeys "%{END}", True ' makre sure last line of text file
SendKeys "{ENTER}", True
AppActivate acrobatID ' NOTE: APPEARS TO WORK UP TO THIS POINT.
SendKeys "{ENTER}", True ' NOTE: SECOND APP IGNORES SUBSEQUENT COMMANDS FROM HERE DOWN.
SendKeys "^a", True '^A selects everything already in the pdf file.
SendKeys "^c", True '^C copies the selection to the clipboard.
SendKeys "%f", True 'alt f, x to exit Notepad.exe
SendKeys "x", True
'acrobatID.Quit
Debug.Print "start second"
AppActivate notepadID ' set the new app as teh active task.
SendKeys "%{END}", True 'Go to end of text file.
SendKeys "^v", True '^V pastes the new stuff at end of file.
SendKeys "{ENTER}", True
SendKeys "^s", True
SendKeys "%f", True 'alt f, x to exit Notepad.exe
SendKeys "x", True
notepadID.Quit
acrobatID.Quit
End Sub
I know this question is old, but I have run into this same problem, but I don't think the answer that was chosen is the correct one.
When you call AppActivate, the script goes on halt, waiting for the target application to terminate. After the application is terminated, the script continues. I see no solution to this issue.
EDIT:
I use a batch-file to call CSCRIPT for the AppActivate command, and I notice you are strictly using a VBS file. Try calling CMD as a new window and pass CSCRIPT //NoLogo //B WScript.CreateObject("WSCript.shell").AppActivate("Window Name") as the argument, and see if that bypasses the issue.
Similarly, I was trying to use AppActivate on a pdf document I had opened with a shell command so that I could use SendKeys on it. It would always generate Run Time Error '5'. My solution, eventually was to NOT use AppActivate at all, in fact opening the document brings it to the forefront anyway. The problem is that the SendKeys statement executes immediately after the shell command but the pdf needs a second or two to open up. My solution is to pause the code for a couple seconds before excuting the sendkeys statement.
I used a time delay curtosy of pootle flump. Check out the thread here:
http://www.dbforums.com/microsoft-access/1219379-time-delay-vba.html
After hours of research on various forums, I could figure out that I wont be able to use Adobe library objects and functions with Adobe Reader. The only viable option left was to use Shell Commands to use "save as text" option available in the Adobe Reader's file menu. The shortcut key is Alt+f+a+x+s. I implemented these in the below code which worked perfectly, although I was required to insert delays in some steps.
Sub SavePDFasText()
Dim AdobeReaderPath As String
Dim PdfFilePath As String
Dim PDFid, NotepdId As Double
AdobeReaderPath = "C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe"
PdfFilePath = "D:\PowerGenMacro\opm_11.pdf"
PDFid = Shell(AdobeReaderPath & " " & PdfFilePath, vbNormalFocus)
Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 5)
'Alt+f+a+x is required to save the pdf as text in the adobe reader
SendKeys "%(FAX)", True
Application.Wait TimeSerial(Hour(Now()), Minute(Now()), Second(Now()) + 2)
SendKeys "%S", True
SendKeys "^q", True
End Sub
This might be more of a comment than an answer, but I don't seem to be allowed to "comment", so...
It's amazing you've gotten as far as you did. Chances are, you're never going to be able to make this work reliably -- period. As soon as you do make it work, something will change about how the Acrobat UI behaves in a future release, or Windows will make another change to the rules for what things can send events to what other things, and then you're hosed again.
What you are probably running into in this case is Windows trying to prevent an application stealing the focus from another when a user is apparently busy interacting with the first one. You see the same kinds of problems trying to do things like have a button in one application write data to a temporary file and open MS Word to edit it. Windows prevents the focus from transferring away from the current app and over to MS Word because you just clicked a button in the current app.
So -- instead of trying to solve this impossible technical issue, let's take a step back and ask what you were originally hoping to accomplish by doing all of this. In that way, perhaps, we can get you where you're trying to go :)
Try
sendkeys "%{tab}"
to switch windows
but then keep only 2 active windows.
or try
appactivate only after sendkeys {Tab} so that text entry box is not selected before switchibg windows cmd.
or try
try appactivate window name, 1
then sleep 2000 °to allow time to bring that window into focus
You forgot to add "DoEvents" between each sendkeys and kind of delay/wait period, to give it some time to execute.

Resources