Can anyone pls explain me how to use SendKeys in PowerPoint VBA.
Sub SK()
SendKeys ("%"), True ' Alt
SendKeys ("H"), True ' Home Tab
SendKeys ("FC"), True ' Font Color pallet
End Sub
SendKeys in VBA will look like this, with a string of commands in double quotes. You don't need a separate statement for each command:
aShape.Select
SendKeys "+{F10}{DOWN}{DOWN}{DOWN}{DOWN}~{DEL}" & PhotoName$ & r & p & "{TAB}{UP}{UP}{UP}{UP}{UP}{UP}{UP}{DOWN}{TAB}~", True
I am trying to use SendKeys to navigate & enter data with Internet Explorer. The first section of code has a loop during which the webpage (ending in .php) refreshes itself to get the entry in. As I'm using SendKeys once the browser is opened, I'm looking for a way to pause the script until the page is loaded and ready to receive input. I'm currently using a small time delay of 3 seconds which works well but if the browser lags, everything goes haywire.
So, is there anything that can facilitate this or at least allow me to use the GetObject command to set a current instance of Internet Explorer as an object.
AppActivate "Explorer"
Dim CCount As Integer
Dim DCount As Integer
Dim LCount As Integer
LCount = 1
CCount = 2
DCount = ActiveSheet.Range("O2").Value
Dim LoopP As Integer
LoopP = 1
For LoopP = 1 To DCount
SendKeys ActiveSheet.Range("A" & CCount).Value, True
SendKeys "{Tab}", True
SendKeys Format(ActiveSheet.Range("C" & CCount).Value, "#,###"), True
SendKeys "{Tab}", True
' Breakdown IF
If ActiveSheet.Range("K" & CCount).Value = 1 Then
SendKeys ActiveSheet.Range("D" & CCount).Value, True
Else
End If
SendKeys "{Tab}"
' Remark IF
If ActiveSheet.Range("L" & CCount).Value = 1 Then
SendKeys ActiveSheet.Range("E" & CCount).Value, True
Else
End If
SendKeys "{Tab}", True
SendKeys "{Enter}", True
CCount = CCount + 1
' While IE.Busy Or IE.readyState <> 4: DoEvents: Wend
' While IE.readyState <> 4 Or IE.Busy: DoEvents: Wend
' Do While IE.readyState <> 4: WScript.Sleep 100: Loop
Application.Wait (Now + TimeValue("0:00:01"))
Next LoopP
I am trying to create a VBA macro to copy data from a PDF file to an Excel file but the data always merges into 1 column instead of 2 columns same as pdf file.
It is difficult to get data for next steps.
So, I would like to use tab "Save as other" ->Text in Pdf file to save file as txt file with 2 columns. I use SendKeys "%fhx" to show Save as dialog but I can't change the path and file name.
Please help me to solve this.
The code is as follows:
Do While filepdf <> ""
task = Shell("C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe " & filename, 1)
Application.Wait Now + TimeValue("00:00:2")
SendKeys "%fhx" AppActivate (task) --- I dont know how to select Save as dialog to change path and file name ---
SendKeys "%s" Application.Wait Now + TimeValue("00:00:2")
SendKeys "%{F4}", True
filepdf = Dir(path & "*.pdf")
Loop
Two disclaimers before my answer:
I do not have Acrobat Reader, that's why I have used Notepad.exe. But the idea is the same, you will manage to adapt it.
Sending keys is not a good practise in general. But everyone uses it, so...
That's the code, the comments are hopefully explanatory.
You should find a way to put it in your loop.
Have fun:
Option Explicit
Public Sub TestMe()
Dim task As Variant
task = Shell("C:/Windows/Notepad.exe", 1)
AppActivate task
'save as
SendKeys "%fa", True
'name in the save as
SendKeys "vityata.txt", True
'press enter
SendKeys "{ENTER}", True
'exit notepad
SendKeys "%fx", True
End Sub
Edit:
If you want to change the destionation path, then change it like this:
'name in the save as
SendKeys "C:\Users\lili\ivanova\ale\new\vityata.txt", True
I tried the below code and found that it was ok :
Sub SaveAsPdf()
Dim task
task = Shell("C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe " & namepdf, 1)
SendKeys "%fhx"
AppActivate (task)
SendKeys nametxt, True
SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{TAB}"
SendKeys "{ENTER}"
SendKeys path, True
SendKeys "{ENTER}"
SendKeys "%s"
End sub
This is the Sub that I have built so far:
Sub Grab_Screencap()
'Open URL
With CreateObject("InternetExplorer.Application")
.Visible = True
.Navigate _
Worksheets("Queue").Range("A3").Value
Application.Wait (Now + #12:00:02 AM#)
SendKeys "^p", True
Application.Wait (Now + #12:00:02 AM#)
SendKeys "{UP}", True
SendKeys "{UP}", True
SendKeys "~", True
Application.Wait (Now + #12:00:02 AM#)
SendKeys "+{TAB}", True
SendKeys "+{TAB}", True
SendKeys "+{TAB}", True
SendKeys "+{TAB}", True
SendKeys "+{TAB}", True
SendKeys "~", True
End With
End Sub
I am sure there are much better ways to do it, but I am still on the kiddie side of the pool.
This takes a URL that I have in a spreadsheets, and then opens IE, navigates to that page, opens the Print dialogue box, selects XPS Document Writer, navigates to the pathway field, and then highlights the value.
Now I want to pass a base directory, and a file name from a cell, something like
"C:\users\user1\desktop\" & Worksheets("Queue").Range("A5").Value
Tinkering around but cant find any existing documentation that lines up with what I'm trying to do that I can comprehend.
In order to print the content of the external file (for example, C:\Temp\TestFile.txt) from Excel VBA macro you can use ShellExecute() function as shown below.
First, insert the VBA Module (e.g. Module1) and place the following declarations and Sub into that Module1:
Public Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation _
As String, ByVal lpFile As String, ByVal lpParameters _
As String, ByVal lpDirectory As String, ByVal nShowCmd _
As Long) As Long
Public Const SW_SHOWNORMAL = 1
Public Sub ShellExecuteSample()
'shown as example: it will open the text file C:\Temp\TestFile.txt
OpenFile = ShellExecute(hwnd, "open", "C:\Temp\TestFile.txt", "", "C:\", SW_SHOWNORMAL)
'this will print the content of the text file C:\Temp\TestFile.txt
PrintFile = ShellExecute(hwnd, "print", "C:\Temp\TestFile.txt", "", "C:\", SW_SHOWNORMAL)
End Sub
When called, this Sub ShellExecuteSample() will print the content and (optionally) open the text file: C:\Temp\TestFile.txt. Pertinent to your case, it could be the file specified by your string:
"C:\users\user1\desktop\" & Worksheets("Queue").Range("A5").Value
Apparently, you do not need a PrintDialog to complete this task.
Hope this may help.
So, im sure this isn't nearly the most elegant way to achieve the task, but this is what I was able to write that does what I want it to do:
Sub Grab_Screencap()
Dim i As Integer
i = 3
Do Until IsEmpty(Cells(i, 2))
'Copy Screencap Name
Sheets("Queue").Cells(i, 6).Copy
'Open URL
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate _
Worksheets("Queue").Cells(i, 2).Value
Application.Wait (Now + #12:00:02 AM#)
SendKeys "^p", True
Application.Wait (Now + #12:00:02 AM#)
SendKeys "{UP}", True
SendKeys "{UP}", True
SendKeys "~", True
Application.Wait (Now + #12:00:02 AM#)
SendKeys "{TAB}", True
SendKeys "{TAB}", True
SendKeys "{TAB}", True
SendKeys "{TAB}", True
SendKeys "{TAB}", True
Application.Wait (Now + #12:00:02 AM#)
SendKeys "~", True
SendKeys "C:\Users\Johnny\Desktop\Job Listings\"
Application.Wait (Now + #12:00:02 AM#)
SendKeys "+{TAB}", True
SendKeys "+{TAB}", True
SendKeys "+{TAB}", True
SendKeys "+{TAB}", True
SendKeys "+{TAB}", True
Application.Wait (Now + #12:00:02 AM#)
SendKeys "^v", True
Application.Wait (Now + #12:00:02 AM#)
SendKeys "%s", True
Application.Wait (Now + #12:00:02 AM#)
IE.Quit
End With
i = i + 1
Loop
End Sub
The 101 lesson was that SendKeys can send an entire string, not just a single key. I didnt need to merge the file name with the pathway. I just needed to drop the pathway, tab back to file name, and drop the file name, which I could copy before opening IE.
I think there is a way to do this task just by opening the print dialogue and activating print to file, but I have not tried to figure that out yet.
Info
I have a button that opens/close a spreadsheet. The button simply checks if a sheet is visible (after another spreadsheet opened) and then calls the specific function. I have two functions that either open a workbook, or close the workbook. Excel only crashes when setting the visibility or closing the workbook. If I run the code in the editor without clicking the button, everything works fine.
Code
Private Sub Main()
If Sheets("XYZ").Visible = True Then
Application.Run ("OFF")
Else
Application.Run ("ON")
End If
End Sub
Private Sub ON()
Dim Pfd As String
Pfd = ThisWorkbook.Path
Application.ScreenUpdating = False
Sheets("XYZ").Visible = True
Sheets("XYZ").Select
Application.DisplayAlerts = False
SendKeys ("mso2013")
SendKeys "~"
SendKeys ("mso2013")
SendKeys "~"
Application.DisplayAlerts = True
Workbooks.Open Filename:=Pfd & "\UserID\second.xlsx"
Workbooks("second.xlsx").Protect Structure:=False, Windows:=False, Password:="mso2013"
Workbooks("second.xlsx").Sheets("XYZ").Unprotect Password:="mso2013"
Windows("second.xlsx").Visible = True
Windows("second.xlsx").WindowState = xlMaximized
Application.ScreenUpdating = True
End Sub
Private Sub OFF()
Application.ScreenUpdating = False
Windows("second.xlsx").WindowState = xlNormal
Windows("second.xlsx").Visible = False
Workbooks("second.xlsx").Sheets("Stamm").Protect Password:="mso2013"
Workbooks("second.xlsx").Protect Structure:=True, Windows:=True, Password:="mso2013"
Workbooks("second.xlsx").Close savechanges:=True
Sheets("XYZ").Visible = xlVeryHidden
Sheets("ADMIN").Select ' Admin is the sheet where the button is located
ActiveWindow.WindowState = xlMaximized
Application.ScreenUpdating = True
End Sub
I've tried timers, a button that just calls the OFF method, close without save, etc...
The problem is that it needs to be .visible = false and therefore I can't remove this line (without the line it would work like it should..).
Solved:
The problem was that I used a (ActiveX) Command Button instead of a normal button. No problems occurred with Excel 2010.