By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

C# Running a Word Macro messes up Word Setting

P: 1
I am trying to develop a C# program to fill in the blanks in a Word Form. Everything works beautifully while the program is running.

I had to go in to fix some links on our Web Site a couple of days later and found any Word Document containing links now shows the code instead of the link. i.e. {HYPERLINK "Executive%20Summary%20for%20CSI.doc"} instead of CSI. I have traced this error down to my program but don't know what is causing it.

The code is:

public string StartupPath = "c:\\Documents and Settings\\Kym\\My Documents\\";
// Object for missing (or optional) arguments.
object oMissing = System.Reflection.Missing.Value;

public void WordMacro()
{
// Create an instance of Word, make it visible.
// and open 971 Form.doc.
Word.ApplicationClass oWord = new Word.ApplicationClass();
oWord.Visible = true;
Word.Documents oDocs = oWord.Documents;
object oFile = StartupPath + "971 Form.doc";

//if the Microsoft Word 11.0 Object Library is referenced
Word._Document oDoc = oDocs.Open(ref oFile, ref oMissing,
ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
RunMacro (oWord, new Object[] {"MergeDocument"});

}

private void RunMacro(object oApp, object[] oRunArgs)
{
oApp.GetType().InvokeMember("Run",
System.Reflection.BindingFlags.Default |
System.Reflection.BindingFlags.InvokeMethod,
null, oApp, oRunArgs);

}

The code for the Macro in Word is:

Sub MergeDocument()
'
' MergeDocument Macro
' Macro recorded 9/17/2007 by h0014742
'
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
.LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
End With
.Execute Pause:=False
End With
End Sub

When finished, this program is supposed to be distributed company wide. I can't have it messing up other people's Word Program. Can you please help me?
Sep 26 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.