Remove unwanted section breaks from Word? - c#

I am trying to create an envelope in MS Word. The following code will create an envelope, but I get "section break (Next Page)" at the top of that page. I would like to remove that.
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDoc.Activate();
object ExtractAddress = false;
object Address = "Address" ;
object AutoText = "AutoText" ;
object OmitReturnAddress = false;
object ReturnAddress = "ReturnAddress" ;
object ReturnAutoText = "ReturnAutoText";
object PrintBarCode = false;
object PrintFIMA = false;
object Size = "E65";
object Height = 110;
object Width = 220;
object FeedSource = true;
object AddressFromLeft = 2;
object AddressFromTop = 2;
object ReturnAddressFromLeft = 2;
object ReturnAddressFromTop = 2;
object DefaultFaceUp = true;
object DefaultOrientation = Microsoft.Office.Interop.Word.WdEnvelopeOrientation.wdCenterPortrait;
object PrintEPostage = false;
object Vertical = false;
object RecipientNamefromLeft = Missing.Value;
object RecipientNamefromTop = Missing.Value;
object RecipientPostalfromLeft = Missing.Value;
object RecipientPostalfromTop = Missing.Value;
object SenderNamefromLeft = Missing.Value;
object SenderNamefromTop = Missing.Value;
object SenderPostalfromLeft = Missing.Value;
object SenderPostalfromTop = Missing.Value;
oDoc.Envelope.Insert(ref ExtractAddress, ref Address, ref AutoText,
ref OmitReturnAddress, ref ReturnAddress, ref ReturnAutoText,
ref PrintBarCode, ref PrintFIMA, ref Size, ref Height,
ref Width, ref FeedSource, ref AddressFromLeft, ref AddressFromTop,
ref ReturnAddressFromLeft, ref ReturnAddressFromTop, ref DefaultFaceUp,
ref DefaultOrientation, ref PrintEPostage, ref Vertical,
ref RecipientNamefromLeft, ref RecipientNamefromTop,
ref RecipientPostalfromLeft, ref RecipientPostalfromTop,
ref SenderNamefromLeft, ref SenderNamefromTop, ref SenderPostalfromLeft,
ref SenderPostalfromTop);

After fiddeling around with the envelope feature of Word, I noticed that this is not a programming question (although you made it looking like one). If you insert an envelope manually into a document, you will get also a section break, which splits the envelope part from the rest of the document, since both have different paper sizes. I did not find an easy way to get rid of that section break easily while still keeping the envelope size intact, so here is my advice to get this right:
don't use the "envelope" feature of Word
adjust paper size and margins manually (in Word, using the menus/ribbons), and place your Address and return address at the positions where you want them to be
use the macro recorder to record the VBA commands to do this
port the VBA code to C#

Related

microsoft.office.interop.word replace table text inside shape

