Link to home
Start Free TrialLog in
Avatar of CFMI
CFMIFlag for United States of America

asked on

MS Access 2007 VBA to convert a Word document into a Text file

We receive registration forms through MS Word and we need to import them into MS Access.  Is there VBA code to transfer a Word document into a text file?  A text file is easier to import.
ASKER CERTIFIED SOLUTION
Avatar of carsRST
carsRST
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sabbir_sourove
sabbir_sourove

it's a conversion of a MS word file from ascii to unicode of bangla language.U have to do the same thing but your destination file will be a txt file
class bijoyToUnicode_doc
{
    int cursor;
    Microsoft.Office.Interop.Word.Characters character;
    ////////////////some temp value
    Microsoft.Office.Interop.Word.Range range, bufferRange, saveRange;
    Microsoft.Office.Interop.Word.Document adoc = null;
    char ch;
    bool toggle, chondoBinduEnable;
    object missing = System.Reflection.Missing.Value;
    object one = 1;
    int start, end;

    string[] allChar ={"!", "\"", "#", "¿", "%", "¿", "'", "(", ")", "*", "+", ",", "-", ".", "/",
                        "¿","¿","¿","¿","¿","¿", "¿","¿", "¿", "¿", ":", ";","<","=",">","?","@",
                        "¿", "¿", "¿", "¿", "¿", "¿", "¿", "¿", "¿", "¿" ,"¿","¿","¿","¿","¿","¿",
                                  "¿","¿","¿","¿","¿","¿","¿","¿","¿","¿","[","¿¿","]","¿¿","¿","¿","¿","¿","¿",
                                  "¿","¿","¿","¿","¿","¿","¿","¿","¿","¿","¿","¿","¿","¿","¿","¿","¿","¿","¿",
                                  "¿", "¿", "¿", "¿" ,"{","¿","}","¿", null,null,null,"¿", "¿","¿","¿","¿","¿",
                                  "¿","¿","¿","¿¿", "¿¿¿¿", null, null, null, null, "¿¿¿", "¿¿", "¿", "¿¿", "¿¿",
                                  "¿", "¿", "¿¿", "¿¿", "¿¿", "¿¿", "¿¿",null,null,"¿¿" ,null,"¿¿","¿¿","¿¿¿¿","¿¿",
                                  "¿¿","¿¿","¿¿","¿¿","¿¿","¿¿","¿¿","¿¿","–","¿¿","¿¿","¿¿¿", "¿¿¿", "¿¿¿¿¿", "¿¿¿", "¿¿¿",
                                  "¿¿¿", "¿¿¿","¿¿¿", "¿¿¿", "¿¿¿", "¿¿¿", "¿¿¿", "¿¿¿","¿¿¿", "¿¿¿", "¿¿¿¿", "¿¿¿", "¿¿¿", "¿¿¿", "¿¿¿", "¿¿¿",
                                  "¿¿¿", "¿¿¿", "¿¿¿", "¿¿¿", "¿¿¿", "¿¿¿", "¿¿¿", "¿¿¿", "¿¿", "¿¿¿","¿¿¿", "¿¿¿", "-", "“", "”",
                                  "‘", "’", "¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿" ,"¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿",
                                  "¿","¿¿¿", "¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿¿","¿¿",
                                  "¿¿¿","¿¿","¿¿","¿¿","¿¿¿","¿¿¿","¿¿¿" };





    int karPosition, refPosition, linkPosition, sor_o_Position;

    public void bijoyToUnicode(string inputfile)
    {
        /////////////////////////////////////////////////////////////////////////////////////////////
        EnvDTE80.DTE2 dte;
        object obj = null;
        System.Type t = null;

        // Get the ProgID for DTE 9.0.
        t = System.Type.GetTypeFromProgID("VisualStudio.DTE.9.0", true);
        // Create a new instance of the IDE.
        obj = System.Activator.CreateInstance(t, false);
        // Cast the instance to DTE2 and assign to variable dte.
        dte = (EnvDTE80.DTE2)obj;

        // Register the IOleMessageFilter to handle any threading
        // errors.
        MessageFilter.Register();
        // Display the Visual Studio IDE.
        //dte.MainWindow.Activate();

        // =====================================
        // ==Insert your automation code here.==
        // ===================================
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        Microsoft.Office.Interop.Word.ApplicationClass wordApp = new ApplicationClass();
        toggle = false;
        wordApp.Visible = true;
        //aita MS word ar file khule
        adoc = null;
        Microsoft.Office.Interop.Word.Document saveDoc = null;
        object oCollapseEnd = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
        chondoBinduEnable = false;
        //aita holo file ar directory
        object fileName = inputfile;

        if (File.Exists((string)fileName))
        {
            object readOnly = true;
            object visible = true;
            ///MS word dia akta doc file khulsi
            adoc = wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing,
                                        ref missing, ref missing, ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref visible, ref missing, ref missing,
                                        ref missing, ref missing);



            saveDoc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref visible);
            saveRange = saveDoc.Content.Paragraphs.Add(ref missing).Range;
            saveRange.Text = "";
            bufferRange = adoc.Content;




