Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   HYPERLINK in WORD OLE mit Delphi 7 (https://www.delphipraxis.net/175726-hyperlink-word-ole-mit-delphi-7-a.html)

wschrabi 12. Jul 2013 07:43

HYPERLINK in WORD OLE mit Delphi 7
 
Hallo,
Ich habe ein WORD File (Word 2010) und darin einige URLs, die jedoch nicht als Hyperlinks eingebettet sind. Jetzt möchte ich ien Delphi 7 Prg schreiben, die diese URLs in eingebettete Hyperlinks umwandelt. Wer kann mir damit helfen?
DANKE!

Konnte das Problem schon lösen, doch eine FRAGe:
Code:
if sel = ''
macht er nicht, daher habe ich mit try gearbeitet. Wie kann man abfragen ob sel <> '' ist?

Code:
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;

  //Übergabe des aktiven Dokuments in die Variable doc
  doc := word.ActiveDocument;
  //Tabelle erzeugen mit 5 Zeilen und 5 Spalten

  (*
  WordTabelle := doc.Tables.Add(Word.selection.range, 5, 5,
                 emptyParam,emptyParam) ;


  //!!!!!!!!!!!Der Index einer Tabelle in Word beginnt immer mit 1,
  //nicht mit 0 wie in Delphi!!!!!!!!!!!!!!!!!!!!!!!

  //Schriftgröße in der 2. Zeile und 2. Spalte auf 14 setzen
  WordTabelle.cell(2,2).range.font.size := 14;
  //Schriftgrad in der 2. Zeile und 2.Spalte aud Fett setzen
  WordTabelle.cell(2,2).range.font.bold := true;
  //Textübergabe in die 2. Zeile und 2. Spalte
  WordTabelle.cell(2,2).range.text := 'Hallo Welt';
  *)

  word.Selection.Find.ClearFormatting;
  word.Selection.Find.Text := 'Http://*'+#$D;
  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();
  try
    repeat
      sel := word.Selection.Range;
      word.ActiveDocument.Hyperlinks.Add(Anchor := sel, Address :=sel);
      word.Selection.Find.Execute();
    until false;
  except
    Application.MessageBox('Kein weitere LINK gefunden', 'Achtung', 48);
  end;

  word.Application.Quit;
end;

nahpets 13. Jul 2013 18:10

AW: HYPERLINK in WORD OLE mit Delphi 7
 
Bin mir nicht ganz sicher, könnte eventuell mit
Delphi-Quellcode:
  ...
  word.Selection.Find.Execute();
  if not (word.Selection.Range = EmptyVar) then
  try
    repeat
      sel := word.Selection.Range;
      word.ActiveDocument.Hyperlinks.Add(Anchor := sel, Address :=sel);
      word.Selection.Find.Execute();
    until word.Selection.Range = EmptyVar;
  except
    Application.MessageBox('Kein weitere LINK gefunden', 'Achtung', 48);
  end;
gehen. Die Unit Variants wird benötigt.

wschrabi 14. Jul 2013 17:23

AW: HYPERLINK in WORD OLE mit Delphi 7
 
Danke Dir, werde ich probieren.:-D
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;


Alle Zeitangaben in WEZ +1. Es ist jetzt 22:19 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz