473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SendKeys not consistent with MS Word

I need to create a report in MS Word populated with data from A2K. I
have been asked to create the report in Word so that parts of it can be
edited as necessary later. The data in the report are in a table with
headings for each column, so converting an Access report to a Word doc
in RTF doesn't work because only the text is converted, not the table.

What I have tried so far, with some success, is to write the report
directly in Word.
I've made a template (a Word document) for the report which consists of
a header and an empty table (1 row, 7 columns).

In a command button click event, I have written code to open the
template in MS Word, then using SendKeys, edit the header and enter the
data in to the cells of the table. Data is entered into the first cell
then Sendkeys TABs to the next cell, enters data, TABs to next cell...
I only enter data into the first three columns of each row. The
remaining cloumns will be filled by the user after the report is
generated.

The problem I am having is that SendKeys does not perform consistently.
When it does work correctly, it will do so for many repititions. When
it doesn't, it seems like the TABs are not being executed correctly -
data is printed to incorrect cells, some cells contain more than they
should. And the incorrect tabbing isn't consistent either within the
same execution or from one to another.

Is there some way to get SendKeys to work the same way every time I run
this sub?

Alternatively, I am open to suggestions of another (easier) way to
create my report in Word.

Thanks for any help offered.

The code for my sub follows.

Private Sub PrintToWord()

On Error GoTo Err_PrintToWord

Dim ReturnValue
Dim strTitle As String ' exam title
Dim strGrade As String ' salary grade
Dim strExamNum As String ' exam number
Dim strType As String ' type of appointment
Dim strLocation As String ' loacation of position

Dim strName As String ' name of Eligible
Dim strPhone As String ' phone number of Eligible
Dim strScore As String ' score on exam
Dim db As DAO.Database
Dim rsReport As DAO.Recordset

Set db = CurrentDb
Set rsReport = db.OpenRecordset("tblReportData")

rsReport.MoveFirst

strTitle = rsReport!Title

If rsReport!SalaryGrade <> Null Then
strGrade = rsReport!SalaryGrade
Else
strGrade = " "
End If

strExamNum = rsReport!ExamNum
strType = mstrApptType
strLocation = mstrLocation

ReturnValue = Shell("WINWORD.EXE template.doc", 1) ' Run Word
AppActivate ReturnValue ' Activate Word

' BEGIN HEADER

' open header
SendKeys "%vh", True

' enter title of exam and salary grade
SendKeys "{DOWN 2}", True
SendKeys "{ENTER}"
SendKeys "{TAB 3}", True
SendKeys strTitle, True
SendKeys ", ", True
SendKeys "SG-" & strGrade, True

' enter exam number
SendKeys "{ENTER}", True
SendKeys "{TAB 3}", True
SendKeys "{(}", True
SendKeys strExamNum, True
SendKeys "{)}", True

' enter type of appointment
SendKeys "{DOWN 4}", True
SendKeys "{TAB 3}", True
SendKeys "PERMANENT", True

' enter location
SendKeys "{TAB 7}", True
SendKeys "LOCATION", True

' close header
SendKeys "%C", True

' END HEADER
' goto first cell of table
SendKeys "{DOWN}", True

' enter first Eligble
' assign data
strName = rsReport!EligName

If rsReport!Phone <> Null Then
strPhone = rsReport!Phone
Else
strPhone = " "
End If

strScore = rsReport!Score

' print name
SendKeys strName, True

' move to next cell and print phone
SendKeys "{TAB}", True
SendKeys strPhone, True

' move to next cell and print score
SendKeys "{TAB}", True
SendKeys strScore, True

' move to end of row
SendKeys "{TAB 4}", True

rsReport.MoveNext

' enter remaining Eligibles
Do While Not rsReport.EOF

' create new row
SendKeys "{TAB}", True

' assign data
strName = rsReport!EligName

If rsReport!Phone <> Null Then
strPhone = rsReport!Phone
Else
strPhone = " "
End If

strScore = rsReport!Score

' print name
SendKeys strName, True

' move to next cell and print phone
SendKeys "{TAB}", True
SendKeys strPhone, True

' move to next cell and print score
SendKeys "{TAB}", True
SendKeys strScore, True

' move to end of row
SendKeys "{TAB 4}", True

rsReport.MoveNext

Loop

' open "Save As" dialog
SendKeys "%fa", True

Exit_PrintToWord:
Exit Sub

Err_PrintToWord:
MsgBox Err.Description
Resume Exit_PrintToWord

End Sub

Nov 13 '05 #1
2 5669
Might be worth having a look @ this thread:

"Making a word document with VBA in Access or Word? Examples from other
users"

As it may be of interest, it is a couple of threads below yours on
google.com groups.

Dave

Nov 13 '05 #2
Thanks. I saw this thread after I posted my question.

Randy
davidjp...@lycos.co.uk wrote:
Might be worth having a look @ this thread:

"Making a word document with VBA in Access or Word? Examples from other users"

As it may be of interest, it is a couple of threads below yours on
google.com groups.

Dave


Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mr. Bungle | last post by:
I would like to send email automatically via a command button. I have accomplished this just fine through the following code: (Outlook should already be open for reliable results) Private Sub...
1
by: George | last post by:
Every time I used the Sendkeys command in my application the "Numlock" turned off and I couldn't use the keypad to hit numbers...... The old code was: Private Sub Command1_Click() SendKeys...
5
by: Wayne Gibson | last post by:
Hi, Was wondering if somebody could help.. I'm trying to use Sendkeys on a Windows forms. I have entered the following command to simulate a CTRL+ALT+1.. ...
1
by: Bryan | last post by:
I am writing a C# Windows App that updates out Excel reports' modules. The app is complete, but has a problem. The only way MS allows you to unprotect the VBA code in Excel is to do it by hand or...
1
by: Logan McKinley | last post by:
I have an application that uses SendKeys.SendWait(...) to send RTF (formatting and all) to a word. I was hoping it would parse the formatting, therefore retaining the original formatting of the...
1
by: GrantS | last post by:
I need to use a sendkeys key combination to automate the "accept files" that a remote user wants to send to me via Windows messenger. I am using automation to work with Windows Messenger client in...
6
by: Michael Maes | last post by:
Hello, I'm invoking successive SendKeys.SendWait("{BACKSPACE}") SendKeys.SendWait("{DELETE}") in a loop. The issue is that it causes a Beep on every Pass. Setting the 'Handled = True' on...
0
by: dtshedd | last post by:
I have a database with hundreds of embedded photos (Microsoft Photo 3.0) Many are larger than 1 MB. I tried Stephen Lebans macro but it did not work probably for the aforementioned reasons Now...
6
by: Whizzo | last post by:
Hi folks; I spent a little while some time ago building a touchscreen data entry interface into an Access 2003 form. I'm not too experienced in VBA at all so I was quite happy when I came up with a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.