Syntax error when exporting data in an excel document to word

tlgallen

New member
Local time
Today, 17:34
Joined
May 16, 2018
Messages
7
I receive a syntax error when trying to re-write old 2007 code used to export excel data to a word document the error is in the following line of code...

Code:
DoCmd.TransferSpreadsheet([TransferType As AcDataTransfer Type = acExport],[SpreadsheetType As AcSpreadSheetType = acSpreadsheetTypeExcel12Xml], [StrQuery], [StrDataDoc] = True)]

The original Access 2007 code which would not complete the merge read:

Code:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel2Xml, StrQuery, StrDataDoc, True

I know this must be a simple fix, but I am not a coder. This one line of code will not let us merge our word documents in 2016.
 
what I know is Transferspreadsheet method will either import the excel file to a table or export it into an excel sheet.
 
I don't see anything wrong with the "original" line that would require you to rewrite it, but your description doesn't work so well. As ArnelGP remarked, you don't send data to Word via TransferSpreadsheet.

If this used to work for Ac2007 and is now failing for some newer version of Office, then it is because MS fixed an error in something that shouldn't have worked in the first place. Word isn't Excel, so importing a spreadsheet to Word in a single step isn't something I would have ever guessed as the purpose of the original TransferSpreadsheet.

I think we will need a bit more background and information to get any traction on this one.
 
Thank you The_Doc_Man. I also appreciate your three time certification.

This code is a mess. Quite simply, the full code is below and it is supposed to:

1. Execute a specific query and export the data to an Excel spreadsheet called "WDExcel;
2. The data then is supposed to go to a WordMerge document based on the word document selected. The full code is below. I was told there is an easier way to do this merge without Excel. I am not sure if everything is Dimmed correctly or called out correctly. The hangup was originally in the fact an Old Version of Excel was called up and we are now in Access 2016. I corrected the versions, but I do not believe the old commands are fully compatible with Access 2016. Everything I have learned is by SOP (Seat of Pants) and now I am hoping to turn to the experts such as yourself here on this forum. I apologize for my ignorance. I am desperate to fix this because our staff is having to hand-write inspections they used to be able to merge into specific Word documents.

Code:
Option Compare Database
Option Explicit

Public Function dmerge(StrTemplate As String, StrQuery As String, strFolder As String)
DoCmd.Hourglass True
'pubCurrDBPath is a variable located in the GenMods module
'it is normally primed when db is opened via macro
'AutoExec(Runcode OpenProcs) by a call to genCurrDBPath
If pubCurrDBPath = "" Then Call genCurrDBPath
'this field is checked later in the procedure for a template named Generic or Generic CMS (added 9/2008)
StrTemplate = pubTemplateFolder & StrTemplate
StrQuery = StrQuery
strFolder = pubTemplateFolder
Dim HoldStrTemplate
HoldStrTemplate = StrTemplate
'pubTemplateFolder is a constant located in the GenMods module
If StrQuery = "none" Then GoTo SkipQuery
'Run query
Dim dbs As DAO.Database, rst As DAO.Recordset, qdf As DAO.QueryDef
Dim N As Integer
Dim StrDataDoc As String
StrDataDoc = pubCurrDBPath & "WDmerge.xlsx"
'delete current Excel data file.
'ignore error if file doesn't exist
On Error Resume Next
'from a macro for adding a new sheet to the spreadsheet
''If StrDataDoc Then
    'Sheets.Add
    'Sheets("Sheet1").Select
'    Sheets("Sheet1").Name = "XYZ"
'    Range("A1").Select
''Else
'original

Kill StrDataDoc
On Error GoTo 0
Set dbs = CurrentDb
On Error GoTo dMergeError
Set qdf = dbs.QueryDefs(StrQuery)
'this checks that the query results in data to merge
For N = 0 To qdf.Parameters.Count - 1
qdf.Parameters(N) = Eval(qdf.Parameters(N).Name)
Next N
Set rst = qdf.OpenRecordset
'exit function if recordset is empty
If rst.EOF Then
    MsgBox "No data to merge.", vbInformation, "Mail Merge"
    rst.Close
    GoTo Exit_Here
End If
rst.Close
qdf.Close
SkipQuery:
Dim wApp As New Word.Application
Dim wDoc As Word.Document
On Error GoTo dMergeError
' open word merge template document
Set wDoc = wApp.Documents.Open(StrTemplate)
'Dim strSampleFile
'strSampleFile = pubCurrDBPath & "Sample.doc"
'if there is no query associated with the passed strTemplate
'then goto procedure exit
If StrQuery <> "none" Then GoTo DodMerge
'StrQuery "none" means that the document is a blank
'form with no merge required
'this copies the template document and pastes it to a
'new open document
wDoc.Select
wApp.Selection.Copy
'close the templates document
wDoc.Close (wdDoNotSaveChanges)
'Documents.Add DocumentType:=wdNewBlankDocument
wApp.Documents.Add DocumentType:=wdNewBlankDocument
wApp.Selection.Paste
GoTo FinishUpDoc
'merge processing
DodMerge:
On Error GoTo dMergeError
'transfer data from the query to the Excel spreadsheet for merging with template data
'NOTE: An excel spreadsheet is used instead of a txt file because the
'latter has problems with quotes within the text
'original statement
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, StrQuery, StrDataDoc, True
'possible newer version of Excel
    'DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel2003, StrQuery, StrDataDoc, True



