none
For MS Word 2007 Importing Styles RRS feed

  • Question

  • Has anyone been able to copy Styles from one Word2007 document to another Word 2007 document and can share the code?

    I tried using the following code but i keep getting an exception "Command Failed".

     

                sourceFileName = inputFilePath + "\\" + "MCS_Word_styles.docx";

     

                myTemplateFile = inputFilePath + "\\" + "Word_styles2.docx";

     

                Word.Document myTemplateDoc = myWordApp.Documents.Open(ref myTemplateFile, ref myConfirmConversions, ref myReadOnly, ref myAddToRecentFiles, ref missing, ref missing, ref myRevert, ref missing, ref missing, ref missing, ref missing, ref myVisible, ref myOpenAndRepair, ref missing, ref myNoEncodingDialog, ref missing);

     

                for (int a = 0; a < listBox_input_files.Items.Count; a++)

                {

                    update_status("");

     

                    destFileName = inputFilePath + "\\" + listBox_input_files.Items[a];

     

                    foreach (Word.Style validStyle in myTemplateDoc.Styles)

                    {

                        myWordApp.Application.OrganizerCopy(sourceFileName, destFileName, validStyle.NameLocal.ToString(), Word.WdOrganizerObject.wdOrganizerObjectStyles);

                    }

                }

                ((Microsoft.Office.Interop.Word._Document)myTemplateDoc).Close(ref saveChanges, ref missing, ref missing);

    Friday, May 13, 2011 3:48 PM

Answers

  •  

    With specific style names for document level style updates, this is VBA code I use.  There might be other ways, but I wrote it long ago and it works. The code is executing from a custom template attached to the source document (strCurDoc) and the destination (strPathName) is a document with the normal template attached.  I am not updating any template styles here.

    Private Sub UpdateStyles(StyleName)

        Documents(strCurDoc).Activate

        GoHome

        With Selection.Find

            .ClearFormatting

            .Style = ActiveDocument.Styles(StyleName)

            .Text = ""

            .Forward = True

            .Wrap = wdFindStop

            .Format = True

            .Execute

        End With

        If Selection.Find.Found Then

            ActiveDocument.Styles(StyleName).Font = Selection.Font

            ActiveDocument.Styles(StyleName).ParagraphFormat = Selection.ParagraphFormat

            Application.OrganizerCopy Source:=ActiveDocument.FullName, _

                Destination:=strPathName, _

                Name:=StyleName, Object:=wdOrganizerObjectStyles

        End If

    End Sub

    Hope this helps!


    Regards, Rich
    Saturday, May 14, 2011 10:27 AM

All replies

  •  

    With specific style names for document level style updates, this is VBA code I use.  There might be other ways, but I wrote it long ago and it works. The code is executing from a custom template attached to the source document (strCurDoc) and the destination (strPathName) is a document with the normal template attached.  I am not updating any template styles here.

    Private Sub UpdateStyles(StyleName)

        Documents(strCurDoc).Activate

        GoHome

        With Selection.Find

            .ClearFormatting

            .Style = ActiveDocument.Styles(StyleName)

            .Text = ""

            .Forward = True

            .Wrap = wdFindStop

            .Format = True

            .Execute

        End With

        If Selection.Find.Found Then

            ActiveDocument.Styles(StyleName).Font = Selection.Font

            ActiveDocument.Styles(StyleName).ParagraphFormat = Selection.ParagraphFormat

            Application.OrganizerCopy Source:=ActiveDocument.FullName, _

                Destination:=strPathName, _

                Name:=StyleName, Object:=wdOrganizerObjectStyles

        End If

    End Sub

    Hope this helps!


    Regards, Rich
    Saturday, May 14, 2011 10:27 AM
  • Rich,

     

    Thank you!

     

    Regards,

    Steve

    Monday, May 16, 2011 2:16 PM