Einzelnen Beitrag anzeigen

wschrabi

Registriert seit: 16. Jan 2005
437 Beiträge
 
#3

AW: HYPERLINK in WORD OLE mit Delphi 7

  Alt 14. Jul 2013, 17:23
Danke Dir, werde ich probieren.
Hat leider nicht geklappt. Habs so gelöst:#
Code:
function VarIsNothing(V: OleVariant): Boolean;
begin
          ShowMessage('Lenght:'+Inttostr(Length(V)));
          ShowMessage(V);

  Result :=
  (*
    (TVarData(V).VType = varDispatch)
    and
    (TVarData(V).VDispatch = nil)
    and *)
    (Length(V)=0);
end;

procedure TForm1.wordformular;
const
    wdFindContinue = 1;
    wdReplaceOne = 1;
    wdReplaceAll = 2;
var Word: Variant;
    doc,sel: OleVariant;
    WordTabelle: OleVariant;
    s: String;
begin
  //screen.Cursor := crHourglass;
  try
    // Läuft Word noch nicht, wird eine neue Verbindung aufgebaut
    Word := CreateOleObject('Word.Application');
  except
    // Schlägt sie fehl (Word nicht installiert), gibt es eine Fehlermeldung
    ShowMessage('Microsoft Word kann nicht starten.');
    screen.cursor := crDefault;
    exit;
  end;

  try
    if OpenDialog2.Execute then
      s := OpenDialog2.FileName;
    Word.Documents.Open(s);
  except
    Application.MessageBox('Fehler beim Laden der Datei aufgetreten', 'Achtung', 48);
  end;

  //Word.Documents.Add;

  word.visible := true;

  try
    //Übergabe des aktiven Dokuments in die Variable doc
    doc := word.ActiveDocument;
  except
    Application.MessageBox('No Ative Document!', 'Achtung', 48);
    word.Application.Quit;
    exit;

  end;


  word.Selection.Find.ClearFormatting;
  word.Selection.Find.Text := 'Http://*'+' ';
  word.Selection.Find.Replacement.Text := 'MYURL'+#$D;
  word.Selection.Find.Forward := True;
  word.Selection.Find.Wrap := wdFindContinue;
  word.Selection.Find.Format := False;
  word.Selection.Find.MatchCase := True;
  word.Selection.Find.MatchWholeWord := False;
  word.Selection.Find.MatchWildcards := True;
  word.Selection.Find.MatchSoundsLike := False;
  word.Selection.Find.MatchAllWordForms := False;
  // Hier kann ein Haltepunkt gesetzt werden um im geöffneten Word-Dokument ein zusätzliches
  // Hallo Welt, außerhalb der Tabelle einzufügen     word.Selection.Find.Execute(Replace := wdReplaceAll);
  word.Selection.Find.Execute();
  sel := word.Selection.Range;

  if not(VarIsNothing(sel)) then
  begin
    try
      repeat
        if not(VarIsNothing(sel)) then
          begin
          if Form2.Checkbox3.Checked then
            word.ActiveDocument.Hyperlinks.Add(Anchor := sel, Address :=sel, TextToDisplay:=Form2.Edit1.Text)
          else
            word.ActiveDocument.Hyperlinks.Add(Anchor := sel, Address :=sel);
          word.Selection.Find.Execute();
          sel := word.Selection.Range;
          end
        else
          begin
          Application.MessageBox('Kein weitere LINK gefunden', 'Achtung', 48);
          end;
      until VarIsNothing(sel);


    except
      Application.MessageBox('Error aufgetreten!', 'Achtung', 48);
      word.Application.Quit;

    end;
  end
   else
   begin
   Application.MessageBox('Kein LINK gefunden', 'Achtung', 48);
   end;

  Application.MessageBox('Finished! Kein weitere LINK gefunden', 'Achtung', 48);
  word.Application.Quit;
end;

Geändert von wschrabi (15. Jul 2013 um 07:49 Uhr)
  Mit Zitat antworten Zitat