I'm using microsoft.office.interop.word to replace( table cells text ) and I'm keeping this table inside shape, I'm trying to replace the text which is inside this table
but the Problem when replacing is done the Table Format is removing and rows is merging like photo below. Please I need to fix my code somehow to keep formatting after replacing text
original doc
output doc
I'm using this code:
private void FindAndReplace(Word.Application wordApp, object ToFindText, object replaceWithText)
{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundLike = false;
object nmatchAllforms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiactitics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
int x = 0;
var shapes = myWordDoc.Shapes;
foreach (Microsoft.Office.Interop.Word.Shape shape in shapes)
{
if (shape.TextFrame.HasText != 0)
{
var initialText = shape.TextFrame.TextRange.Text;
var resultingText = initialText.Replace(ToFindText.ToString(), replaceWithText.ToString());
if (initialText != resultingText)
{
shape.TextFrame.TextRange.Text = resultingText;
}
}
}
wordApp.Selection.Find.Execute(ref ToFindText,
ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundLike,
ref nmatchAllforms, ref forward,
ref wrap, ref format, ref replaceWithText,
ref replace, ref matchKashida,
ref matchDiactitics, ref matchAlefHamza,
ref matchControl);
}
private void CreateWordDocument(object filename, object SaveAs)
{
try
{
Word.Application wordApp = new Word.Application();
object missing = Missing.Value;
if (File.Exists((string)filename))
{
object readOnly = false;
object isVisible = false;
wordApp.Visible = false;
myWordDoc = 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 missing,
ref missing, ref missing, ref missing, ref missing);
myWordDoc.Activate();
wordApp.Application.Documents.Open(filename, ReadOnly: true, Visible: false);
DataTable dtfees = DB.GetData("select fees from fees where feesid='" + 1 + "'");
if (dtfees.Rows.Count > 1)
{
string fees1 = dtfees.Rows[0][0].ToString();
string fees2 = dtfees.Rows[1][0].ToString();
string fees3 = dtfees.Rows[2][0].ToString();
string fees4 = dtfees.Rows[3][0].ToString();
this.FindAndReplace(wordApp, "tyfes1", fees1);
this.FindAndReplace(wordApp, "Tyfes2", fees2);
this.FindAndReplace(wordApp, "Tyfes3", fees3);
}
myWordDoc.SaveAs2(ref SaveAs, 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);
myWordDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
}
}
catch (Exception ex)
{
}
}
The main thing wrong with your code is that whatever material (text/tables etc.) is in your shape, if the findText is in it and the replacement text is different from the find text, the entire content of the shape's TexTObject will be replaced by a string with no table, in this line
shape.TextFrame.TextRange.Text = resultingText;
Judging from the Find.Execute that's still in your code, I'm guessing that you tried to use the Find object to do the replace but couldn't work out how to get it to work just in the Shapes.
If I have understood what you're doing, I think the following fairly simple change will work, but you will need to check the C# and test it.
foreach (Microsoft.Office.Interop.Word.Shape shape in shapes)
{
if (shape.TextFrame.HasText != 0)
{
shape.TextFrame.TextRange.Find.Execute(ref ToFindText,
ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundLike,
ref nmatchAllforms, ref forward,
ref wrap, ref format, ref replaceWithText,
ref replace, ref matchKashida,
ref matchDiactitics, ref matchAlefHamza,
ref matchControl);
}
}

Word interop problem : String parameter too long

Im working on word app which is allowed me to change the text inside a word document but I get error String parameter too long because the text is more than 255 char . here is the code im using
private void FindAndReplace(Word.Application wordApp, object ToFindText, object replaceWithText)
{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundLike = false;
object nmatchAllforms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiactitics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
wordApp.Selection.Find.Execute(ref ToFindText,
ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundLike,
ref nmatchAllforms, ref forward,
ref wrap, ref format, ref replaceWithText,
ref replace, ref matchKashida,
ref matchDiactitics, ref matchAlefHamza,
ref matchControl);
var shapes = myWordDoc.Shapes;
foreach (Word.Shape shape in shapes)
{
var initialText = shape.TextFrame.TextRange.Text;
var resultingText = initialText.Replace(Convert.ToString(ToFindText), Convert.ToString(replaceWithText));
shape.TextFrame.TextRange.Text = resultingText;
}
}
I get an error(string parameter too long)in this line
wordApp.Selection.Find.Execute(ref ToFindText,
ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundLike,
ref nmatchAllforms, ref forward,
ref wrap, ref format, ref replaceWithText,
ref replace, ref matchKashida,
ref matchDiactitics, ref matchAlefHamza,
ref matchControl);

c# ms-word office-interop problem with FindAndReplace Header

I´m trying to FindAndReplace a Header of a document with Word.Interop in c#, but it doesn´t find the text, However when i do it for the footer it works fine. I don´t know what the problem is. My code is:
private void FindAndReplaceFooter(Word.Document aDoc,Microsoft.Office.Interop.Word.Application wordApp, object findText, object replaceWithText)
{
object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundLike = false;
object nmatchAllForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiactitics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2; // 2 replace all the occurrence, 0 replace none, 1 replace first
object wrap = 1;
foreach (Microsoft.Office.Interop.Word.Section section in aDoc.Sections)
{
Microsoft.Office.Interop.Word.Range footerRange = section.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footerRange.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundLike, ref nmatchAllForms,
ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
ref matchKashida, ref matchDiactitics, ref matchAlefHamza, ref matchControl);
if (footerRange.Find.Found)
{
Logger.Trace(Logger.GetCallerInfo(), this, "Encuentra Footer {0}:", replaceWithText);
}
}
And the code for the Header is :
object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundLike = false;
object nmatchAllForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiactitics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2; // 2 replace all the occurrence, 0 replace none, 1 replace first
object wrap = 1;
foreach (Microsoft.Office.Interop.Word.Section Wordsection in aDoc.Sections)
{
Microsoft.Office.Interop.Word.Range headerrRange = Wordsection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
// headerrRange.Select();
headerrRange.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundLike, ref nmatchAllForms,
ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
ref matchKashida, ref matchDiactitics, ref matchAlefHamza, ref matchControl);
if (headerrRange.Find.Found)
{
Logger.Trace(Logger.GetCallerInfo(), this, "Encuentra Header {0}:", replaceWithText);
}
}
I don´t know what i´m doing wrong. Thank u very much.
Best regards,
Jorge Alonso.

