SendKeys Compile Error

jagrenet

Board Regular
Joined
Feb 23, 2022
Messages
81
Office Version
  1. 365
  2. 2013
  3. 2011
  4. 2010
Platform
  1. Windows
Hello,

I am attempting to use SendKeys in vba to "send" Enter to a Word doc. I keep getting a Compile error even after the variable is declared.
This code already lives inside a working Sub Routine, just want to "add" this function.

Dim SndEnter As Variant

Set SndEnter = Application.SendKeys("{ENTER}", True)

With
(working Application.Selection code

SndEnter
End With

"Compile Error - Expected Function or variable "

Not sure why this error is coming up.
Any ideas ??

Thanks in advance
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Sendkeys doesn’t return anything.
 
Upvote 0
Yes, my point is that SendKeys doesn't return anything so there is no result to assign to your variable. What are you expecting the variable to contain?
 
Upvote 0
Yes, my point is that SendKeys doesn't return anything so there is no result to assign to your variable. What are you expecting the variable to contain?
The only reason I set it as a variable is because a different error message prompted for it.
I have the Word document Open. The Collapsed Selection has the cursor at thhe beginning of the sentence, exxactly where I want it. I simply want to "send" Enter, so that whole sentence goes to the beginning of the next line. When I do this manually it works perfectly. I wouold like for my code to perform this task.

With doc.Application.Selection

.Find.Text = "Some Text"
.Find.Execute
.Next(Unit:=wdWord, Count:=1).Select
.InsertDateTime DateTimeFormat:="MMMM yyyy", InsertAsField:=True
.Text = " "
.Next(Unit:=wdWord, Count:=1).Select
.Delete

'NEED TO SOMEHOW SEND {ENTER} - RIGHT HERE - BEFORE MOVING FORWARD

SndEnter

End With
 
Upvote 0
I can't believe you would need Sendkeys for this at all. I would think you would simply add a carriage return (or similar, Word not being my thing) to the document text.
 
Upvote 0
I can't believe you would need Sendkeys for this at all. I would think you would simply add a carriage return (or similar, Word not being my thing) to the document text.
If I can make that work ..... with my code, I would be happy to. How is that done via VBA ???
 
Upvote 0
May just use the Selection.TypeParagraph method.
PERFECT !!!!! Now, I need to "Select" the entire sentence and set Line Spacing and "Justify" left. Tetra201, thank you for pointing out that Method. I have been trying to figure out a way to do that for a couple of days. How do I select the entire sentence ??
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top