'execute merge
With wDoc.MailMerge
.MainDocumentType = wdFormLetters
.SuppressBlankLines = True
.Destination = wdSendToNewDocument
'6/15/05:  ME:  change format to wdOpenFormatDocument; hopefully will correct issue w. Generic merge
'.OpenDataSource Name:=StrDataDoc, Format:=wdOpenFormatDocument, LinkToSource:=False, Connection:="Entire Spreadsheet"
'.OpenDataSource Name:=StrDataDoc, Format:=wdOpenFormatAuto, LinkToSource:=False, Connection:="Entire Spreadsheet"
'12/20/05: ME: subtype is Word2000 for the merges
.OpenDataSource Name:=StrDataDoc, Format:=wdOpenFormatDocument, LinkToSource:=False, Connection:="Entire Spreadsheet", subtype:=wdMergeSubTypeWord
.Execute
End With
'close the merge 'template document' without saving
wDoc.Close (wdDoNotSaveChanges)
'ME:  6/21/06:  THIS IS THE AREA APPROXIMATELY WHERE THE EXCEL SPREADSHEET SHOULD CLOSE
If HoldStrTemplate = "Generic" Then GoTo GenericProcessing
If HoldStrTemplate = "Generic CMS" Then GoTo GenericProcessing
'If HoldStrTemplate = "Generic CMS" Then GoTo GenericProcessing

GoTo FinishdMerge
    
'If HoldStrTemplate <> "Generic" Or HoldStrTemplate <> "Generic CMS" Then GoTo FinishdMerge
'ORIGINAL STATEMENT:  ME:  9/9/2008
'If HoldStrTemplate <> "Generic" Then GoTo FinishdMerge
GenericProcessing:
''Generic' document processing follows
'pubCopyPermitDoc is a variable located in GenMods module
'it is primed in form fLetterPInsp when the Generic document
'is selected and when there is a valid document to copy
If IsNull(pubCopyPermitDoc) Then GoTo FinishdMerge
Dim strPermitDoc
'pubPermitDocFolder is a constant located in GenMods module
strPermitDoc = pubPermitDocFolder & pubCopyPermitDoc
'open the permit document and copy its contents to the clipboard
Set wDoc = wApp.Documents.Open(strPermitDoc)
wDoc.Select
wApp.Selection.Copy
'close the permit document
wDoc.Close (wdDoNotSaveChanges)
'locate and select "PermitInfoHere" text in the
'open (merged) document
With wApp.Selection.Find
    .Forward = True
    .ClearFormatting
    .MatchWholeWord = True
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute FindText:="PermitInfoHere"
End With
On Error GoTo dMergeError
'paste the contents of the clipboard over the current selected text
wApp.Selection.Paste
'2/25/05 ME:  add the following statement-see if it will give another case
On Error GoTo dMergeError
'set permit variable to null in prep for next 'generic' call
' pubCopyPermitDoc = Null
pubCopyPermitDoc = ""
FinishUpDoc:
'place cursor at top of document
wApp.Selection.GoTo What:=wdGoToLine, which:=wdGoToAbsolute, Count:=1
'select and copy a small amount of data to the clipboard
'this is done because otherwise user would have to answer a Windows-
'generated question on having to keep a large amount of data in the
'clipboard.
wApp.ActiveDocument.Characters(1).Select
wApp.Selection.Copy
FinishdMerge:
'show document in maximized window
'pubSaveAsFolder is a constant located in the GenMods module
wApp.Options.DefaultFilePath(wdDocumentsPath) = pubSaveAsFolder & strFolder
wApp.Visible = True
wApp.WindowState = wdWindowStateMaximize
Exit_Here:
DoCmd.Hourglass False
Exit Function
dMergeError:
DoCmd.Hourglass False
Select Case Err.Number
    Case 3265
        MsgBox ("Query named " & StrQuery & " cannot be located.")
    Case 5174
        MsgBox ("Word doc named " & StrTemplate & " cannot be located.")
        ActiveDocument.Close
    Case 5922
        MsgBox ("Data transferred to Word cannot contain quotes!")
            ActiveDocument.Close savechanges:=wdPromptToSaveChanges, OriginalFormat:=wdPromptUser
    Case Else
        MsgBox ("error # " & Err.Number & Err.Description)
End Select
Resume Exit_Here
End Function
 

Users who are viewing this thread

Back
Top Bottom