I have 2 tables in my document. I found out their variable numbers, but I don't know how to go the next table

huangapple go评论69阅读模式
英文:

I have 2 tables in my document. I found out their variable numbers, but I don't know how to go the next table

问题

这是我的代码。

Sub sWordTables(daSN2, daGL, daST, daSY, daBEY, daENY, daSUP)
    Dim daDB As DAO.Database
    Dim appWord As Object 'Word.Application
    Dim WordDoc As Object 'Word.Document
    Dim wdTable As Object 'Word.Table
    Dim cname As String
    Dim cnum As String
    Dim gss As Integer
    Dim rst As DAO.Recordset
    Dim aVar As String
    Dim wdcount As Integer
    
    Dim strPath As String
    Dim IntHolder As Integer
    Set appWord = CreateObject("Word.Application")
    
    strPath = "C:\Users\wpulk\Documents\"
    
    Set daDB = CurrentDb()
    Set rst = daDB.OpenRecordset("qry" & daST, dbOpenDynaset)
    Set WordDoc = appWord.Documents.Open(strPath & "MasterRecord.docx")
    wdcount = WordDoc.Tables.Count
    Do While Not rst.EOF
        With WordDoc
            cname = rst!CourseName
            cnum = rst!CourseNumber
            gss = rst!GradeScore
            incn = rst!IncrementNumber
            wdcount = wdcount + 1
            
            If wdcount = 3 Then
                .Variables.Add ("StudentName"), daST
                .Variables.Add ("Academic Advisor"), daSUP
                .Variables.Add ("School Year"), daSY
                .Variables.Add ("Grade Level"), daGL
                .Variables.Add ("Beginning Date"), daBEY
                .Variables.Add ("Ending Date"), daENY
                .Fields.Update
            End If
            If wdcount >= 4 Then
                MsgBox .Variables(6).Name
                
                If .Variables("Math").Value = cname Then
                    .Variables.Add ("Math" & incn & "a"), cnum
                    .Variables.Add ("Math" & incn & "b"), gss
                ElseIf .Variables("English").Value = cname Then
                    .Variables.Add ("Eng" & incn & "a"), cnum
                    .Variables.Add ("Eng" & incn & "b"), gss
                ElseIf .Variables("Word Building").Value = cname Then
                    .Variables.Add ("WB" & incn & "a"), cnum
                    .Variables.Add ("WB" & incn & "b"), gss
                ElseIf .Variables("Lierature").Value = cname Then
                    .Variables.Add ("Lit" & incn & "a"), cnum
                    .Variables.Add ("Lit" & incn & "b"), gss
                ElseIf .Variables("Science").Value = cname Then
                    .Variables.Add ("Science" & incn & "a"), cnum
                    .Variables.Add ("Science" & incn & "b"), gss
                ElseIf .Variables("Social Studies").Value = cname Then
                    .Variables.Add ("SocS" & incn & "a"), cnum
                    .Variables.Add ("SocS" & incn & "b"), gss
                ElseIf .Variables("Bible").Value = cname Then
                    .Variables.Add ("Bible" & incn & "a"), cnum
                    .Variables.Add ("Bible" & incn & "b"), gss
                ElseIf .Variables("Florida History").Value = cname Then
                    .Variables.Add ("oth" & incn & "a"), cnum
                    .Variables.Add ("oth" & incn & "b"), gss
                    .Fields.Update
                End If
            End If
        End With
    Loop
    appWord.ActiveDocument.SaveAs strPath & "MasterRecord" & daST & ".docx"
    Set WordDoc = Nothing
    Set appWord = Nothing
End Sub

With other's help I was able to fix my first problem, but I have another problem where my document has 2 tables, and I know how to get the variable number of the first table, but I don't know how to get the second table. The first table has only 1 row and 6 columns, and I was able to process that one correctly, but when the code was going to what I thought was the second table, it did not. it was still at the first table. I don't know what is the right code to do it.

英文:

Here is my code.

