自动热键word com obj添加多个表

自动热键word com obj添加多个表,com,ms-word,autohotkey,Com,Ms Word,Autohotkey,我尝试创建一个word文档,其中包含两个带有自动热键的表。我可以成功地添加一个表并键入一些文本。现在,我尝试在旧表下面的同一文档中创建另一个表 oWord := ComObjCreate("Word.Application") ; create MS Word object Document := oWord.Documents.Add ; create new document oWord.Visible := 1 ; Ma

我尝试创建一个word文档,其中包含两个带有自动热键的表。我可以成功地添加一个表并键入一些文本。现在,我尝试在旧表下面的同一文档中创建另一个表

oWord := ComObjCreate("Word.Application")       ; create MS Word object
Document := oWord.Documents.Add             ; create new document
oWord.Visible := 1                  ; Make winword visible
range := oWord.ActiveDocument.Range(0, 0)       ; Set Range
oWord.ActiveDocument.tables.Add(range,1,2)      ; Add table in range
oWord.Selection.Tables(1).Style := "Table Grid"     ; set style
oWord.Selection.Tables(1).Cell(1,2).Range.Select    ; select a cell
oWord.Selection.TypeText("Hi hi")           ; type a text in selected cell
oWord.Selection.EndKey                  ; from here I couldn't able to create a new table
oWord.Selection.TypeParagraph
range := oWord.ActiveDocument.Range(0, 0) 
oWord.ActiveDocument.tables.Add(range,10,5)
oWord.Selection.Tables(1).Style := "Table Grid"
oWord.Selection.Tables(1).Cell(1,3).Range.Select    ; get error 0x800A1735 and it mentions 'Cell' The requested member of the collection does not exist
;oWord.Selection.TypeText("Hi di")
oWord.Quit

我到底做错了什么?

感谢大家为这个问题提供解决方案。在发布这个问题两小时后,我能想出一个主意。我必须补充一点

oWord.Selection.MoveDown(5,1)

以前

oWord.Selection.EndKey

修改

oWord.ActiveDocument.tables.Add(oWord.Selection.Range,5,5)

反而

oWord.ActiveDocument.tables.Add(范围5,5)

并删除了第二个

range:=oWord.ActiveDocument.range(0,0)

;OP reports being unable to add a second table. 
;The following AutoHotkey code demonstrates the use of Selection.Range to properly place the additional table.

full_command_line := DllCall("GetCommandLine", "str")

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
    try ; leads to having the script re-launching itself as administrator
    {
        if A_IsCompiled
            Run *RunAs "%A_ScriptFullPath%" /restart
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
    }
    ExitApp
}

oWord := ComObjCreate("Word.Application")       
; create MS Word object
Document := oWord.Documents.Add             
; create new document
oWord.Visible := 1                  ; Make winword visible
range := oWord.Selection.Range       
; Set Range - this was at the heart of the problem
; reported by OP
oWord.ActiveDocument.tables.Add(range,11,5)      
; Add table in range with 11 rows and 5 columns
oWord.Selection.Tables(1).Style := "Table Grid"    
; set style to Table Grid
oWord.Selection.Tables(1).Cell(1,1).Range.Select    
; select a cell ("A1" in Excel parlance)
oWord.Selection.TypeText("A1")           
; type identifying text in selected cell
oWord.Selection.MoveDown(5, 12, 0)           
; wdLine := 5, Count has no constant. wdMove is 0, wdExtend is 1
oWord.Selection.TypeText("Ending first table.")
oWord.Selection.TypeParagraph
oWord.Selection.TypeText("Starting second table.") ; Just what is to be expected.
oWord.Selection.TypeParagraph
range2 := oWord.Selection.Range
; Set Range - this was at the heart of the problem
; reported by OP
oWord.ActiveDocument.tables.Add(range2,11,5)      
; Add another table in range
oWord.Selection.Tables(1).Style := "Table Grid"    
; set style
oWord.Selection.Tables(1).Cell(2,2).Range.Select    
; select a cell (would be "B2" if this were Excel)
oWord.Selection.TypeText("B2")           
; type identifying text in selected cell
oWord.Selection.MoveDown(5, 12, 0)           
; wdLine := 5, Count has no constant. wdMove is 0, wdExtend is 1
oWord.Selection.TypeText("End of table demonstration")