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
Related
i'm trying to copy screen info from 5250 green screen application, paste into text file then save it into a specific path location (C:\Desktop\testing).
Sub Savetxt()
AppActivate 5250Screen
SendKeys "%", True
SendKeys "EC", True
Call Shell("NOTEPAD.EXE", 1) 'open notepad
SendKeys "^v", True 'paste to notepad
SendKeys "^s", True 'save notepad
Item.SaveAs "C:\Desktop\testing" & filename & ".txt", olTXT
SendKeys "{enter}", True
SendKeys "%{F4}", True
End Sub
I am trying to create two keyboard shortcuts which allow me to move selected words quickly to the right and left within a text. The selected text should move one word to the left or the right.
Here is what I want to do
1) Select words e.g. “this is” in the sentence “this is a tree”
2) Press e.g. ctrl + alt + arrow to the right
3) The sentence reads now as “a this is tree”
4) Press again ctrl alt + arrow to the right
5) The sentence reads now as “a tree this is”
The idea is to replace the cut / paste steps and make the process a bit more efficient and smoother.
I have no knowledge in VB, but managed to get close to by using Word’s macro-function.
Sub moveRight()
'moveRight Macro
Selection.Cut
Selection.moveRight Unit:=wdWord, Count:=1
Selection.PasteAndFormat (wdFormatOriginalFormatting)
End Sub
The problem with this function is that the selected words are no longer selected once they are pasted. Hence, triggering the function again (=moving the text more than one word) results in an error (I would have to select the relevant text again). Is there any way that the selected words remain selected after they are pasted so that I can trigger the function repeatedly?
Many thanks.
You might like to try this solution. The first two procedures below should be called by your keyboard shortcuts. The both call the same executing sub, but with different parameters.
Sub MoveSelectionLeft()
' call with keyboard shortcut
GetSelection True
End Sub
Sub MoveSelectionRight()
' call with keyboard shortcut
GetSelection False
End Sub
Private Sub GetSelection(ByVal ToLeft As Boolean)
' 22 Apr 2017
Dim Rng As Range
Dim SelTxt As String ' selected text (trimmed)
Dim Sp() As String
Set Rng = Selection.Range
With Rng
SelTxt = Trim(.Text)
If ToLeft Then
.MoveStart wdWord, -1
Else
.MoveEnd wdWord, 1
End If
Sp = Split(Trim(.Text))
If ToLeft Then
.Text = SelTxt & " " & Sp(0) & " "
Else
.Text = Sp(UBound(Sp)) & " " & SelTxt & " "
End If
.Find.Execute SelTxt
.Select
End With
End Sub
A cheap way of doing this is with bookmarks. At some point before and after moving the text, run AddBookMark and DeleteBookMark respectively.
Public Sub AddBookMark()
Dim myDocument As Document
Set myDocument = ActiveDocument
myDocument.Bookmarks.Add "MySelectedText", Selection
End Sub
Public Sub DeleteBookMark()
Dim myDocument As Document
Set myDocument = ActiveDocument
myDocument.Bookmarks("MySelectedText").Delete
End Sub
Sub moveRight()
Dim myDocument As Document
Set myDocument = ActiveDocument
Selection.Cut
Selection.moveRight Unit:=wdWord, Count:=1
Selection.PasteAndFormat (wdFormatOriginalFormatting)
myDocument.Bookmarks("MySelectedText").Select
End Sub
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
I need to come up with an updated VBA script that will transfer the result from an Access 2010 request into an existing Excel file, after the current content.
Here is the previous script, that worked with Access 97.
Private Sub CmdTransfert_Click()
On Error GoTo Err_CmdTransfert_Click
Dim Requete As QueryTable
Dim appXL As Excel.Application
Dim Classeur As Excel.Workbook
Dim Cellule As Excel.Range
Dim Plage As Excel.Range
' Bills that needs to be transfered
DoCmd.OpenQuery "Liste_factures_numero"
' Select answers
SendKeys "^a", True
' Copy
SendKeys "^c", True
' Opening Excel
Set appXL = CreateObject("Excel.Application")
appXL.Visible = True
'Only XL 97 supports UserControl Property
On Error Resume Next
appXL.UserControl = True
Set Classeur = appXL.Workbooks.Open("C:\Users\me\Desktop\copiedetravailvlvaccdb\reglements.xlsx")
Set Cellule = Classeur.Worksheets(1).Cells(Classeur.Worksheets(1).Range("a1").CurrentRegion.Rows.Count + 1, 1)
Cellule.Select
' Paste
SendKeys "^v", True
' Cellule.PasteSpecial xlPasteValues
' Deleting titles
Cellule.EntireRow.Delete
'Fixing date format
Set Plage = Selection
For Each Cellule In Plage
If Cellule.Column = 2 Then
If Cellule.Value <> "" Then
Cellule.Value = CDate(Cellule.Value)
End If
End If
Next
' Mise au format normal
Range("A3:D3").Copy
Plage.PasteSpecial Paste:=xlFormats
Excel.Application.CutCopyMode = False
' Closing request
DoCmd.Close acQuery, "Liste_factures_numero", acSaveNo
Exit_CmdTransfert_Click:
Exit Sub
Err_CmdTransfert_Click:
MsgBox Err.Description
Resume Exit_CmdTransfert_Click
End Sub
I have tried adding Sleep commands around the Sendkeys instruction, it did not worked. The result was not selected, thus not copied nor pasted.
As last resort I tried creating a new script using DoCmd.TransferSpreadsheet, but I can't figure how to add the new content after the existing one.
DoCmd.TransferSpreadsheet acExport, , "Liste_factures_numero", "C:\Users\me\Desktop\copiedetravailvlvaccdb\reglements.xlsx", True
However as expected it overwrite the content.
This is my first time with VBA.
You can change this:
DoCmd.OpenQuery "Liste_factures_numero"
' Select answers
SendKeys "^a", True
' Copy
SendKeys "^c", True
To this:
DoCmd.OpenQuery "Liste_factures_numero"
DoCmd.RunCommand acCmdSelectAllRecords
DoCmd.RunCommand acCmdCopy
And this:
' Paste
SendKeys "^v", True
To this:
Cellule.PasteSpecial Paste:=xlPasteValues
Also, I must add that it's another way to achieve this copy-pasting from Access to Excel via macros:
Make connection to Access file;
Get data from query into recordset;
Paste data on excel sheet;
Use formatting on pasted cells (font, size etc).
Something like this, changing worksheets/column A as necessary:
Dim r As Range
Set r = ThisWorkbook.Worksheets(1).Range("A1").End(xlDown).Offset(1, 0)
DoCmd.TransferSpreadsheet acExport, , "Liste_factures_numero" _
, ThisWorkbook.FullName & Chr(35) & "Sheet1", True, r.Address
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.