Suchbegriffe aus einer Excel-Datei in einer Word-Datei suchen und das Ergebnis in Excel (neue Arbeitsblatts) schreiben. Topic is solved

Stelle Fragen zur Programmierung mit Autohotkey

Moderator: jNizM

Salah
Posts: 20
Joined: 17 Mar 2022, 14:37

Suchbegriffe aus einer Excel-Datei in einer Word-Datei suchen und das Ergebnis in Excel (neue Arbeitsblatts) schreiben.

Post by Salah » 19 May 2022, 07:40

Hallo zusammen,

Ich versuche mit diesem Skript, Suchbegriffe aus einer "Excel-Datei" in einer "Word-Datei" zu finden und danach das Ergebnis in Excel (neue Arbeitsblatts) zu schreiben.

Code: Select all

#NoEnv
#Persistent
#SingleInstance,Force
#NoTrayIcon

#!h::
{
oWord := ComObjActive("word.application")
oWord.Selection.WholeStory
var := oWord.selection.text
msgbox % var

Xl := ComObjActive("excel.application")
arr := [],colcnt := Xl.Activesheet.UsedRange.Columns.count
for c in Xl.Activesheet.UsedRange.Columns(1).Cells
	if instr(var,c.value)
		arr.push(c.entirerow.value)   						

path := "C:\xxx\test2.xlsx" 
wrkbk := Xl.workbooks.open(path)
nwsht := wrkbk.activesheet
for a,b in arr
;msgbox % a
	nwsht.range(nwsht.cells(a,1),nwsht.cells(a, colcnt)) := b      	
;msgbox % b
return
}
Wenn ich das Ergebnis in eine TXT Datei schreibe, funktioniert es, beim Excel leider nicht, gibt es eine Erklärung dafür? Oder gibt es eine bessere Alternative?

Vielen Dank!

Salah
Posts: 20
Joined: 17 Mar 2022, 14:37

Re: Suchbegriffe aus einer Excel-Datei in einer Word-Datei suchen und das Ergebnis in Excel (neue Arbeitsblatts) schreib  Topic is solved

Post by Salah » 19 May 2022, 11:53

Die Lösung:

Code: Select all

#!h::
{
oWord := ComObjActive("word.application")
oWord.Selection.WholeStory
var := oWord.selection.text
;msgbox % var := oWord.selection.text

filepath := "C:\xxx\test.csv"
Xl := ComObjCreate("excel.application")
Xl.DisplayAlerts := False
wrkbk := Xl.workbooks.open(filepath)

;msgbox % Xl.range("a1").text

arr := [],colcnt := Xl.Activesheet.UsedRange.Columns.count
for c in Xl.Activesheet.UsedRange.Columns(1).Cells
	{
	msgbox % c.value
	if instr(var,c.value)
		arr.push(c.entirerow.value)   ; arr is an array containing safearrays
	}	
nwsht := Xl.workbooks.add.activesheet
for a,b in arr
	nwsht.range(nwsht.cells(a,1),nwsht.cells(a, colcnt)) := b
return
}

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Suchbegriffe aus einer Excel-Datei in einer Word-Datei suchen und das Ergebnis in Excel (neue Arbeitsblatts) schreib

Post by BoBo » 19 May 2022, 12:08

Worin lag jetzt genau die Lösung, im Wechsel von einer *.xlsx zu einer *.csv-Datei?

Aha, also ein Zugriffskonflikt, heißt, das Script war nicht fehlerhaft: viewtopic.php?p=463052#p463052 :eh: :thumbup:

Salah
Posts: 20
Joined: 17 Mar 2022, 14:37

Re: Suchbegriffe aus einer Excel-Datei in einer Word-Datei suchen und das Ergebnis in Excel (neue Arbeitsblatts) schreib

Post by Salah » 19 May 2022, 12:52

BoBo wrote:
19 May 2022, 12:08
Worin lag jetzt genau die Lösung, im Wechsel von einer *.xlsx zu einer *.csv-Datei?

Aha, also ein Zugriffskonflikt, heißt, das Script war nicht fehlerhaft: viewtopic.php?p=463052#p463052 :eh: :thumbup:
Genau, vielen Dank! :thumbup:

Post Reply

Return to “Ich brauche Hilfe”