Sub sWordTables(daSN2, daGL, daST, daSY, daBEY, daENY, daSUP)
    Dim daDB As DAO.Database
    Dim appWord As Object 'Word.Application
    Dim WordDoc As Object 'Word.Document
    Dim wdTable As Object 'Word.Table'
    Dim cname As String
    Dim cnum As String
    Dim gss As Integer
    Dim rst As DAO.Recordset
    Dim aVar As String
    Dim wdcount As Integer
    
    Dim strPath As String
    Dim IntHolder As Integer
    Set appWord = CreateObject("Word.Application")
    
    strPath = "C:\Users\wpulk\Documents\"
    
    Set daDB = CurrentDb()
    Set rst = daDB.OpenRecordset("qry" & daST, dbOpenDynaset)
    Set WordDoc = appWord.Documents.Open(strPath & "MasterRecord.docx")
    wdcount = WordDoc.Tables.Count
    Do While Not rst.EOF
        With WordDoc
            cname = rst!CourseName
            cnum = rst!CourseNumber
            gss = rst!GradeScore
            incn = rst!IncrementNumber
            wdcount = wdcount + 1
            
            If wdcount = 3 Then
                .Variables.Add ("StudentName"), daST
                .Variables.Add ("Academic Advisor"), daSUP
                .Variables.Add ("School Year"), daSY
                .Variables.Add ("Grade Level"), daGL
                .Variables.Add ("Beginning Date"), daBEY
                .Variables.Add ("Ending Date"), daENY
                .Fields.Update
            End If
            If wdcount >= 4 Then
                MsgBox .Variables(6).Name
                
                If .Variables("Math").Value = cname Then
                    .Variables.Add ("Math" & incn & "a"), cnum
                    .Variables.Add ("Math" & incn & "b"), gss
                ElseIf .Variables("English").Value = cname Then
                    .Variables.Add ("Eng" & incn & "a"), cnum
                    .Variables.Add ("Eng" & incn & "b"), gss
                ElseIf .Variables("Word Building").Value = cname Then
                    .Variables.Add ("WB" & incn & "a"), cnum
                    .Variables.Add ("WB" & incn & "b"), gss
                ElseIf .Variables("Lierature").Value = cname Then
                    .Variables.Add ("Lit" & incn & "a"), cnum
                    .Variables.Add ("Lit" & incn & "b"), gss
                ElseIf .Variables("Science").Value = cname Then
                    .Variables.Add ("Science" & incn & "a"), cnum
                    .Variables.Add ("Science" & incn & "b"), gss
                ElseIf .Variables("Social Studies").Value = cname Then
                    .Variables.Add ("SocS" & incn & "a"), cnum
                    .Variables.Add ("SocS" & incn & "b"), gss
                ElseIf .Variables("Bible").Value = cname Then
                    .Variables.Add ("Bible" & incn & "a"), cnum
                    .Variables.Add ("Bible" & incn & "b"), gss
                ElseIf .Variables("Florida History").Value = cname Then
                    .Variables.Add ("oth" & incn & "a"), cnum
                    .Variables.Add ("oth" & incn & "b"), gss
                    .Fields.Update
                End If
            End If
        End With
    Loop
    appWord.ActiveDocument.SaveAs strPath & "MasterRecord" & daST & ".docx"
    Set WordDoc = Nothing
    Set appWord = Nothing
End Sub

With other's help I was able to fix my first problem, but I have another problem where my document has 2 tables, and I know how to get the variable number of the first table, but I don't know how to get the second table. The first table has only 1 row and 6 columns, and I was able to process that one correctly, but when the code was going to what I thought was the second table, it did not. it was still at the first table. I don't know what is the right code to do it.

答案1

得分: 1

我不明白为什么您要用表格的数量来初始化wdcount,以及为什么它被称为wdcount。您不是在统计单词,而是在统计表格。通常情况下,您应该从1或者0开始计数。

您还可以进行倒数计数。那么从表格的数量开始是合适的。但是在每次迭代时,计数器必须减少。

这是我的建议:

...
Dim tableIndex As Integer  ' <=======
...
tableIndex = 1  ' <=======
Do While Not rst.EOF
    With WordDoc
        cname = rst!CourseName
        cnum = rst!CourseNumber
        gss = rst!GradeScore
        incn = rst!IncrementNumber
        ' ===> wdcount = wdcount + 1 <==== 已移除!

        If tableIndex = 1 Then  ' <=======
            .Variables.Add ("StudentName"), daST
            .Variables.Add ("Academic Advisor"), daSUP
            .Variables.Add ("School Year"), daSY
            .Variables.Add ("Grade Level"), daGL
            .Variables.Add ("Beginning Date"), daBEY
            .Variables.Add ("Ending Date"), daENY
           .Fields.Update
        End If
        If tableIndex >= 2 Then  ' <=======
            MsgBox .Variables(6).Name
    
            If .Variables("Math").Value = cname Then
                .Variables.Add ("Math" & incn & "a"), cnum
                .Variables.Add ("Math" & incn & "b"), gss
                ...
            ...
            End If
        End If
    End With
    tableIndex = tableIndex + 1  ' <======= 已添加!
Loop
...

我还怀疑像If .Variables("Math").Value = cname Then这样的条件是否正确。您是否应该像这样从数据库测试课程名称:

