问题描述
我想打开一个Word文件并将其连接到Excel文档中的第3个表作为邮件的数据源.我的主要问题:如何设置"Table3"以避免打开此对话框?
I want to open a Word file and connect it to the 3rd table in an Excel document as data source for mailing. My main problem: how can I set the "Table3" to avoid opening this dialog box?
这里是我的审判(都作为#后面的注释):
Here my trials (all as comments behind #):
$Word = New-Object -Com Word.Application
$Word.Visible = 'TRUE'
$Doc = $Word.Documents.Open( "D:\GoogleDrive\_POWERSHELL\Template.doc" )
$Doc.Activate()
$DataPath = "D:\GoogleDrive\_POWERSHELL\DataSource.xls"
$DataTable = "Table3"
$default = [Type]::Missing
#1 $Doc.MailMerge.OpenDataSource( "D:\GoogleDrive\_POWERSHELL \DataSource.xls" ) #this works fine but opens a box to choose the table
#2 $Doc.MailMerge.OpenDataSource( $DataPath ) #this works fine too but opens a box to choose the table
#3 $Doc.MailMerge.OpenDataSource( Name:="D:\GoogleDrive\_POWERSHELL\DataSource.xls", Connection:="Table3" ) #SYNTAX ERROR: Fehlende ")" im Methodenaufruf / Missing ")" in Method Call
#4 $Doc.MailMerge.OpenDataSource -Name "D:\GoogleDrive\_POWERSHELL\DataSource.xls" -Connection "Table3" #SYNTAX ERROR: Sie müssen auf der rechten Seite des Operators "-" einen Wertausdruck angeben.
#5 $Doc.MailMerge.OpenDataSource( "D:\GoogleDrive\_POWERSHELL\DataSource.xls", 'TRUE', 'TRUE', 'TRUE', 'TRUE', '', '', 'FALSE', '', '', '',"'Table3'", '', '', '', '' )
$Doc.MailMerge.OpenDataSource( "D:\GoogleDrive\_POWERSHELL\DataSource.xls", $default, $default, $default, $default, $default, $default, $default, $default, $default, $default, "Table3", $default, $default, $default, $wdMergeSubTypeWord2000 )
#7 $Doc.MailMerge.OpenDataSource( $DataPath, $default, $default, $default, $default, $default, $default, $default, $default, $default, $default, $DataTable, $default, $default, $default, $default )
Write-Host $Doc.MailMerge.DataSource.Name
#$Doc.MailMerge.Execute()
#$Doc.MailMerge.Destination = "D:\GoogleDrive\_POWERSHELL\MailMergeTest.doc"
Start-Sleep 2 #Pause von 2 Sekunden
$Doc.Close()
$Word.Quit()
- #6(处于活动状态)似乎快到了,但无论如何都会打开用于选择表格的框.
- #3和#4-为什么出现这些语法错误?正确的语法是什么?
推荐答案
Word的最新版本不关心为Excel文件提供的连接字符串-如果需要特殊的字符串,则必须使用.odc文件(或ODBC).
Recent versions of Word do not care about the connection string that you provide for Excel files - if you need a special string, you have to use a .odc file (or ODBC).
您需要找到SQLStatement参数并将其设置为正确的SQL-例如
What you need is to find the SQLStatement parameter and set it to the correct SQL - e.g.
"SELECT * FROM [Table3 $]"
"SELECT * FROM [Table3$]"
但是确切的语法取决于Table3到底是什么.如果它是一个名为"Table3"的工作表,则需要
But the precise syntax depends on what exactly Table3 is. If it is a worksheet, called "Table3", you will need
"SELECT * FROM [Table3 $]"
"SELECT * FROM [Table3$]"
如果是命名范围,则需要
If it is a named range, you will need
"SELECT * FROM [Table3]"
"SELECT * FROM [Table3]"
如果它是一个命名表,我将不得不检查它是否完全起作用.
If it is a named Table, I would have to check whether it works at all.
如果它不是名称,而是Excel工作簿中的第三张纸,或者工作簿中的第三处其他",那么我认为您首先必须使Excel自动化以找到名称您要使用的对象.
If it isn't a name but really the third sheet in the Excel workbook, or the 3rd "something else" in the workbook, then I think you will first have to automate Excel to find the name of the object you want to use.
这篇关于调用MailMerge.OpenDataSource()设置连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!