我尝试用两个带Autohotkey的表创建一个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
我在这里做错了什么?
答案 0 :(得分:1)
感谢大家尝试为这个问题提供解决方案。在发布此问题两小时后,我能够找到一个想法。我不得不添加
oWord.Selection.MoveDown(5,1)
前
oWord.Selection.EndKey
并修改
oWord.ActiveDocument.tables.Add(oWord.Selection.Range,5,5)
代替
oWord.ActiveDocument.tables.Add(range,5,5)
并删除了第二个
range := oWord.ActiveDocument.Range(0, 0)
答案 1 :(得分: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")