Select Case cname
    Case "Math"
        .Variables.Add ("Math" & incn & "a"), cnum
        .Variables.Add ("Math" & incn & "b"), gss
    Case "English"
        .Variables.Add ("Eng" & incn & "a"), cnum
        .Variables.Add ("Eng" & incn & "b"), gss
    Case "Word Building"
        .Variables.Add ("WB" & incn & "a"), cnum
        .Variables.Add ("WB" & incn & "b"), gss
    Case "Literature"   ' <==== 这里应该是"Literature"吗?
        .Variables.Add ("Lit" & incn & "a"), cnum
        .Variables.Add ("Lit" & incn & "b"), gss
    Case "Science"
        .Variables.Add ("Science" & incn & "a"), cnum
        .Variables.Add ("Science" & incn & "b"), gss
    Case "Social Studies"
        .Variables.Add ("SocS" & incn & "a"), cnum
        .Variables.Add ("SocS" & incn & "b"), gss
    Case "Bible"
        .Variables.Add ("Bible" & incn & "a"), cnum
        .Variables.Add ("Bible" & incn & "b"), gss
    Case "Florida History"
        .Variables.Add ("oth" & incn & "a"), cnum
        .Variables.Add ("oth" & incn & "b"), gss
End Select
.Fields.Update   ' <===== 或许应该在`Loop`之后?

或者简化为:

Dim prefix As String
Select Case cname
    Case "Math"
        prefix = "Math"
    Case "English"
        prefix = "Eng"
    Case "Word Building"
        prefix = "WB"
    Case "Literature"   ' <==== 这里应该是"Literature"吗?
        prefix = "Lit"
    Case "Science"
        prefix = "Science"
    Case "Social Studies"
        prefix = "SocS"
    Case "Bible"
        prefix = "Bible"
    Case "Florida History"
        prefix = "oth"
End Select
.Variables.Add (prefix & incn & "a"), cnum
.Variables.Add (prefix & incn & "b"), gss

甚至更简化。首先,我们将cname作为前缀,如果前缀与cname不同,则分配一个更正的版本:

Dim prefix As String
prefix = cname
Select Case cname
    Case "English"
        prefix = "Eng"
    Case "Word Building"
        prefix = "WB"
    Case "Literature"   ' <==== 这里应该是"Literature"吗?
        prefix = "Lit"
    Case "Social Studies"
        prefix = "SocS"
    Case "Florida History"
        prefix = "oth"
End Select
.Variables.Add (prefix & incn & "a"), cnum
.Variables.Add (prefix & incn & "b"), gss

您还没有移动到下一条记录。循环的结尾应该像这样:

...
End With
tableIndex = tableIndex + 1
rst.MoveNext
Loop
WordDoc.Fields.Update
appWord.ActiveDocument.SaveAs strPath & "MasterRecord" & daST & ".docx"
...
英文:

I do not understand why you are initializing wdcount with the number of tables and also why it is called wdcount. You are not counting words, you are counting tables. Usually you start counting at 1 or maybe 0.

You could also count backwards. Then starting with the number of tables would be appropriate. But then the counter would have to be decreased at each iteration.

Here is what I suggest:

...
Dim tableIndex As Integer  &#39;&lt;=======
...
tableIndex = 1  &#39;&lt;=======
Do While Not rst.EOF
    With WordDoc
        cname = rst!CourseName
        cnum = rst!CourseNumber
        gss = rst!GradeScore
        incn = rst!IncrementNumber
        &#39;===&gt; wdcount = wdcount + 1 &lt;==== removed!

        If tableIndex = 1 Then  &#39;&lt;=======
            .Variables.Add (&quot;StudentName&quot;), daST
            .Variables.Add (&quot;Academic Advisor&quot;), daSUP
            .Variables.Add (&quot;School Year&quot;), daSY
            .Variables.Add (&quot;Grade Level&quot;), daGL
            .Variables.Add (&quot;Beginning Date&quot;), daBEY
            .Variables.Add (&quot;Ending Date&quot;), daENY
           .Fields.Update
        End If
        If tableIndex &gt;= 2 Then  &#39;&lt;=======
            MsgBox .Variables(6).Name
    
            If .Variables(&quot;Math&quot;).Value = cname Then
                .Variables.Add (&quot;Math&quot; &amp; incn &amp; &quot;a&quot;), cnum
                .Variables.Add (&quot;Math&quot; &amp; incn &amp; &quot;b&quot;), gss
                ...
            ...
            End If
        End If
    End With
    tableIndex = tableIndex + 1  &#39;&lt;======= ADDED!
