none
Word 2003: Search and Replace RRS feed

  • Frage

  • Hi,

    möchte / muss massenhaft Word 2003 Dateien bearbeiten, indem Strings (UNC-Pfade) ausgetauscht werden.

    $word = New-Object -ComObject Word.Application

    $word.Visible = $true

    $document = $word.Documents.Open("d:\docfile.doc")

    $document.Activate()

    $objSelection= $word.Selection

    $objSelection.Find.Text = "<searchstring>"

    $objSelection.Find.Forward = $true

    $objSelection.Find.MatchWholeWord = $true

    $objSelection.Find.Replacement.Text = "<replacestring>"

    $objSelection.Find.Execute ,,,,,,,,,,2

    $document.Save()

    $word.ActiveDocument.Close()

    $word.Quit()

    Leider wird nichts ersetzt. Wo liegt der Fehler.

    Ein VBS Script http://technet.microsoft.com/en-us/library/ee692875.aspx 

    funktioniert dagegen.

    Gruss Olaf

    Donnerstag, 6. Januar 2011 20:06

Antworten

  • Ich denke du hast einen Fehler in der Parameterzuweisung, schau dir mal diese Funktion von Niklas an, die geht:

    function Replace-Word ([string]$Document,[string]$FindText,[string]$ReplaceText) {
    
    	#Variables used to Match And Replace
    
    	$ReplaceAll = 2
    	$FindContinue = 1
    
    	$MatchCase = $False
    	$MatchWholeWord = $True
    	$MatchWildcards = $False
    	$MatchSoundsLike = $False
    	$MatchAllWordForms = $False
    	$Forward = $True
    	$Wrap = $FindContinue
    	$Format = $False
    
    	$Word = New-Object -comobject Word.Application
    	$Word.Visible = $False
    
    	$OpenDoc = $Word.Documents.Open($Document)
    	$Selection = $Word.Selection
    
    	$Selection.Find.Execute(
    		$FindText,
    		$MatchCase,
    		$MatchWholeWord,
    		$MatchWildcards,
    		$MatchSoundsLike,
    		$MatchAllWordForms,
    		$Forward,
    		$Wrap,
    		$Format,
    		$ReplaceText,
    		$ReplaceAll
    	)
    
    	$OpenDoc.Close()
    }
    

    Grüße, Denniver

    Donnerstag, 6. Januar 2011 23:21
    Moderator
  • Bezüglich Excel, schau mal hier und hier und für Powerpoint hier.

    Hoffe das hilft weiter.

    Denniver

    Freitag, 7. Januar 2011 19:17
    Moderator

Alle Antworten

  • Ich denke du hast einen Fehler in der Parameterzuweisung, schau dir mal diese Funktion von Niklas an, die geht:

    function Replace-Word ([string]$Document,[string]$FindText,[string]$ReplaceText) {
    
    	#Variables used to Match And Replace
    
    	$ReplaceAll = 2
    	$FindContinue = 1
    
    	$MatchCase = $False
    	$MatchWholeWord = $True
    	$MatchWildcards = $False
    	$MatchSoundsLike = $False
    	$MatchAllWordForms = $False
    	$Forward = $True
    	$Wrap = $FindContinue
    	$Format = $False
    
    	$Word = New-Object -comobject Word.Application
    	$Word.Visible = $False
    
    	$OpenDoc = $Word.Documents.Open($Document)
    	$Selection = $Word.Selection
    
    	$Selection.Find.Execute(
    		$FindText,
    		$MatchCase,
    		$MatchWholeWord,
    		$MatchWildcards,
    		$MatchSoundsLike,
    		$MatchAllWordForms,
    		$Forward,
    		$Wrap,
    		$Format,
    		$ReplaceText,
    		$ReplaceAll
    	)
    
    	$OpenDoc.Close()
    }
    

    Grüße, Denniver

    Donnerstag, 6. Januar 2011 23:21
    Moderator
  • Hallo Denniver,

    ja danke. Funktioniert super.

    Jetzt brauche ich nur noch was für Excel und Powerpoint.

    Hat jemand dafür auch Samples?

    Gruss Olaf

    Freitag, 7. Januar 2011 09:28
  • Bezüglich Excel, schau mal hier und hier und für Powerpoint hier.

    Hoffe das hilft weiter.

    Denniver

    Freitag, 7. Januar 2011 19:17
    Moderator