How to retrieve IFileOpenDialog interface in C# from IUnknown_QueryService SID_SExplorerBrowserFrame?

i'm trying to hook into file dialogs in my custom namespace extensions project.
this is done in C#.
i'm trying to follow the post:
http://blogs.msdn.com/b/winsdk/archive/2015/03/24/how-to-register-for-file-dialog-notifications-from-shell-namespace-extension.aspx
in C++ everything works, and i get the IFileOpenDialog interface:
this is done under SetSite method:
HRESULT hr = IUnknown_QueryService(m_pUnkSite, SID_SExplorerBrowserFrame, IID_PPV_ARGS(&m_fileOpenDialog));
where m_fileOpenDialog is IFileOpenDialog
i'm trying to do the same in C#, but it doesn't work...
i've tried several ways:
FileDialogNative.IFileOpenDialog o2 = Marshal.GetObjectForIUnknown(m_pUnkSite) as FileDialogNative.IFileOpenDialog;
o2 is null.
i've tried
IntPtr ptr;
Guid g = new Guid("000214f1-0000-0000-c000-000000000046");
int rc = Marshal.QueryInterface(m_pUnkSite, ref g, out ptr);
this succeeds, but i have no idea how to convert the "ptr" into the required interface.
any help would be appriciated.
**Update from the comment **,
i tried doing this:
[DllImport("shlwapi.dll")]
internal static extern int IUnknown_QueryService(IntPtr pUnk, ref Guid guidService, ref Guid riid, out IntPtr ppvOut);
Guid g = new Guid("000214F1-0000-0000-C000-000000000046"); // SID_SExplorerBrowserFrame
Guid g2 = new Guid("d57c7288-d4ad-4768-be02-9d969532d960"); // IFileOpenDialog
IntPtr pp;
int rrc = Win32.IUnknown_QueryService(pUnkSite, ref g, ref g2, out pp);
FileDialogNative.IFileOpenDialog o2 = Marshal.GetObjectForIUnknown(pp) as FileDialogNative.IFileOpenDialog;
this worked!!! thanks!!
So, Thanks to Hans Passant, i understood that the "IUnknown_QueryService" is not the same as what i tried.
i've managed to catch the interface in the following way:
[DllImport("shlwapi.dll")]
internal static extern int IUnknown_QueryService(IntPtr pUnk, ref Guid guidService, ref Guid riid, out IntPtr ppvOut);
Guid g = new Guid("000214F1-0000-0000-C000-000000000046"); // SID_SExplorerBrowserFrame
Guid g2 = new Guid("d57c7288-d4ad-4768-be02-9d969532d960"); // IFileOpenDialog
IntPtr pp;
int rrc = Win32.IUnknown_QueryService(pUnkSite, ref g, ref g2, out pp);
FileDialogNative.IFileOpenDialog dlg = Marshal.GetObjectForIUnknown(pp) as FileDialogNative.IFileOpenDialog;
Marshal.Release(pp);
Then, i was able to use dlg :)

How to Activate a Word document?

I am creating word document from c# windows application. but Word document is getting minimized even though i used the Activate() method to activate the document.
(it is working fine when debugging but in release version document getting minimized)
How I Can Make the document active one???
Thanks in advance.
my code is almost like this
Microsoft.Office.Interop.Word.Document
document;
object objFileName = locationOfFile;
object objTrueValue = true; object
objMissing = Type.Missing;
document =
application.Documents.Open(ref
objFileName, ref objMissing, ref
objTrueValue,
ref objMissing, ref objMissing, ref objMissing, ref
objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref
objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing);
document.Activate();
It is working for both.
document = application.Documents.Open(#"\Docu.docx", ReadOnly: false, Visible: true );
set the Visible:true, Document.Activate(); is not required.
It would be help more if you show your code.

Categories

Resources