Loop
...

I also suspect the conditions like If .Variables(&quot;Math&quot;).Value = cname Then to be wrong. Shouldn't you be testing the course name from the database like this:

Select Case cname
    Case &quot;Math&quot;
        .Variables.Add (&quot;Math&quot; &amp; incn &amp; &quot;a&quot;), cnum
        .Variables.Add (&quot;Math&quot; &amp; incn &amp; &quot;b&quot;), gss
    Case &quot;English&quot;
        .Variables.Add (&quot;Eng&quot; &amp; incn &amp; &quot;a&quot;), cnum
        .Variables.Add (&quot;Eng&quot; &amp; incn &amp; &quot;b&quot;), gss
    Case &quot;Word Building&quot;
        .Variables.Add (&quot;WB&quot; &amp; incn &amp; &quot;a&quot;), cnum
        .Variables.Add (&quot;WB&quot; &amp; incn &amp; &quot;b&quot;), gss
    Case &quot;Lierature&quot;   &#39; &lt;==== Shoudn&#39;t this be &quot;Literature&quot;?
        .Variables.Add (&quot;Lit&quot; &amp; incn &amp; &quot;a&quot;), cnum
        .Variables.Add (&quot;Lit&quot; &amp; incn &amp; &quot;b&quot;), gss
    Case &quot;Science&quot;
        .Variables.Add (&quot;Science&quot; &amp; incn &amp; &quot;a&quot;), cnum
        .Variables.Add (&quot;Science&quot; &amp; incn &amp; &quot;b&quot;), gss
    Case &quot;Social Studies&quot;
        .Variables.Add (&quot;SocS&quot; &amp; incn &amp; &quot;a&quot;), cnum
        .Variables.Add (&quot;SocS&quot; &amp; incn &amp; &quot;b&quot;), gss
    Case &quot;Bible&quot;
        .Variables.Add (&quot;Bible&quot; &amp; incn &amp; &quot;a&quot;), cnum
        .Variables.Add (&quot;Bible&quot; &amp; incn &amp; &quot;b&quot;), gss
    Case &quot;Florida History&quot;
        .Variables.Add (&quot;oth&quot; &amp; incn &amp; &quot;a&quot;), cnum
        .Variables.Add (&quot;oth&quot; &amp; incn &amp; &quot;b&quot;), gss
End Select
.Fields.Update   &#39; &lt;===== maybe this should be after `Loop`?

Or simplified:

Dim prefix As String
Select Case cname
    Case &quot;Math&quot;
        prefix = &quot;Math&quot;
    Case &quot;English&quot;
        prefix = &quot;Eng&quot;
    Case &quot;Word Building&quot;
        prefix = &quot;WB&quot;
    Case &quot;Lierature&quot;   &#39; &lt;==== Shoudn&#39;t this be &quot;Literature&quot;?
        prefix = &quot;Lit&quot;
    Case &quot;Science&quot;
        prefix = &quot;Science&quot;
    Case &quot;Social Studies&quot;
        prefix = &quot;SocS&quot;
    Case &quot;Bible&quot;
        prefix = &quot;Bible&quot;
    Case &quot;Florida History&quot;
        prefix = &quot;oth&quot;
End Select
.Variables.Add (prefix &amp; incn &amp; &quot;a&quot;), cnum
.Variables.Add (prefix &amp; incn &amp; &quot;b&quot;), gss

Or even more simplified. First we take cname as a prefix and assign a corrected version if the prefix differs from cname:

Dim prefix As String
prefix = cname
Select Case cname
    Case &quot;English&quot;
        prefix = &quot;Eng&quot;
    Case &quot;Word Building&quot;
        prefix = &quot;WB&quot;
    Case &quot;Lierature&quot;   &#39; &lt;==== Shoudn&#39;t this be &quot;Literature&quot;?
        prefix = &quot;Lit&quot;
    Case &quot;Social Studies&quot;
        prefix = &quot;SocS&quot;
    Case &quot;Florida History&quot;
        prefix = &quot;oth&quot;
End Select
.Variables.Add (prefix &amp; incn &amp; &quot;a&quot;), cnum
.Variables.Add (prefix &amp; incn &amp; &quot;b&quot;), gss

You are also not moving to the next record. The end of the loop should look like this:

        ...
    End With
    tableIndex = tableIndex + 1
    rst.MoveNext
Loop
WordDoc.Fields.Update
appWord.ActiveDocument.SaveAs strPath &amp; &quot;MasterRecord&quot; &amp; daST &amp; &quot;.docx&quot;
...

huangapple
  • 本文由 发表于 2023年7月27日 21:47:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定