AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

HYPERLINK in WORD OLE mit Delphi 7

Ein Thema von wschrabi · begonnen am 12. Jul 2013 · letzter Beitrag vom 14. Jul 2013
Antwort Antwort
wschrabi

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

HYPERLINK in WORD OLE mit Delphi 7

  Alt 12. Jul 2013, 07:43
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;

Geändert von wschrabi (12. Jul 2013 um 08:53 Uhr)
  Mit Zitat antworten Zitat
nahpets
(Gast)

n/a Beiträge
 
#2

AW: HYPERLINK in WORD OLE mit Delphi 7

  Alt 13. Jul 2013, 18:10
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.
  Mit Zitat antworten Zitat
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
Antwort Antwort

 

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 20:49 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