            refPosition = 0;
            linkPosition = 0;
            sor_o_Position = 0;
            for (start = 0, end = 0; (int)end <= adoc.Content.Characters.Count; )
            {
                ///////////////////////////////////////////////////////////////////////////

                start = (int)end;
                if (adoc.Content.Characters.Count >= (int)start + 100)
                    end = (int)start + 100;
                else
                    end = adoc.Content.Characters.Count + 1;

                bufferRange.SetRange(start, end);

                character = bufferRange.Characters;

                ///////////////////////////////////////////////////////
                for (cursor = 1; cursor <= character.Count; )
                {
                    range = character[cursor];

                    if (range.Font.Name.EndsWith("MJ"))
                    {
                        ch = this.read(range.Text[0]);

                        if (ch >= 65 && ch <= 74)
                            convertVowel();
                        else if ((ch >= 118 && ch <= 138) || ch == 230 || ch == 150 || ch == 147)
                            convertKar();
                        else if (ch == 169)
                        {
                            saveRange.Text = saveRange.Text.Insert(refPosition, "¿¿");
                            cursor++;
                        }
                        else if (ch == 117)
                        {
                            chondoBinduEnable = true;
                            cursor++;
                        }
                        else if (ch >= 75 && ch <= 255)
                            convertConstant();
                        else if (ch == 32 || ch == 9 || ch == 10)
                        {
                            if (karPosition != 0)
                            {

                                saveRange.Text = saveRange.Text + allChar[karPosition];
                                karPosition = 0;
                            }
                            if (chondoBinduEnable)
                            {
                                saveRange.Text = saveRange.Text + "¿";
                                chondoBinduEnable = false;
                            }

                            saveRange.Text = saveRange.Text + character[cursor].Text;
                            cursor++;
                         }
                        else if (ch == 13 || ch==11)
                        {
                            if (karPosition != 0)
                            {

                                saveRange.Text = saveRange.Text + allChar[karPosition];
                                karPosition = 0;
                            }
                            if (chondoBinduEnable)
                            {
                                saveRange.Text = saveRange.Text + "¿";
                                chondoBinduEnable = false;
                            }

                            saveRange.Text = saveRange.Text + (char)13;
                            saveRange.Collapse(ref oCollapseEnd);
                            cursor++;
                        }
                        else if (ch >= 33 && ch <= 64)
                            convertSignNumber();
                        else
                            cursor++;
                    }
                    else
                    {
                        if (karPosition != 0)
                        {
                            saveRange.Text = saveRange.Text + allChar[karPosition];
                            karPosition = 0;
                        }
                         if (chondoBinduEnable)
                        {
                            saveRange.Text = saveRange.Text + "¿";
                            chondoBinduEnable = false;
                        }
                        if(cursor!=0)
                            saveRange.Text = saveRange.Text + character[cursor].Text;
                        cursor++;
                    }
                }
                makeLink();
            }


            object saveFileName = inputfile.Remove(inputfile.LastIndexOf('.')-1) + "_unicode.doc";

            saveDoc.SaveAs(ref saveFileName, 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);



           

            //CLOSING THE FILE

            object oFalse = false;

            adoc.Close(ref oFalse, ref missing, ref missing);
            saveDoc.Close(ref oFalse, ref missing, ref missing);


            //QUITTING THE APPLICATION

            wordApp.Quit(ref missing, ref missing, ref missing);


