none
Microsoft Office 2010 Content Searching By Using Script RRS feed

  • Question

  • Hi,

    Does anyone know how to use a script or coding to grab the content of microsoft office 2010 (excel,word).

    Thanks


    Leong

    • Moved by George123345 Thursday, September 19, 2013 1:58 AM
    Wednesday, September 18, 2013 2:08 AM

All replies

  • Hi,

    This is the forum to discuss questions and feedback for Microsoft Office Programs, I'll move your question to the MSDN forum

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=officegeneral&filter=alltypes&sort=lastpostdesc

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    George Zhao
    TechNet Community Support

    Thursday, September 19, 2013 1:58 AM
  • Hi,

    From your description, I suggest you to write a VBA script to achieve your goal.

    Here is a sample for your reference:

    Word:

    Sub GetWordContent()
    Set myRange = ActiveDocument.Content
    With myRange.Font
        .Name = "Arial"
        .Size = 12
    End With
    End Sub
    

    Excel:

    Sub GetAndSetExcelRange()
    Application.ActiveSheet.Range("A2").Value = 8
    MsgBox Application.ActiveSheet.Range("A1").Value
    End Sub

    Here are some links about how to develop a macro in the Word and Excel:

    Get Started with VBA in Word 2010
    Content Property
    Getting Started with VBA in Excel 2010


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, September 19, 2013 9:35 AM
    Moderator
  • Dear Zhang,

    Thanks for the feedback.

    The purpose we need a script is to schedule running on every production workstation to scan all the microsoft office content to avoid some sexual harassment articles by searching using special keyword. Did you have any better idea to implement on it.

    Thanks


    • Edited by YS Leong Friday, September 20, 2013 4:51 AM amended
    Friday, September 20, 2013 4:49 AM
  • Hi,

    What do you mean by “scan all the Microsoft office content”? Based on my understanding, you want scan all office documents in a specified location.

    If so, I recommend you using Open XML SDK to implement it.

    Here is a sample for your reference:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using DocumentFormat.OpenXml.Spreadsheet;
    using DocumentFormat.OpenXml.Packaging;
    using System.IO;
    using System.Text.RegularExpressions;
    namespace SpreadsheetMLSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                SearchWordDocument(@"C:\Drafts\file-to-search.docx");
            }
            public static void SearchWordDocument(string document)
            {
                using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
                {
                    string docText = null;
                    using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
                    {
                        docText = sr.ReadToEnd();
                    }
                    Regex regexText = new Regex("Context To Search");
                    var result = regexText.Match(docText);
                    if (result != null && result.Captures.Count > 1)
                    {
                        // TODO
                    }
                }
            }
        }
    }


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, September 20, 2013 12:53 PM
    Moderator
  • Dear Zhang,

    Thanks for the advise.

    Yes. I just want to scan all office documents in a specified location example (C drive or D drive). I did tested by using the sample Open XML coding but have some error during double click. Did you have any idea. Below is the coding modify area :-

    SearchWordDocument(@"D:\test.docx");

    Regex regexText = new Regex("sex");


    Leong

    Tuesday, September 24, 2013 3:07 AM