从Word中的表读取值以用作文件名 [英] Read value from table in Word to use as a filename
问题描述
您好
我是VB新手并从互联网上获取以下代码,但我无法正常使用。
I'm new to VB and got the following code from the internet, but I cannot get it to work correctly.
我第一次做一个mailmerge,它将所有字母合并成一个文件。然后我使用VB宏循环文档并拆分单独的Word文件。这些文件只是保存,文件名是循环计数器x。这是正常的。
I 1st do a mailmerge which merge all the letters into a single document. I then use the VB macro to loop through the document and split out separate Word files. These files are just saved with the file name being the loop counter x. This works correctly.
然后我添加了代码来从表中读取人名,并希望使用表中的值作为文件名而不是x。无法让它发挥作用
I then added the code to read the person's name from a table and want to use the value in the table as the filename instead of the x. Cannot get it to work
如果你能提供帮助,我将不胜感激。
Would appreciate it if you can assist.
Rossouw
Sub AllSectionsToSubDoc()
Sub AllSectionsToSubDoc()
Dim x As Long
Dim x As Long
Dim Sections As Long
Dim Sections As Long
Dim Doc 作为文件
Dim Doc As Document
Dim fName As String
Dim fName As String
Application.ScreenUpdating = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.DisplayAlerts = False
Set Doc = ActiveDocument
Set Doc = ActiveDocument
Sections = Doc.Sections.Count
Sections = Doc.Sections.Count
对于x =部分 - 1到1步-1
For x = Sections - 1 To 1 Step -1
Doc.Sections(x).Range.Copy
Doc.Sections(x).Range.Copy
Documents.Add
Documents.Add
ActiveDocument.Range.Paste
ActiveDocument.Range.Paste
调用ExtractTableData(fName)
ActiveDocument.SaveAs(Doc.Path&" \"
& x &" .doc" )
ActiveDocument.SaveAs (Doc.Path & "\" & x & ".doc")
ActiveDocument.Close False
ActiveDocument.Close False
下一个x
Application.ScreenUpdating = True
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.DisplayAlerts = True
End Sub
Sub ExtractTableData(aDataAll)
Sub ExtractTableData(aDataAll)
Dim Doc As Word.Document
Dim Doc As Word.Document
Dim tbl As Word.Table
Dim tbl As Word.Table
Dim rng As Word.Range
Dim rng As Word.Range
Dim sData As String
Dim sData As String
Dim aData1()As String
Dim aData1() As String
Dim aData2()As String
Dim aData2() As String
Dim aDataAll()As String
Dim aDataAll() As String
Dim nrRecs As Long
Dim nrRecs As Long
Dim nrFields As Long
Dim nrFields As Long
Dim lRecs As Long
Dim lRecs As Long
Dim lFields As Long
Dim lFields As Long
设置Doc = ActiveDocument
Set Doc = ActiveDocument
设置tbl = Doc.Tables(1)
Set tbl = Doc.Tables(1)
设置rng = tbl.ConvertToText(分隔符:= vbTab,_
Set rng = tbl.ConvertToText(Separator:=vbTab, _
NestedTables:= False )
NestedTables:=False)
'将分隔的文本放入并将其放入字符串变量中。
' Pick up the delimited text into and put it into a string variable.
sData = rng.Text
sData = rng.Text
'恢复原始表。
Doc.Undo
'剥去最后一段标记。
sData = Mid(sData,1,Len(sData) - 1)
sData = Mid(sData, 1, Len(sData) - 1)
'将每个表行分成一个数组元素。
' Break up each table row into an array element.
aData1()=拆分(sData,vbCr)
aData1() = Split(sData, vbCr)
nrRecs = UBound(aData1())
nrRecs = UBound(aData1())
'下面的消息框用于调试目的并告诉你
' The messagebox below is for debugging purposes and tells you
'表格中有多少行。
它已被注释掉但可以
' how many rows are in the table. It is commented out but can
'只需取消注释就可以使用。
' be used simply by uncommenting it.
'MsgBox"该表包含"& nrRecs + 1&" rows"
'MsgBox "The table contained " & nrRecs + 1 & " rows"
'处理每一行以分解字段信息
'Process each row to break down the field information
"进入另一个数组。
For lRecs = LBound(aData1())to nrRecs
For lRecs = LBound(aData1()) To nrRecs
aData2()= Split(aData1(lRecs),vbTab)
aData2() = Split(aData1(lRecs), vbTab)
'我们只需要这样做一次!
' We need to do this only once!
如果lRecs = LBound(aData1())那么
If lRecs = LBound(aData1()) Then
nrFields = UBound(aData2())
nrFields = UBound(aData2())
ReDim保留aDataAll(nrRecs,nrFields)
ReDim Preserve aDataAll(nrRecs, nrFields)
结束如果
'现在将行和字段信息放在一起
' Now bring the row and field information together
'在一个二维数组中。
For lFields = LBound(aData2())to nrFields
For lFields = LBound(aData2()) To nrFields
aDataAll(lRecs,lFields)= aData2(j)
aDataAll(lRecs, lFields) = aData2(j)
下一步
下一步
End Sub
收件人:
小飞侠
Peter Pan
30-Sep-13
2013年薪酬与绩效评估
REMUNERATION AND PERFORMANCE REVIEW 2013
我很高兴为您提供公司在当前和上一个财政年度向您提供的薪酬概览。
我还确认对根据您的继续工作,您对即将到来的财政年度的预计薪酬。
It gives me pleasure to provide you with an overview of the remuneration provided to you by the company in the current and previous financial year. I also confirm changes to your projected compensation for the forthcoming financial year based on your continued employment.
截至2013年9月30日止年度
Year Ended 30 Sep 2013
%更改
% change
截至2012年9月30日止年度
Year Ended 30 Sep 2012
保证公司成本
R 0
R 0
R 0
100%奖金
分配
100% Bonus Allocation
R 0
R 0
延长通知期
R 0
R 0
所有数字
R 0
0.00%
R 0
______________________________________________
______________________________________________
推荐答案
你没有给我们足够的信息来重复,也不够分析你的代码。能不能提供更多信息,说明表格中哪个项目应该提供文件名,你期望的结果以及实际得到的结果。
You don't give us enough information to repro, nor enough to analyse your code. Could you please provide more information about which item in the table is supposed to be providing the file name, the result you expect and the result you're actually getting.
这篇关于从Word中的表读取值以用作文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!