Laden...

Convertierung VB Nach C#

Erstellt von Scarecraft vor 13 Jahren Letzter Beitrag vor 13 Jahren 2.484 Views
Scarecraft Themenstarter:in
268 Beiträge seit 2008
vor 13 Jahren
Convertierung VB Nach C#

Hallo liebe Comunity,
Ich versuche grad ganz dringend folgenden Artikel :
http://support.microsoft.com/kb/830561/DE/#3(Punkt 3)
nach C# umzubauen.

Orginal VB

Sub Test()
        Dim strFilePath As String
        Dim strPath As String
        Dim intCounter As Integer
        Dim strFileName As String
        Dim OldServer As String
        Dim NewServer As String
        Dim objDoc As Document
        Dim objTemplate As Template
        Dim dlgTemplate As Dialog

        OldServer = "<\\rsnj01\vol1>"
        NewServer = "<\\rsnyc1p\vol3>"
        strFilePath = InputBox("What is the folder location that you want to use?")
        If Right(strFilePath, 1) <> "\" Then strFilePath = strFilePath & "\"
        strFileName = Dir(strFilePath & "*.doc")
        Do While strFileName <> ""
            objDoc = Documents.Open(strFilePath & strFileName)
            objTemplate = objDoc.AttachedTemplate
            dlgTemplate = Dialogs(wdDialogToolsTemplates)
            strPath = dlgTemplate.Template
            If LCase(Left(strPath, 13)) = LCase(OldServer) Then
                objDoc.AttachedTemplate = NewServer & Mid(strPath, 14)
            End If
            strFileName = Dir()
            objDoc.Save()
            objDoc.Close()
        Loop
        objDoc = Nothing
        objTemplate = Nothing
        dlgTemplate = Nothing

    End Sub

Das C# Programm:


 
    void Test() {
        string strFilePath;
        string strPath;
        int intCounter;
        string strFileName;
        string OldServer;
        string NewServer;
        Document objDoc;
        Template objTemplate;
        Dialog dlgTemplate;
        OldServer = "<\\\\rsnj01\\vol1>";
        NewServer = "<\\\\rsnyc1p\\vol3>";
        strFilePath = InputBox("What is the folder location that you want to use?");
        if ((strFilePath.Substring((strFilePath.Length - 1)) != "\\")) {
            strFilePath = (strFilePath + "\\");
        }
        strFileName = Dir((strFilePath + "*.doc"));
        while ((strFileName != "")) {
            objDoc = Documents.Open((strFilePath + strFileName));
            objTemplate = objDoc.AttachedTemplate;
            dlgTemplate = Dialogs(wdDialogToolsTemplates);
            strPath = dlgTemplate.Template;
            if ((strPath.Substring(0, 13).ToLower() == OldServer.ToLower())) {
                objDoc.AttachedTemplate = (NewServer + strPath.Substring(13));
            }
            strFileName = Dir();
            objDoc.Save();
            objDoc.Close();
        }
        objDoc = null;
        objTemplate = null;
        dlgTemplate = null;
    }

Leider hab ich mal so keine ahnung von VB. Und stoße bei Folgendem auf Problemen:

Die Objekte :
Document ,Template, Dialog
(wobei letzteres net so das Problem sein dürfte)
werden nicht erkannt. Ich hab dannach gegooglet ob evtl. ein verweis fehlt aber naja das ist bei solchen Namen wie die Nadel im Heuhaufen.

Dann erkennt er "Dir" nicht. Aber das ist glaubich ne VB sache. Gibt es dazu nen C# Äquivalent?(vielleicht Directory?)
Naja das wars auch eigendlich. Ich brauche das Script halt dringend am laufen kann mir bitte bitte wer helfen ?

Danke schonmal im vorraus
Christopher

Name: Scarecraft
Language:C#
learning since:winter 2007
IDE: Visual Studio 2005/08 Professional Editon
Skill:Medium
qualifications: MCP - Microsoft Certified Professional
Homepage : FX RPG Maker

Gelöschter Account
vor 13 Jahren

Also diese Objekte die er nicht erkennt sind Objekte die dir in VBA in Word
automatisch zur Verfügung stehen. Im Klartext Word.Application.
In C# musst du Word natürlich referenzieren und eine Instanz ertellen.
Documents zb. ist:

Word.Application wordApp = new Word.Application();
wordApp.Documents;

Dir ist sowas wie System.IO.File.Exists kurz und knapp gesagt.

Scarecraft Themenstarter:in
268 Beiträge seit 2008
vor 13 Jahren

Also diese Objekte die er nicht erkennt sind Objekte die dir in VBA in Word
automatisch zur Verfügung stehen. Im Klartext Word.Application.
In C# musst du Word natürlich referenzieren und eine Instanz ertellen.
Documents zb. ist:

Word.Application wordApp = new Word.Application();
wordApp.Documents;

Dir ist sowas wie System.IO.File.Exists kurz und knapp gesagt.

Okay vielen Dank ich bin nun ein stückchen weiter. Er erkennt nun die Objekte. Leider sind die wohl auch nen bissl anders. Und er will bei

 objDoc = Documents.Open(strFilePath + strFileName);

das nicht schlucken. Er sagt nur der Pfad wäre ihm zu wenig. Und bietet mir an da geschätzte 20 argumente mehr reinzupacken. Nur hab ich keine ahnung was ich da noch alles reinpacken soll. Ich hab das ganze mal als screenshoot angehängt.

Im internet hab ich noch das hier gefunden :

object fileName = @"C:\Test\NewDocument.doc"; 

this.Application.Documents.Open(ref fileName,
    ref missing, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing);

bzw in meinem Fall:

objDoc = Documents.Open(ref strFilePath+strFileName,
     ref Type.Missing, ref Type.Missing, ref Type.Missing, ref Type.Missing, ref Type.Missing,
    ref Type.Missing, ref Type.Missing, ref Type.Missing, ref Type.Missing, ref Type.Missing,
    ref Type.Missing, ref Type.Missing, ref Type.Missing, ref Type.Missing, ref Type.Missing);

aber das versteh ich nicht ganz.Da meckert der auch wieder rum.. ich will doch nur das das Programm läuft 🙁
hat einer eine ahnung wie ich dem Document.open nun nur den Pfad mitgeben kann ?..mehr hab ich ja auch nicht.

Wieso kann M$ denn das ganze nicht als .exe oder auch in C# anbieten ich hab hier die Bude am brennen und bekomm das net ans laufen ...

mein Sourcecode bisher:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Scripting;
using System.Threading;
using Microsoft.Office.Interop.Word;
namespace Mainka_Vorlagen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox3.Text != "" && textBox3.Text != null && textBox1.Text != "" && textBox1.Text != null && textBox2.Text != "" && textBox2.Text != null)
            {
                System.Threading.Thread newThread;
                newThread = new System.Threading.Thread(Test);
                newThread.Start();
               
            }
            else
                MessageBox.Show("Bitte zuerst alle Felder ausfüllen!");
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }


        void Test()
        {
            try
            {
                //Deklaration

                string strFilePath;
                string strPath;
                int intCounter;
                string strFileName;
                string OldServer;
                string NewServer;


                Document objDoc = new Document();
                Template objTemplate ;
                Dialog dlgTemplate;

                //einlesen der Servernamen//Wertzuweisung
                OldServer = textBox1.Text;
                NewServer = textBox2.Text;
                strFilePath = textBox3.Text;

                //Wenn Pfadname am anfang nicht \\ hat diesen einfügen
                if ((strFilePath.Substring((strFilePath.Length - 1)) != "\\"))
                {
                    strFilePath = (strFilePath + "\\");
                }
                //Dateiendung hinzufügen
                strFileName = strFilePath + "*.doc";

                while ((strFileName != ""))
                {
                    object name = strFilePath + strFileName;
                    
                    object no = false;

                    objDoc = Documents.Open(ref name, ref no, ref no, ref no, ref no, ref no, ref no, ref no, ref no, ref no, ref no, ref no, ref no, ref no, ref no, ref no);

                    objTemplate = objDoc.get_AttachedTemplate();
                    dlgTemplate = Dialogs(wdDialogToolsTemplates);
                    strPath = dlgTemplate.Template;
                    if ((strPath.Substring(0, 13).ToLower() == OldServer.ToLower()))
                    {
                        objDoc.AttachedTemplate = (NewServer + strPath.Substring(13));
                    }
                    strFileName = Dir();
                    objDoc.Save();
                    object save = true;
                    objDoc.Close(ref save);
                }
                objDoc = null;
                objTemplate = null;
                dlgTemplate = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler:" + ex.Message);

            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
           textBox3.Text= folderBrowserDialog1.SelectedPath;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = textBox1.Text;

            openFileDialog1.ShowDialog();
            if (openFileDialog1.FileName!= "")
           textBox1.Text= openFileDialog1.FileName;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = textBox2.Text;
            openFileDialog1.ShowDialog();
            if (openFileDialog1.FileName != "")
                textBox2.Text = openFileDialog1.FileName;
        }

        
    }
}

Gruß Christopher

Name: Scarecraft
Language:C#
learning since:winter 2007
IDE: Visual Studio 2005/08 Professional Editon
Skill:Medium
qualifications: MCP - Microsoft Certified Professional
Homepage : FX RPG Maker

Gelöschter Account
vor 13 Jahren

Was genau meckert er denn rum bei Open und welche Word Version hast du?
Evtl. mehrere Word Versionen auf dem System?

Google bietet mit "C#" "Word" "Documents" "Open" eigentlich einige gute Beispiele.

Auch einen MyCsharp Thread Word Dokument öffnen und auf Inhalt zugreifen in C#-Anwendung mit einem Code Snippet von sheitman.
Da scheint Documents.Open zu funktionieren.

61 Beiträge seit 2009
vor 13 Jahren

Die Konvertierung leicht gemacht: (funktioniert sehr gut - jedenfalls C# to VB)
DeveloperFusion

Den SourceCode gibt es auch. Der ist OpenSource.
SourceCode - SharpDevelop 3.2

In der Zeit vor fünf Minuten ist Jetzt die Zukunft. Jetzt ist die Gegenwart. Die Zeit, in der ich zu erzählen begonnen habe, ist die Vergangenheit von Jetzt und die Zukunft von der Gegenwart der Zeit, fünf Minuten bevor ich zu erzählen begann.

Scarecraft Themenstarter:in
268 Beiträge seit 2008
vor 13 Jahren

Ich hab es herausgefunden. Danke an alle die mir geholfen haben.gruß Christopher

Name: Scarecraft
Language:C#
learning since:winter 2007
IDE: Visual Studio 2005/08 Professional Editon
Skill:Medium
qualifications: MCP - Microsoft Certified Professional
Homepage : FX RPG Maker