            ///////////////////////////////////////////////////////
            Solution2 soln = (Solution2)dte.Solution;
            // All done, so shut down the IDE...
            dte.Quit();
            // and turn off the IOleMessageFilter.
            MessageFilter.Revoke();

            ///////////////////////////////////////////////////////////
        }
    }

    void convertVowel()
    {


        if (karPosition != 0)
        {
            saveRange.Text = saveRange.Text + allChar[karPosition];
            karPosition = 0;
        }
        if (chondoBinduEnable)
        {
            saveRange.Text = saveRange.Text + "¿";
            chondoBinduEnable = false;
        }
        if (ch == 65)
        {
            hros_o();
            return;
        }
       

        saveRange.Text = saveRange.Text + allChar[ch - 33];
        cursor++;
    }

    /// <summary>
    /// ///////////////////////////////////////////////////////////////////////////////////////////////////
    /// </summary>
    void hros_o()
    {
        if (cursor < character.Count)
        {
            cursor++;
            if (character[cursor].Text[0] == 118)
            {
                saveRange.Text = saveRange.Text + "¿";
                cursor++;
            }
            else if (character[cursor].Text[0] == 117)
            {
                if (cursor < character.Count)
                {
                    cursor++;
                    if (character[cursor].Text[0] == 118)
                    {
                        saveRange.Text = saveRange.Text + "¿¿";
                        cursor++;
                    }
                    else
                        saveRange.Text = saveRange.Text + "¿¿";
                }
                else
                {
                    sor_o_Position = saveRange.Text.Length;
                    end = end - 2;
                    saveRange.Text = saveRange.Text + "¿¿";
                    cursor++;
                }
            }
            else
                saveRange.Text = saveRange.Text + "¿";
        }
        else
        {
            sor_o_Position = saveRange.Text.Length;
            end = end - 1;
            saveRange.Text = saveRange.Text + "¿";
            cursor++;
        }
    }


    /// <summary>
    /// //////////////////////////////////////////////////////////////////////////////////////////
    /// </summary>

    void convertConstant()
    {
        object one = 1;
        if (ch == 115 || ch == 116)
        {
            if (karPosition != 0)
            {
                saveRange.Text = saveRange.Text + allChar[karPosition];
                karPosition = 0;
            }
            if (chondoBinduEnable)
            {
                saveRange.Text = saveRange.Text + "¿";
                chondoBinduEnable = false;
            }
            saveRange.Text = saveRange.Text + allChar[ch - 33];
            cursor++;
        }
        else if (ch == 105)
        {
            if (karPosition != 0 && toggle)
            {
                saveRange.Text = saveRange.Text + allChar[karPosition];
                karPosition = 0;
            }
            if (chondoBinduEnable)
            {
                saveRange.Text = saveRange.Text + "¿";
                chondoBinduEnable = false;
            }
            toggle = true;
            if (cursor < character.Count && character[cursor+1].Text[0] == 168)
            {
                saveRange.Text = saveRange.Text + "¿¿¿¿";
                cursor = cursor + 2;
            }
            else
            {
                saveRange.Text = saveRange.Text + "¿";
                cursor++;
            }
            return;
        }
        else if (!allChar[ch - 33].StartsWith("¿") && (saveRange.Text == null || !saveRange.Text.EndsWith("¿")))
        {
            if (karPosition != 0 && toggle)
            {
                saveRange.Text = saveRange.Text + allChar[karPosition];
                karPosition = 0;
            }
            if (chondoBinduEnable)
            {
                saveRange.Text = saveRange.Text + "¿";
                chondoBinduEnable = false;
            }
            if (saveRange.Text != null)
            {
                refPosition = saveRange.Text.Length;
                linkPosition = start + cursor - 1;
            }

            saveRange.Text = saveRange.Text + allChar[ch - 33];
            cursor++;
            toggle = true;
        }
        /////////////////////saverange null hoile problem
        else if (allChar[ch - 33].StartsWith("¿") && saveRange.Text != null && saveRange.Text.EndsWith("¿"))
        {
            saveRange.Text = saveRange.Text.Remove(saveRange.Text.Length - 1) + allChar[ch - 33];
            cursor++;
        }
        else
        {
            saveRange.Text = saveRange.Text + allChar[ch - 33];
            cursor++;
        }
    }
Avatar of CFMI

ASKER

CarsRST code works but is there code that also copies the HEADER from Word?