none
Problem with style.font.hidden when printing RRS feed

  • Question

  • Hello!

    I have to fix a bug in a solution which I did not developed and I have never developed an Office application. There is a Word 2007 Addin which has a DocumentBeforePrint event handler registered. Whenever a document is going to be printed the event handler checks wether that document has a special boolean property and if wether it is true. When printing a "special" document, the user gets a dialog shown where he/she can decide wether he/she wants a copy print, an original print, or both.

    In the document there are pararaphs which are formatted with a user defined style named "DFG Sichtbar in Kopie". Dependent on the choice of the user the style's font is set to hidden or visible. So a copy print has some more texts than an original print.

    There are some problems, which I could not solve. In the production the user can print copies and originals. Here an example I have identified, when something goes wrong:

    1. the user prints an original and afterwards closes Word
    2. the user gets asked to save the document (because style definition has changed, font is now hidden), user says yes
    3. user opens document (document is in "original state") and prints a copy (style's font changes to visible)
    4. in the document: all paragraphs styled "DFG Sichtbar in Kopie" are displayed
    5. in the print out: not all paragraphs with style "DFG Sichtbar in Kopie" are printed, also some paragraphs with different styles who come after a paragraph styled with "DFG Sichtbar in Kopie" are not printed

    On my development environment I observed a different behaviour:

    1. 1st paragraph is always visible (different style), 2nd and 3rd paragraph styled as "DFG Sichtbar in Kopie", 4th paragraph always visible
    2. I print a copy
    3. in the document: 1st paragraph is visible, 2nd and 3rd paragraph are invisible, 4th paragraph visible
    4. in the print out: 1st paragraph visible, 2nd and 3rd are invisible, 4th paragraph visible 3 times (one for the 2nd, one for the 3rd, and one for itself)

    This is very strange and I don't know what to do. Has anyone an idea? The code is written very ugly. Sorry for that. By the way: the code wants to save an unsaved document before printing it. Does not work because unsaved documents have different time, at least on my machine.

     

    void Application_DocumentBeforePrint(Microsoft.Office.Interop.Word.Document Doc, ref bool Cancel)
    {
    	//check if the current document is a DFG document
    	bool isDFGDocument = false;
    	DocumentProperties customProps = (DocumentProperties)this.Application.ActiveDocument.CustomDocumentProperties;
    	foreach (DocumentProperty dp in customProps)
    	{
    		if (dp.Name.Equals(SettingNames.basedOnDFGFlag) && (bool)dp.Value == true) //custom property was set when the document was created
    		{
    			isDFGDocument = true;
    		}
    	}
    
    	if (isDFGDocument)
    	{
    		//check if document was saved before    
    		foreach(DocumentProperty dp in (DocumentProperties)this.Application.ActiveDocument.BuiltInDocumentProperties)
    		{
    			if (dp.Name.ToLower().Equals("last save time"))
    			{
    				DateTime? savedate = dp.Value as DateTime?;
    				if (savedate.Equals(new DateTime(1601, 1, 1, 1, 0, 0)))
    				{
    					SaveFileDialog sfd = new SaveFileDialog();
    					sfd.Filter = "Word-Dokument (*.docx)|*.docx";
    					sfd.FilterIndex = 1;
    					sfd.DefaultExt = ".docx";
    					sfd.AddExtension = true;       
    					DialogResult res = sfd.ShowDialog();
    					if (res == DialogResult.OK)
    					{
    						if (!string.IsNullOrEmpty(sfd.FileName))
    						{
    							try
    							{
    								object filename = sfd.FileName;
    								this.Application.ActiveDocument.SaveAs(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);
    							}
    							catch
    							{
    								Cancel = true;
    								return;
    							}
    						}
    						else
    						{
    							Cancel = true;
    							return;
    						}
    					}
    					else
    					{
    						Cancel = true;
    						return;
    					}       
    				}
    				
    				break;
    			}
    		}
    
    		//update fields
    		this.Application.ActiveDocument.Fields.Update();
    
    		//display custom print dialog 
    		PrintForm pf = new PrintForm();
    		DialogResult result = pf.ShowDialog();
    		if (result == DialogResult.OK)
    		{
    			try
    			{
    				//display standard print dialog
    				Word.Dialog pd = this.Application.Dialogs[Word.WdWordDialog.wdDialogFilePrint];
    				if (pd.Display(ref missing) == -1)
    				{
    					//set visibility according to selected options
    					ArrayList styles = new ArrayList();
    					try
    					{
    						foreach (Word.Style style in this.Application.ActiveDocument.Styles)
    						{
    							if (style.NameLocal.ToLower().Equals(styleName.ToString().ToLower()))
    							{
    								styles.Add(style);
    							}
    						}
    					}
    					catch (Exception)
    					{
    						if (styles.Count < 1)
    						{
    							DialogResult res = MessageBox.Show(string.Format("Die Formatvorlage \"{0}\" wurde in der Vorlage nicht gefunden.\nDrucken in Kopie steht nicht zur Verfügung. Soll stattdessen ein normaler Druckvorgang ausgeführt werden?", styleName), "Formatvorlage nicht gefunden", MessageBoxButtons.YesNo);
    							if (res != DialogResult.OK)
    							{
    								Cancel = true;
    							}
    						}
    					}
    
    					if (styles.Count>0)
    					{
    						if (pf.PrintCopy)
    						{
    							foreach (Word.Style style in styles)
    							{
    								style.Font.Hidden = 0;
    							}
    							this.Application.ActiveDocument.Fields.Update();
    							pd.Execute();
    							Cancel = true;
    						}
    
    						if (pf.PrintOriginal)
    						{
    							foreach (Word.Style style in styles)
    							{
    								style.Font.Hidden = 1;
    							}
    							this.Application.ActiveDocument.Fields.Update();
    							pd.Execute();
    							Cancel = true;
    						}
    					}
    				}
    				else
    				{
    					Cancel = true;
    				}
    			}
    			catch (Exception ex)
    			{
    				string message = ex.Message + ex.StackTrace;
    				if (ex.InnerException != null)
    				{
    					message += ex.InnerException.Message;
    				}
    
    				Logger.LogError(message);
    			}
    		}
    		else if (result == DialogResult.Cancel)
    		{
    			Cancel = true;
    		}
    	}
    }
    

     

    • Edited by Yavuz T Tuesday, May 10, 2011 11:27 AM edited some typos
    Tuesday, May 10, 2011 11:22 AM

All replies

  • Hi Yavuz,

     

    Thanks for posting in the MSDN Forum.

     

    Would you please clarify the “DocumentProperties” or show me its snippet for further research?

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 12, 2011 2:55 AM
    Moderator