CFMI
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.W ord.Charac ters character;
////////////////some temp value
Microsoft.Office.Interop.W ord.Range range, bufferRange, saveRange;
Microsoft.Office.Interop.W ord.Docume nt 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.GetTypeFromPro gID("Visua lStudio.DT E.9.0", true);
// Create a new instance of the IDE.
obj = System.Activator.CreateIns tance(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.W ord.Applic ationClass wordApp = new ApplicationClass();
toggle = false;
wordApp.Visible = true;
//aita MS word ar file khule
adoc = null;
Microsoft.Office.Interop.W ord.Docume nt saveDoc = null;
object oCollapseEnd = Microsoft.Office.Interop.W ord.WdColl apseDirect ion.wdColl apseEnd;
chondoBinduEnable = false;
//aita holo file ar directory
object fileName = inputfile;
if (File.Exists((string)fileN ame))
{
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.Co unt; )
{
////////////////////////// ////////// ////////// ////////// ////////// /////////
start = (int)end;
if (adoc.Content.Characters.C ount >= (int)start + 100)
end = (int)start + 100;
else
end = adoc.Content.Characters.Co unt + 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(refP osition, "¿¿");
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 .LastIndex Of('.')-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;
}
/////////////////////saver ange null hoile problem
else if (allChar[ch - 33].StartsWith("¿") && saveRange.Text != null && saveRange.Text.EndsWith("¿ "))
{
saveRange.Text = saveRange.Text.Remove(save Range.Text .Length - 1) + allChar[ch - 33];
cursor++;
}
else
{
saveRange.Text = saveRange.Text + allChar[ch - 33];
cursor++;
}
}
{
int cursor;
Microsoft.Office.Interop.W
////////////////some temp value
Microsoft.Office.Interop.W
Microsoft.Office.Interop.W
char ch;
bool toggle, chondoBinduEnable;
object missing = System.Reflection.Missing.
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.GetTypeFromPro
// Create a new instance of the IDE.
obj = System.Activator.CreateIns
// 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.W
toggle = false;
wordApp.Visible = true;
//aita MS word ar file khule
adoc = null;
Microsoft.Office.Interop.W
object oCollapseEnd = Microsoft.Office.Interop.W
chondoBinduEnable = false;
//aita holo file ar directory
object fileName = inputfile;
if (File.Exists((string)fileN
{
object readOnly = true;
object visible = true;
///MS word dia akta doc file khulsi
adoc = wordApp.Documents.Open(ref
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
saveRange.Text = "";
bufferRange = adoc.Content;
refPosition = 0;
linkPosition = 0;
sor_o_Position = 0;
for (start = 0, end = 0; (int)end <= adoc.Content.Characters.Co
{
//////////////////////////
start = (int)end;
if (adoc.Content.Characters.C
end = (int)start + 100;
else
end = adoc.Content.Characters.Co
bufferRange.SetRange(start
character = bufferRange.Characters;
//////////////////////////
for (cursor = 1; cursor <= character.Count; )
{
range = character[cursor];
if (range.Font.Name.EndsWith(
{
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(refP
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
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]
{
saveRange.Text = saveRange.Text + "¿";
cursor++;
}
else if (character[cursor].Text[0]
{
if (cursor < character.Count)
{
cursor++;
if (character[cursor].Text[0]
{
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
{
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;
}
/////////////////////saver
else if (allChar[ch - 33].StartsWith("¿") && saveRange.Text != null && saveRange.Text.EndsWith("¿
{
saveRange.Text = saveRange.Text.Remove(save
cursor++;
}
else
{
saveRange.Text = saveRange.Text + allChar[ch - 33];
cursor++;
}
}
ASKER
CarsRST code works but is there code that also copies the HEADER from Word?